You could take the first character from Scanner.next:
char c = reader.next().charAt(0);
To consume exactly one character you could use:
char c = reader.findInLine(".").charAt(0);
To consume strictly one character you could use:
char c = reader.next(".").charAt(0);
Answer from ShihabSoft on Stack OverflowYou could take the first character from Scanner.next:
char c = reader.next().charAt(0);
To consume exactly one character you could use:
char c = reader.findInLine(".").charAt(0);
To consume strictly one character you could use:
char c = reader.next(".").charAt(0);
You can do input.next(pattern) to get the next input which matches a certain pattern. If you set this to "." then it will just get the next character entered.
char c = input.next(".").charAt(0);
Input:
import java.util.Scanner;
class HelloWorld {
public static void main(String[] args) {
Scanner abc = new Scanner(System.in);
System.out.println("What's your name?");
String name = abc.nextLine();
System.out.println("How old are you?");
int age = abc.nextInt();
System.out.println("What is your favourite movie?");
String movie = abc.nextLine();
System.out.println("What's your contact number?");
int phone = abc.nextInt();
System.out.print("Your name is " + name + ".You're " + age + ".Your favourite movie is " + movie + ", and your contact is " + phone);
}
}The above input is giving the following output in all my IDEs:
What's your name? Zachary Hach // input by me How old are you? 23 // input by me What is your favourite movie? What's your contact number? 1234567890 // input by me Your name is Zachary Hach.You're 23.Your favourite movie is , and your contact is 1234567890
Now, my question is why the system didn't ask me to input my favorite movie. It skips the movie part and goes straight to the contact number. Why is this happening? Why won't it let me input my favorite movie? It works fine with the name part though which uses the same code as the movie part.
next() vs nextLine()
java - Understanding Scanner's nextLine(), next(), and nextInt() methods - Stack Overflow
Reading input from user - Java - Code with Mosh Forum
java - Scanner is skipping nextLine() after using next() or nextFoo()? - Stack Overflow
Here's the code I'm referring to: https://paste.ofcode.org/ARGhGxyDuLSPrehfYWUgLu
in my CSC class (its a low level java class) we were assigned with the task of essentially filling in code based a lot of gaps and comments that were given to us by the teacher. The above code was my result
When I run this code (via eclipse) it runs fine up to line 30, outputs line 30 - but then line 31 doesn't give me a field to type in any input at the command line. The entire program just runs as if line 31 doesn't exist.
However, if I change line 31 to repeat = input.next(); - it works perfectly fine and allows me to type in input.
Why is this? I don't see any reason why repeat = input.nextLine(); wouldn't at least give me a field to type at the command line.
nextLine() reads the remainder of the current line even if it is empty.
Correct.
nextInt() reads an integer but does not read the escape sequence "\n".
Correct1.
next() reads the current line but does not read the "\n".
Incorrect. In fact, the next() method reads the next complete token. That may or may not be the rest of the current line. And it may, or may not consume an end-of line, depending on where the end-of-line is. The precise behavior is described by the javadoc, and you probably need to read it carefully for yourself in order that you can fully understand the nuances.
So, in your example:
The nextInt() call consumes the
2character and leaves the position at theNL.The next() call skips the
NL, consumesHandi, and leaves the cursor at the second NL.The nextLine() call consumes the rest of the 2nd line; i.e. the
NL.
Note that the above assumes that you are using the default delimiter. (As your example does.) If you change the scanner's delimiter by calling one of the useDelimiter(...) methods, you can cause (for example) next() to return newline characters as part of a token.
1 ... except that your terminology is wrong. When the data is being read, it is not an escape sequence. It is an end-of-line sequence that could consist of a CR, a NL or a CR NL depending on the platform. The escape sequences you are talking about are in Java source code, in string and character literals. They may >>represent<< a CR or NL or ... other characters.
If your input is 2 hello hi
nextInt() - just read the next token as a int (Here - it is 2) otherwise it give error
next() - Just read the next token, (here - it is hello)
nextline() - It read a line until it gets newline (here - after read previous 2 hello input by nextInt, next; nextline read hi only because after that it finds a newline )
if input is
2
Hi
Hello
nextInt, next is same for above discussion .
In nextline(), it finds newline after completing the input Hi read by next(). So, nextline() stops to read input for getting the newline character.
That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter" and so the call to Scanner.nextLine returns after reading that newline.
You will encounter the similar behaviour when you use Scanner.nextLine after Scanner.next() or any Scanner.nextFoo method (except nextLine itself).
Workaround:
Either put a
Scanner.nextLinecall after eachScanner.nextIntorScanner.nextFooto consume the rest of that line including newline:int option = input.nextInt(); input.nextLine(); // Consume left over newline String str1 = input.nextLine();Or, even better, read the input through
Scanner.nextLineand convert your input to the proper format you need. For example, you may convert to an integer using theInteger.parseInt(String)method:int option = 0; try { option = Integer.parseInt(input.nextLine()); } catch (NumberFormatException e) { e.printStackTrace(); } String str1 = input.nextLine();
The problem is with the input.nextInt() method; it only reads the int value. So when you continue reading with input.nextLine() you receive the "\n" Enter key. So to skip this you have to add the input.nextLine().
Try it like this, instead:
System.out.print("Insert a number: ");
int number = input.nextInt();
input.nextLine(); // This line you have to add (It consumes the \n character)
System.out.print("Text1: ");
String text1 = input.nextLine();
System.out.print("Text2: ");
String text2 = input.nextLine();