3

The problem is I cant read the variable input with next() cause when I try to split (.split" ") every whitespace then the array just get the first two words I type so I had to use keyboard.nextLine() and the splitting process works the way it should work and I get all the words in the array but the problem is that If I use nextLine() then I have to create another keyboard object to read the first variable (answer) and that is the only way I can make it work here is the code

    Scanner keyboard=new Scanner(System.in);
    Scanner keyboard2=new Scanner(System.in);//just to make answer word

    int answer=keyboard.nextInt();//if I don't use the keyboard2 here then the program will not work as it should work, but if I use next() instead of nextLine down there this will not be a problem but then the splitting part is a problem(this variable counts number of lines the program will have).

    int current=1;
    int left=0,right=0,forward=0,back=0;

    for(int count=0;count<answer;count++,current++)
    {
        String input=keyboard.nextLine();
        String array[]=input.split(" ");
        for (int counter=0;counter<array.length;counter++)
        {
            if (array[counter].equalsIgnoreCase("left"))
            {
                left++;
            }
            else if (array[counter].equalsIgnoreCase("right"))
            {     
                right++;
            }
            else if (array[counter].equalsIgnoreCase("forward"))
            {
                forward++;   
            }
            else if (array[counter].equalsIgnoreCase("back"))
            {     
                back++;
            }
        }

   }
}

Thanks :)

Jonas
  • 121,568
  • 97
  • 310
  • 388
user1076331
  • 437
  • 2
  • 5
  • 4
  • 1
    Could you please edit your question to include punctuation. Its really hard to understand what your question is. – W.K.S Jan 15 '12 at 21:12

2 Answers2

13

Put keyboard.nextLine() after this line:

int answer=keyboard.nextInt();

This is a common problem that usually happens when you use nextLine() method after nextInt() method of Scanner class.

What actually happens is that when the user enters an integer at int answer = keyboard.nextInt();, the scanner will take the digits only and leave the new-line character \n. So you need to do a trick by calling keyboard.nextLine(); just to discard that new-line character and then you can call String input = keyboard.nextLine(); without any problem.

Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

My first suggestion is to just add the entire line, split on a space, to a String array.
Then, assign answer the 0 index of the array, utilizing the Integer.parseInt method.

Here is your code, refactored.

Scanner keyboard = new Scanner(System.in);
String[] strings = keyboard.nextLine().split(" ");
int answer = Integer.parseInt(strings[0]);
int left = 0, right = 0, forward = 0, back = 0;
for (int countA = 0; countA < answer; countA++) {
    for (String string : strings) {
        switch (string.toLowerCase()) {
            case "left" -> left++;
            case "right" -> right++;
            case "forward" -> forward++;
            case "back" -> back++;
        }
    }
}

Input

10 left right back left

Output

answer = 10
strings = [10, left, right, back, left]
left = 20
right = 10
forward = 0
back = 10
Reilas
  • 3,297
  • 2
  • 4
  • 17