I've used the Scanner var = input.nextLine();
It works for my name variable but not address?
Here is my code:
Scanner input = new Scanner(System.in);
System.out.println("What is your name?");
String name = input.nextLine();
System.out.println("How old are you?");
int age = input.nextInt();
System.out.println("Where do you live?");
String address = input.nextLine();
System.out.println("Your name is " + name + ".\n You are " + age + " years old." + "\n You live at " + address + ".");
And here's what it's displaying:
What is your name?
Yassine assim
How old are you?
17
Where do you live?
Your name is Yassine assim.
You are 17 years old.
You live at .
int age = input.nextInt(); consumes the int you typed but that's it, you surely return to line after if BUT this char (return line) has not been consumed
And so input.nextLine(); of adress will do it directly
2 options :
int age = Integer.parseInt(input.nextLine()); take all line and convert to int
int age = input.nextInt(); input.nextLine(); take int and use, then consume return like
Related
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
I need get a number from prompt, and juste after I need get a String list from prompt. I have a problem. Is it OK if the 1st question ask a string with nextLine() see this post.
Java code:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number:");
int num = input.nextInt();
System.out.println("Enter a name list:");
String nameList = input.nextLine();
System.out.println("Enter last name:");
String lastName = input.nextLine();
input.close();
System.out.println(num + " * " + nameList + " ** " + lastName);
}
console result:
Enter a number:
2
Enter a name list:
Enter last name:
1st response is 2 + enter
but juste after 2 + enter, the program display Enter a name list:
Enter last name:
I would use input.next() instead of input.nextLine() as next blocks for user input while nextLine moves the scanner past the current line and it buffers all the inputs until it finds a line separator.
or use nextLine() after nextInt to consume the linefeed which is left by nextInt
Solution add input.nextLine(); juste after int num = input.nextInt();.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number:");
int num = input.nextInt();
input.nextLine();
System.out.println("Enter a name list:");
String nameList = input.nextLine();
System.out.println("Enter last name:");
String lastName = input.nextLine();
input.close();
System.out.println(num + " * " + nameList + " ** " + lastName);
}
console:
Enter a number:
2
Enter a name list:
aa bb cc
Enter last name:
dd
2 * aa bb cc ** dd
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 am VERY new to Java and I'm trying to make a madlib that takes user input and inserts it into a sentence. So far, I'm not having much luck. When I run the program, it prompts me for the first input, but after that it errors out. Do you know why? I think it has something to do with data types (int, string, etc).
Bonus question: How would I make the inputs appear bold once inserted into the paragraph? Assuming I can get this code to work in the first place.
Here is my code:
import java.util.Scanner;
public class Madlib
{
public static void main(String[] args)
{
int firstName, childAge, colorToy, typeToy;
Scanner input = new Scanner(System.in);
System.out.println("Enter someone's first name:");
firstName = input.nextInt();
System.out.println("Enter a child's age:");
childAge = input.nextInt();
System.out.println("Enter a color:");
colorToy = input.nextInt();
System.out.println("Enter a toy:");
typeToy = input.nextInt();
System.out.println("\"I am" + childAge + "years old, so that means I get to have the" + colorToy + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + firstName + "grovelled, barely peering over their large, Sunday newspaper.");
}
}
You have this error because
firstName, childAge, colorToy, typeToy
should all not supposed to be an integer. They are supposed to be String You should be doing
firstName = input.nextLine();
childAge = input.nextLine();
colorToy = input.nextLine();
typeToy = input.nextLine();
Also, this is not part of your question; however, when you do
System.out.println("\"I am " + childAge + " years old, so that means I get to have the " + colorToy + " " + typeToy + "!\" exclaimed the little girl.");
System.out.println("\"Share with your sister,\"" + " " + firstName + " grovelled, barely peering over their large, Sunday newspaper.");
It should look like that with spaces included in places that you didn't have them in. I hope this answered your question!
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");
}