This question already has answers here:
Understanding Scanner's nextLine(), next(), and nextInt() methods
(2 answers)
Closed 6 years ago.
I am making a program and am using user input. When I am getting a String I have always used .nextLine(), but I have also used .next() and it does the same thing. What is the difference?
Scanner input = new Scanner(System.in);
String h = input.nextLine();
String n = input.next();
What is the difference?
To make long story short:
nextLine - reads the whole line.
next - reads until the first space.
As an example:
// Input : Barack Obama
String st = in.next(); // st holds the String "Barack"
String st2 = in.nextLine(); //st2 holds the String "Barack Obama"
Related
This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 5 years ago.
I have made this program and I have gotten stuck. When I run it and I have inputs without using spaces it works fine, for example just Bob input in customer input. However, when I enter Bob White, it merges the next two string input directions (as Shown in pic attached) . What am I doing wrong here?
import java.util.*;
public class Blah{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
String customerName;
System.out.println("Enter customer name: ");
customerName = in.next();
String customerAddress;
System.out.println("Enter customer address: ");
customerAddress = in.next();
String customerPhoneNumber;
System.out.println("Enter customer phone number: ");
customerPhoneNumber = in.next();
in.close();
}
}
Thats because the Scanner#next method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine
Try using nextLine() method instead with trem() method
customerName = in.nextLine().trim();
You need to read the entire line using nextLine() method then trim it from leading and trailing whitespace using trim() method.
customerName = in.nextLine().trim();
customerAddress = in.next().trim();
customerPhoneNumber = in.next().trim();
This question already has answers here:
What's the difference between next() and nextLine() methods from Scanner class?
(15 answers)
Closed 6 years ago.
I have a error when i give a input for string t= "is my favorite language";
it shows output java is . please tell what i made a mistake.
public class DataTypes {
public static void main(String[] args) {
String s = "Java ";
Scanner scan = new Scanner(System.in);
String t = scan.next();
String u = s.concat(t);
System.out.println(u);
}
}
scan.next() will get the next token from the input. The default delimiter for this is whitespace, so it will get the first word from your input.
To get all of the input up until the newline (when the user presses enter) use scan.nextLine() instead.
String s = "Java ";
Scanner scan = new Scanner(System.in);
String t = scan.nextLine();
String u = s + t;
System.out.println(u);
Use:
scanner.nextLine()
This will read a whole line till the system defined line seperator (usually \n)
scanner.next() only reads the next token till space.
This question already has answers here:
Take a char input from the Scanner
(24 answers)
Closed 8 years ago.
This is what I have in my code
char guess = Keyboard.readChar();
but the error message comes up as "The method readChar() is undefined for the type scanner" The scanner i have is Scanner keyboard = new Scanner (System.in). Why Is this wrong?
you need to use this
char guess = keyboard.next().charAt(0);
Scanner does not have a method to read a char. Fundamentally, System.in is a buffered stream. You can read a line,
while(keyboard.hasNextLine()) {
String line = keyboard.nextLine();
char[] chars = line.toCharArray(); // <-- the chars read.
}
You can trying using nextLine() which reads in a String of text.
char code = keyboard.nextLine().charAt(0);
charAt(0) takes in the first character of the received input.
Additional Note:
If you want to convert user inputs to upper/lower case. This is especially useful.
You can chain String methods together:
char code1 = keyboard.nextLine().toUpperCase().charAt(0); //Convert input to uppercase
char code2 = keyboard.nextLine().toLowerCase().charAt(0); //Convert input to lowercase
char code3 = keyboard.nextLine().replace(" ", "").charAt(0); //Prevent reading whitespace
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
Closed 8 years ago.
Consider the following snippet:
System.out.print("input iteration cycles");
Scanner reader=new Scanner(System.in);
int iteration = reader.nextInt();
System.out.print("input choice: Var or par");
String choice=reader.nextLine();
System.out.print(choice);
I was hoping to get one line where it prints the first input and the next print out to only print the next input which is either var or par, however it seems that the second print prints out the second input in addition to the first input. Any idea why?
nextInt() does not advance to the next line. So you must make an explicit call to nextLine() immediately after it. Otherwise, choice will be given that newline. Just add this line to your code:
System.out.print("input iteration cycles");
Scanner reader=new Scanner(System.in);
int iteration = reader.nextInt();
reader.nextLine(); //<==ADD
System.out.print("input choice: Var or par");
String choice=reader.nextLine();
System.out.print(choice);
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 8 years ago.
I was writing my first, well second if you count hello world, program and ran into a small issue.
My code:
import java.util.*;
class test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("What is your name: ");
String name = scan.nextLine();
System.out.print("What is your favorite number: ");
int favoriteNumber = scan.nextInt();
System.out.print("What is your favorite game: ");
String game = scan.nextLine();
}
}
So it scans in name and favoriteNumber but the program ends before asking the user to enter a favorite game. Essentially the user can only enter text twice instead of three times like I would like.
Just a guess: scan.nextInt() does not consume the trailing newline character after the user presses enter. The subsequent call to scan.nextLine() then finds a newline character waiting.
Try switching out int favoriteNumber = scan.nextInt(); for String favoriteNumber = scan.nextLine(); to see if that fixes the issue. If it does, my hypothesis is correct.
If that is the case, then you should probably use Integer.parseInt to convert this string into an integer. The problem here is effectively that you really want to collect 3 lines of input, where a line is defined as "any sequence of characters ending with a newline". Your program is currently written to request a line, an int, then a line, rather than "3 lines of input, one of which contains an int".
an exception must be thrown in here scan.nextInt();
Check whether you entered a proper int