0

I'm writing a simple tic tac toe game and need to accept user input during their turn. The player should simply provide a set of coordinates for where to place their token (1,1) to (3,3). I am supposed to be able to accept input as either "2 1" or "2,1" or "2, 1". So I need to be able to take their String input and pull out each of the two numbers, regardless of delimiter and use them to assign their token to the specified cell in the 3x3 array.

The major catch is only being able to utilize stuff we've been taught already (this is the first quarter of Java). This is the first seven chapters of Building Java Programs which consists of Scanner, conditionals/logic, loops and arrays. No patterns, matchers, lists, etc.

Is there a way to accomplish this using only the String class, scanner, or arrays?

Gautam Savaliya
  • 1,209
  • 2
  • 17
  • 28

2 Answers2

0

Just using the String class, you can use String.split() to get an array of strings which can then be parsed to Integers

public class Example{

 public static void main(String []args){
     String str = "2 1";
     // first split the original string on a comma
     String[] str_arr = str.split(",");
     // if the length is one then there were no commas in the input, so split again on white space
     if (str_arr.length == 1){
         str_arr = str.split(" ");
     } 
     int[] int_arr = new int[str_arr.length];
     // assign the string array to an int array
     for (int i = 0; i < str_arr.length; i++){
         int_arr[i] = Integer.parseInt(str_arr[i]);
     }
    // output to console         
     for (int j : int_arr){
         System.out.println(j);
     }

 }
}
Kiz
  • 399
  • 4
  • 11
  • I ended up with a variation of this. And I tried it out before posting this time :) Thanks to all! – Aaron Hansen Jun 12 '16 at 07:40
  • @Kiz You may want to handle the exception when the user wants to input "2, 1". java.lang.NumberFormatException. – Twahanz Jun 12 '16 at 07:57
  • Actually I fixed the problem by adding a line at the top: `System.out.println("Please provide your input in the format: \"x y\"");` – Aaron Hansen Jun 12 '16 at 09:10
0

Updated

Forgot to add "" to convert char to String.

Scanner input = new Scanner(System.in);

String userInput;
String[] coordinates = new String[2];

char character;
int length;

userInput = input.nextLine();
length = userInput.length();

if(length > 2){
  coordinates[0] = "" + userInput.charAt(0);
  character = userInput.charAt(2);

  if(character != ',' && character != ' '){
    coordinates[1] = "" + character;
  }
  else{
    coordinates[1] = "" + userInput.charAt(3);
  }
}

Explained:

We use an Array to store the two positons you need.

We use a Character to store read in input positions.

We get the length of the read input. This is to validate if it is correct. Since the correct input should be at least more than 2 characters.

We know that the first position is valid so we assign it immediately.We also know that the second position cannot be valid so we skip it (charAt(2) and not charAt(1)) Then we check if the third position is valid if not we assign the fourth position.

Goodluck!

Twahanz
  • 204
  • 1
  • 14