This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
I am trying to take multiple input from user of different classes, if I am working only with String type my code work fine but when I am using any other class like int, flot or any other it ignore the next String input why is this happening can any one help me.
Scanner obj = new Scanner(System.in);
String a,b,c;
int x,y,z;
System.out.print("Enter any string: ");
a = obj.nextLine();
System.out.print("enter any int: ");
x = obj.nextInt();
System.out.print("Enter any thing: "); //after getting input for int it ignore next string
b = obj.nextLine(); input
System.out.print("Enter any thing: ");
c = obj.nextLine();
After you call obj.nextInt(), you still have the EOL from the Return that the user pressed after entering the number sitting in the input buffer, because nextInt() only read the numeric characters. You have to skip those characters by calling obj.nextLine() and just ignoring the result. Then you can go on and ask for additional input from the user.
This gives you what you expected:
Scanner obj = new Scanner(System.in);
String a,b,c;
int x,y,z;
System.out.print("Enter any string: ");
a = obj.nextLine();
System.out.print("enter any int: ");
x = obj.nextInt();
obj.nextLine();
System.out.print("Enter any thing: "); //after getting input for int it ignore next string
b = obj.nextLine();
System.out.print("Enter any thing: ");
c = obj.nextLine();
System.out.println("1: " + b);
System.out.println("2: " + c);
Sample run:
Enter any string: anystring
enter any int: 12345
Enter any thing: anything
Enter any thing: more anything
1: anything
2: more anything
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I'm trying to make a calculator in java that can multiply subtract and add depending if the user wants that they can choose what they want. For some reason its giving me a weird output
Code
import java.util.Scanner; // Import the Scanner class
public class calculator {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
//System.in is a standard input stream
System.out.print("Enter first number- ");
int a = sc.nextInt();
System.out.print("Enter second number- ");
int b = sc.nextInt();
System.out.print("Do you want to multiply, add, divide, or subtract? ");
String c = sc.nextLine();
switch(c) {
case "multiply":
System.out.print(a * b);
break;
case "add":
System.out.print(a * b);
break;
default:
System.out.print("Invalid input!");
}
}
}
Output
Enter first number- 2
Enter second number- 2
Do you want to multiply, add, divide, or subtract? Invalid input!
Like I didnt even type Invalid input it just does it by itself for some reason
There can be input left in the scanner before you request a value. In this case, the line break marks the end of the integer, but is not consumed as part of the integer. The call to nextLine() sees there is already an unused line break at the end of the buffer and returns that result. In this case, an empty string is returned. One way to fix this is to consume that unused line break first before requesting the next line or requesting a full line then parsing an integer from it.
Scanner scan = new Scanner(System.in);
// Always request a full line
int firstInt = Integer.parse(scan.nextLine());
int secondInt = Integer.parse(scan.nextLine());
String option = scan.nextLine();
// Use an extra call to nextLine() to remove the line break causing the issues
int firstInt = scan.nextInt();
int secondInt = scan.nextInt();
scan.nextLine(); // Consume the unused line break
String option = scan.nextLine();
sc.nextInt() does not read the enter key that you entered, so sc.nextLine() will read that new line and return it. Use sc.next() instead of sc.nextLine() to avoid this issue. Your code also multiplies the numbers when the user inputs add, so I changed that as well.
import java.util.Scanner; // Import the Scanner class
public class calculator {
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
//System.in is a standard input stream
System.out.print("Enter first number- ");
int a = sc.nextInt();
System.out.print("Enter second number- ");
int b = sc.nextInt();
System.out.print("Do you want to multiply, add, divide, or subtract? ");
String c = sc.next();
switch(c) {
case "multiply":
System.out.print(a * b);
break;
case "add":
System.out.print(a + b);
break;
default:
System.out.print("Invalid input!");
}
}
}
This question already has answers here:
Scanner is not reading the whole sentence sentence
(2 answers)
Closed 3 years ago.
I am trying to get the standard input of int, double and String(same order) and then printing out the input in order(String, double and then int). Here is the code:
public static void main (String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer: ");
int intNum = scan.nextInt();
System.out.println("Enter a double: ");
double doubleNum = scan.nextDouble();
System.out.println("Enter your string: ");
String str=scan.next();
scan.close();
System.out.println("Your string is: "+ str);
System.out.println("Your double is: "+ doubleNum);
System.out.println("Your integer is: "+ intNum);
}
But when I am giving the input(for example):
Enter an integer:
88
Enter a double:
11.1
Enter your string:
Hello World.
The output I am getting is:
Your string is: Hello
Your double is: 11.1
Your integer is: 88
Here I am not getting the full string. Where am I going wrong?
I have tried 'nextLine' but it did not work.
The problem is in this line - String str = scan.next();,
.next() function only take the first string value till it encounters a whitespace or a new line.
Instead use - String str = scan.nextLine(); which takes the whole string line till it encounters a new line.
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 5 years ago.
I ran into a problem that I don't understand. nextLine() should be for sentences, right?
System.out.println("Enter film's name");
a = scan.nextLine();
System.out.println("What number did the film released?");
b = scan.nextInt();
System.out.println("Who's the director?");
c = scan.nextLine();
System.out.println("How long is the film in minutes?");
d = scan.nextInt();
System.out.println("Have you seen the movie? Yes/No?");
e = scan.next();
System.out.println("Mark for the film?");
f = scan.nextDouble();
It runs correctly till the releasing date, and then it shows "Who is the director" and "How long is the film" together and doesn't work like it supposed to work.
How can you use nextLine(); and why doesn't it work for me?
The buffer is stuffed really reset your scanner after every consecutive calls. scan.reset(); . The reason is that previous characters are cached in the input stream.
Your problem is that Scanner.nextInt() does not reads to the next line. So you need to issue nextLine() calls too and throw away their content:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter film's name");
String a = scan.nextLine();
System.out.println("What number did the film released?");
int b = scan.nextInt();
scan.nextLine(); // this
System.out.println("Who's the director?");
String c = scan.nextLine();
System.out.println("How long is the film in minutes?");
int d = scan.nextInt();
scan.nextLine(); // this
System.out.println("Have you seen the movie? Yes/No?");
String e = scan.next();
System.out.println("Mark for the film?");
double f = scan.nextDouble();
scan.nextLine(); // after nextDouble() too
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
I called a scanned object, using same object I am trying to take from user a int and a string.
Scanner scan = new Scanner(System.in);
//for a int
int first_value = scan.nextInt();
System.out.println(first_value);
//for a string
String first_name = scan.nextLine();
System.out.println(first_name);
Here console not waiting for string but If I use another object then it's working fine.
Scanner insert = new Scanner(System.in);
String name = insert.nextLine();
Is it possible to get int and string using same Scanner object ?
That is because after reading int there left a new line character which was consumed by your scan.nextLine and it did not wait for your next input. You need to consume it before reading a String.
Scanner scan = new Scanner(System.in);
//for a int
int first_value = scan.nextInt();
scan.nextLine();
System.out.println(first_value);
//for a string
String first_name = scan.nextLine();
System.out.println(first_name)
Use System.out.println("Enter Your Age"); and System.out.println("Enter Your Name"); just before taking input from user by Scanner Object.
System.out.println("Enter your first name:");
String first_name = scan.nextLine();
System.out.println("Enter your age:");
int age = Integer.parseInt(scan.nextLine());
Now it enables you to hold the screen For entering you Input to the Program.
I need to write a test class that will do the following:
a. Let the user input an integer and display it.
b. Let the user input a float value and display it.
c. Let the user input his/her name (no white spaces) and display the
name as: “Hello <name>, welcome to Scanner!”
d. Let the user input a character and display it.
e. Let the user input any string (with white spaces) and display it.
My questions is, how can I simply scan just a Character and display it? And in number 2, How can I input a String with white spaces and display it? (letters "d" and "e")
I've searched around, but I cannot find the simplest solution (since I'm new to Java and programming).
Here is my code so far:
package aw;
import java.io.PrintStream;
import java.util.Scanner;
public class NewClass1
{
public static void main(String[] args)
{
int num;
double num2;
String name;
char c;
Scanner sc = new Scanner(System.in);
PrintStream ps = new PrintStream(System.out);
//for integer
System.out.println("Enter a number: ");
num = sc.nextInt();
ps.printf("%d\n", num);
//for float
System.out.println("Enter a float value: ");
num2 = sc.nextDouble();
ps.printf("%.2f\n", num2);
//for name w/o white space
System.out.print("Enter your first name: ");
name = sc.next();
ps.printf("Hello %s, welcome to Scanner\n", name);
//for character
System.out.print("Enter a character: ");
c = sc.findWithinHorizon(".", 0).charAt(0);
System.out.print(“%c”, c);
//for name w/ white space
System.out.print("Enter your full name: ");
name = sc.nextLine();
System.out.print(“%s”, name);
}
}
I hope you can help me. Thanks!
First, there's no need to wrap System.out in a PrintStream because out already supports formatting with format() or printf() methods.
Next, you need to understand that when you input a line of data you also terminate it with a new line \n. The next<Type>() methods only consume the <Type> and nothing else. So, if a next<Type>() call may match \n, you need to skip over any extra new lines \n with another nextLine() before.
Here's your code with fixes:
int num;
double num2;
String name;
char c;
Scanner sc = new Scanner(System.in);
//for integer
System.out.print("Enter a number: ");
num = sc.nextInt();
System.out.printf("%d\n", num);
//for float
System.out.print("Enter a float value: ");
num2 = sc.nextDouble();
System.out.printf("%.2f\n", num2);
//for name w/o white space
System.out.print("Enter your first name: ");
name = sc.next();
System.out.printf("Hello %s, welcome to Scanner\n", name);
//for character
System.out.print("Enter a character: ");
c = sc.findWithinHorizon(".", 0).charAt(0);
System.out.printf("%c\n", c);
sc.nextLine(); // skip
//for name w/ white space
System.out.print("Enter your full name: ");
name = sc.nextLine();
System.out.printf("%s", name);
Use Scanner.next(Pattern) and pass Pattern.compile("[A-Za-z0-9]") to let scanner accept only 1 character defined. You can pass any regex as argument and check for next() Scanner.next(); for next line with spaces
Use this:
//for a single char
char Character = sc.findWithinHorizon(".", 0).charAt(0);
//for a name with white space
System.out.print("Enter your full name: ");
String name2 = sc.next();
String surname = sc.next();
System.out.println(name2 + " " + surname);