-1

I have been searching for this but couldn't find it. I want to get command moves input from user in Java using Scanner. The inputs the user can make are U(up), D(down), L(left), R(right). So for instance if the user writes UDULR (with nothing separating them) then I want to read in the letters separately. I tried like this:

String command;
char command;
Scanner scan = new Scanner(System.in);

System.out.println("Enter commands U(up),D(down),L(left),R(right);
//command = scan.next();                       
command = scan.next().charAt(0);

if(command == 'U'){
    "Do this"
}
else if(command == 'D'){
    "Do that"
}

When I use this code it only recognizes the first letter, (in this case I understand that charAt(0) represents the first letter). But how is it possible to get the other inputs? I tried both with String and char.

Tunaki
  • 116,530
  • 39
  • 281
  • 370
toppen
  • 71
  • 1
  • 12
  • You might need something like scan.next().split('') which will give you an array of commands. Then you could loop over those commands – kuporific Nov 01 '15 at 19:55
  • Possible duplicate of [Take a char input from the Scanner](http://stackoverflow.com/questions/13942701/take-a-char-input-from-the-scanner) – smac89 Nov 01 '15 at 19:59

2 Answers2

2

By default Scanner uses one or more whitespaces as delimiter, which separates tokens, so next returns token representing entire word, not single characters.

If you want next to return single non-whitespace characters then you can set delimiter to

  • series of whitespaces (\s+ in regex where \ needs to be written as "\\" in String)

or

  • empty string ""

But since zero OR one or more of X is same as zero or more of X, instead of + which in regex means "one or more occurrences", we can use * which represents "zero or more occurrences". So regex representing our delimiter may look like \s+ (which in String literal needs to be written as "\\s+" since \ needs to be escaped)

Demo:

Scanner scan = new Scanner(System.in);
scan.useDelimiter("\\s*");
System.out.println("Enter commands U(up),D(down),L(left),R(right)");
String command = scan.next();

if (command.equalsIgnoreCase("U")) {
    System.out.println("Do this");
} else if (command.equalsIgnoreCase("D")) {
    System.out.println("Do that");
} else {
    System.out.println("unknown command: "+ command);
}

Based on your comment it looks like you want to handle group of commands passed as one word like UDULR which should move up, down, up, left and finally right. In that case you could organize your code like:

private void moveUp(){
    //code for moving up:
    System.out.println("moving up");
}

private void moveDown(){
    //... similar 
}
//rest of moving methods...

Now you can add one more method which will let chose which method to use based on char we pass to it:

public void move(char dirrection){
    switch(dirrection){
       case 'U' : moveUp(); break;
       case 'D' : moveDown(); break;
       case 'L' : moveLeft(); break;
       case 'R' : moveRight(); break;
       default: System.out.println("can't move in dirrection: "+dirrection);
    }
}

Now in your code you should be able to use something like:

System.out.println("Enter commands U(up),D(down),L(left),R(right)");
String command = scan.next();

for (char directionCommand : command.toCharArray()){
    move(directionCommand);
}
Pshemo
  • 113,402
  • 22
  • 170
  • 242
  • Thank you for answer. If the user presses for example UD then I want to print both "Do this" "Do that", but in this example it only prints the first. I guess maybe the if-statement is wrong?? How can I solve that? – toppen Nov 01 '15 at 20:15
  • In that case you want to read it as word (so there is no need to change original delimiter) and simply iterate over all characters from that word and act accordingly for all characters. Will update my answer. – Pshemo Nov 01 '15 at 20:17
  • @toppen I updated my answer. You may want to make these methods static if you don't want them to be dependant on instances of your class. – Pshemo Nov 01 '15 at 20:29
0

You need to save the result of next() in a variable; then you can use it to get any character in it in any order you want.

Scott Hunter
  • 44,196
  • 8
  • 51
  • 88
  • Thanks for answer, but I don't really understand, how do I save the result of next() in a variable? And how do I then get the characters from that variable? – toppen Nov 01 '15 at 20:02
  • What do you think command= does? What do you think charAt(i) does? – Scott Hunter Nov 01 '15 at 21:17
  • Well doesn't command = look if charAt(i) is the same as input? – toppen Nov 02 '15 at 08:33
  • 'This method returns the character located at the String's specified index. The string indexes start from zero.' So basically command = will look the character in the specified index. 0,1,2 etc. Right? – toppen Nov 02 '15 at 14:05
  • That's what charAt() does; command = *assigns" that result to the variable command. – Scott Hunter Nov 02 '15 at 14:23
  • Okay, and how do I "assign" then the first, second, third etc. And separate them? But still get use of them UDLR. For example: UUDLLRR and still get all those inputs to "command".? – toppen Nov 02 '15 at 15:18
  • You don't really understand what "assign" means, do you? – Scott Hunter Nov 02 '15 at 16:04
  • Maybe not, English is not my first language. So you don't need to be so cocky about it, if you don't want to answer then it is okay. Just let it be then. You don't have to write stupid comments. – toppen Nov 02 '15 at 16:08
  • I'm *trying* to answer; if you don't know what a term means, there isn't much point in my trying to use it in an explanation. Similarly for operators; = does not *look* anything, for example: it puts a value (that is, *assigns* it) into a variable. – Scott Hunter Nov 02 '15 at 18:05