This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
With this code, I'm looking to get 2 foods from the user. The print statement with food1 prints and displays the string the user entered correctly in the console.
With the print statement for food2, it will print the statement but the string that the user input will not display along with it.
I've tried using print/println neither change anything.
for(x=1; x <= 2; x++)
System.out.print("Enter nationality of the food: ");
Nation = sc.nextLine();
System.out.print("Enter your first food: ");
food1 = sc.nextLine();
System.out.print("Enter food price: ");
value1 = sc.nextDouble();
System.out.print("Enter your second food choice: ");
food2 = sc.nextLine();
sc.next();
System.out.print("Enter food price: ");
value2 = sc.nextDouble();
total = (value1 + value2);
System.out.println("Your food nationality: " + Nation);
System.out.printf("Your total price is: %.2f\n", total);
System.out.print("Would you like to list your items? (1 For Yes/2 For No)");
choice = sc.nextInt();
while( choice == 1)
System.out.println("Your first food: " + food1); // This one displays correctly in the console
System.out.println("Your second food: " + food2); // This one does not display the string that the user inputted in the console
choice++;
sc.nextLine();
sc.nextDouble() does not take in the entire line, only the next decimal value. Therefore you will need to read in that line before reading in food2.
System.out.print("Enter your first food: ");
food1 = sc.nextLine();
System.out.print("Enter food price: ");
value1 = sc.nextDouble();
sc.nextLine(); // add this line
System.out.print("Enter your second food choice: ");
food2 = sc.nextLine();
sc.next(); // I am not sure what you are doing here. If I understand your code correctly, you can delete this
System.out.print("Enter food price: ");
value2 = sc.nextDouble();
sc.nextLine(); // add this line
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(25 answers)
Closed 5 years ago.
So everything compiles fine but when I run it the line asking for city and the line asking for zip both print out at the same time. I need them to print individually so the user can answer.
import java.util.Scanner;
public class PersonalInfo
{
public static void main(String[] args)
{
String name, city, state, major;
int zip, phone, address;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter your name: ");
name = scanner.nextLine();
System.out.println("please enter your address number: ");
address = scanner.nextInt();
System.out.println("Please enter the name of the city you live in: ");
city = scanner.nextLine();
System.out.println("Please enter your zip code: ");
zip = scanner.nextInt();
System.out.println("Please enter the name of the state you live in: ");
state = scanner.nextLine();
System.out.println("Please enter your phone number(format ##########): ");
phone = scanner.nextInt();
System.out.println("Please enter your college major: ");
major = scanner.nextLine();
System.out.println(name + "\n" + address + "," + city + "," + state +
"," + zip + "\n" + phone + "\n" + major);
}
}
The only method that consume newline of the input is nextLine(), so if you use nextInt() and then you want to capture anything else you have to call a nextLine() after you call nextInt().
For example:
System.out.println("please enter your address number: ");
address = scanner.nextInt();
scanner.nextLine();
System.out.println("Please enter the name of the city you live in: ");
city = scanner.nextLine();
Output:
please enter your address number:
567
Please enter the name of the city you live in:
Puerto Montt
I'm new to java and I made this program, the problem I'm asking the user to input their age and name then the program should use the input to print the statement. however the when I type the age then press enter the program executes all the statements without waiting for me to enter my name.. how can I fix this?
Scanner input = new Scanner(System.in);
int age;
String name;
System.out.println("Please enter your age ");
age = input.nextInt();
System.out.println("and now enter your name");
name = input.nextLine();
System.out.println("Your name is " +name + " and you're " +age + "years old");
You can use nextLine() for both:
System.out.println("Please enter your age ");
age = Integer.parseInt(input.nextLine());
System.out.println("and now enter your name");
name = input.nextLine();
The problem is that when you hit Enter after you input the age, the resulting newline is interpreted as the end of the input for the following nextLine().
You could also use next() instead of nextLine(), but then John Doe would be interpreted as just John because next() uses whitespace as a separator.
Use next() instead of nextLine() at name variable.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age;
String name;
System.out.println("Please enter your age ");
age = input.nextInt();
System.out.println("and now enter your name");
name = input.next();
System.out.println("Your name is " +name + " and you're " +age + " years old");
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
My program should ask users to enter some value and evetually those value should be printed out depending on the menu option choosen (in my case it's 3). I have used System.out.println("Your name: " + name); to print out the name inserted by user, but unfortunately it can't print out the name. The line is just left empty. Why so? How can I fix it.
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int userChoose;
String name = null;
int accNum = 0;
double initiateAmount = 0;
double newAmm = 0;
double depOrWith = 0;
System.out.println("WELCOME TO OUR BANK!\n\n"
+ "...................\n"
+ "...................\n\n"
+ "Choose your optiin:\n"
+ "1. Create new account\n"
+ "2. Deposit/withdraw\n"
+ "3. View details\n"
+ "4. Deleting an account\n"//not used yet
+ "5. View all the accounts\n"//not used yet
+ "6. Quit\n\n");
System.out.println("*************\n"
+ "************");
while (true) {
userChoose = sc.nextInt();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.nextLine();
sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
System.out.println("Enter an initiating amount:");
initiateAmount = sc.nextDouble();
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 2) {
System.out.println("Enter negative value to withdraw and positive to deposit");
depOrWith = sc.nextInt();
if (depOrWith < 0) {
initiateAmount = initiateAmount + depOrWith;
} else {
initiateAmount = initiateAmount + depOrWith;
}
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 3) {
System.out.println("Your name: " + name);
System.out.println("Your account number: " + accNum);
System.out.println("Your current balance: " + initiateAmount);
System.out.println("\n-----------\n"
+ "------------");
} else if (userChoose == 6) {
System.exit(0);
}
}
}
After selecting the option and pressing Enter, the Scanner is not reading the newline. Afterwards when name = sc.nextLine(); is called it will only read the new line following the selected option and name will be assigned the empty string. To solve this, simply add a call nextLine after reading the selected option, and remove the duplicate nextLine when reading the name:
while (true) {
userChoose = sc.nextInt();
sc.nextLine();
if (userChoose == 1) {
System.out.println("Enter your full name:");
name = sc.nextLine();
System.out.println("Choose an account number:");
...
Interchange the Blank nextLine() and the assigning one.
System.out.println("Enter your full name:");
sc.nextLine();
name = sc.nextLine();
System.out.println("Choose an account number:");
accNum = sc.nextInt();
name is null. If the user selects three you haven't actually assigned a value to the name variable yet, that only happens if they choose 1
When you select the option 3 you're not setting the name value which defaults to null, so it's printing Your name: null
You need to read and assign name value if you want something to be printed.
I have the following code:
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//System.out.println("Enter quantity:");
//int quantity = input.nextInt();
//System.out.println("You entered: " + quantity);
//System.out.println("Enter price: ");
//double price = input.nextDouble();
//System.out.println("You entered: " + price);
System.out.println("Enter city: ");
String city = input.nextLine();
System.out.println("You entered: " + city);
System.out.println("Enter state code: ");
String state = input.next();
System.out.println("You entered: " + state);
}
}
When I run the program with the middle section commented out like this, it works correctly. But when I uncomment it, it messes up the last block by printing the following lines simultaneously:
Enter city:
You entered:
Enter state code:
Why is this happening, and how can I fix it?
You typed something like this:
12<enter>1.3<enter>AZ
right?
When you call nextInt, it reads the next integer. So it reads "12" and what is left is:
<enter>1.3<enter>AZ<enter>
Now you call nextDouble. It skips past the first <enter> and reads "1.3" (a double). What is left is:
<enter>AZ<enter>
Now you call nextLine, which reads until the next <enter>. Oh look, you already pressed <enter>! So it reads the <enter> and returns a blank line. What is left is:
AZ<enter>
Now you call nextLine again, which reads until the next <enter>. It reads AZ<enter> and returns "AZ".
This is a quirk of how Scanners and streams work. The usual fix is to call nextLine immediately after nextInt and nextDouble, and ignore the result. Something like:
System.out.println("Enter quantity: ");
int quantity = input.nextInt();
input.nextLine(); // ignore newline
System.out.println("You entered: " + quantity);
System.out.println("Enter price: ");
double price = input.nextDouble();
input.nextLine(); // ignore newline
System.out.println("You entered: " + price);
input.nextDouble();
does not consume the line, insert a line:input.nextLine();
right after the commented block, don't assign it to any variable.
use ScnObj.next() instead of ScnObj.nextLine();
System.out.println("Enter price: ");
double price = ScnObj.nextDouble();
System.out.println("You entered: " + price);
System.out.println("Enter city: ");
String city = ScnObj.next();
System.out.println("You entered: " + city);
System.out.println("Enter state code: ");
String state = ScnObj.next();
System.out.println("You entered: " + state);
Is there a way to use the same prompt yet store the users data again without creating a second variable? It all needs to displayed at the same time in a table like fashion (hence the tabs)
//Variables
Scanner user = new Scanner(System.in);
NumberFormat percent = NumberFormat.getPercentInstance();
int num = 1;
int group = 10, string = 12, formatting = 8;
while (num <= 3)
{
System.out.print("Please enter name of assignment 1: ");
String assignment = user.nextLine().toUpperCase();
System.out.print("Please enter points earned: ");
double earned = user.nextDouble();
System.out.print("Please enter total points possible: ");
double total = user.nextDouble();
}
System.out.print(assignment + "\t\t");
System.out.print(earned + "\t");
System.out.print(total + "\t\t");
System.out.print(percent.format(earned / total) + "\n\t");
when looping it is skipping the "Please enter name of assignment prompt:
Please enter name of assignment 2: Please enter points earned:
I expected the output to be:
Please enter name of assignment 1: Practice Exam
Please enter points earned: 89
Please enter total points possible: 125
Please enter name of assignment 2: FooBar
Please enter points earned: 42
you're consuming the numbers but you're not consuming the newlines
try this
String assignment;
String exit=null;
double earned, total;
int i = 0;
Scanner scan = new Scanner(System.in);
while (!"Y".equalsIgnoreCase(exit)) {
i++;
System.out.print("Please enter name of assignment " + i + ": ");
assignment = scan.nextLine().toUpperCase(); //consumes string + \n
System.out.print("Please enter points earned: ");
earned = scan.nextDouble();//consumes double
scan.nextLine();//consumes \n
System.out.print("Please enter total points possible: ");
total = scan.nextDouble();
scan.nextLine();
System.out.print("Assignment\t");
System.out.print("Score\t");
System.out.print("Total Points\t");
System.out.print(assignment + "\t\t");
System.out.print(earned + "\t");
System.out.println(total + "\t\t");
System.out.print("Stop?(Y/N)");
exit = scan.nextLine();
}
returns
Please enter name of assignment 1: 1
Please enter points earned: 1
Please enter total points possible: 1
Assignment Score Total Points 1 1.0 1.0
Stop?(Y/N)n
Please enter name of assignment 2: 2
Please enter points earned: 2
Please enter total points possible: 2
Assignment Score Total Points 2 2.0 2.0
Stop?(Y/N)y