variable.nextInt() not working in Java program [duplicate] - java

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed last year.
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
**System.out.println("Please enter the name of the second subject");
String B = input.nextLine();
System.out.println("Please enter the marks of the subject "+ B );**
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.nextLine();
In this code I expect that program to ask the name of second subject after taking in the value for first subjet marks but instead its printing both the print statement for second subject and ignoring to take the second subject name.

If the name of the subject is only one word you should use input.next() instead of input.nextLine()
Try this:
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.next();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
float b = input.nextFloat();
System.out.println("Please enter the name of the last subject");
String C = input.next();

When you use Scanner functions that read tokens (e.g.: next(), nextInt(), nextFloat() etc.), the Scanner reads and returns the next token. When you read an entire line (i.e.: readLine()), it reads from the current position until the beginning of the next line.
use this
Scanner input = new Scanner(System.in);
System.out.println("Please enter the name of the first subject");
String A = input.nextLine();
System.out.println("Please enter marks in subject " + A );
float a = input.nextFloat();
System.out.println("Please enter the name of the second subject");
String B = input.next();
System.out.println("Please enter the marks of the subject "+ B );
input.nextLine();
float b = input.nextFloat();
input.nextLine();
System.out.println("Please enter the name of the last subject");
String C = input.next();

Related

Two print statements, but only one displays a user input string [duplicate]

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

nextLine printing out 2 at a time? [duplicate]

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

how do I make the program take the input before displaying results

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");
}

Begginner Java: nextLine Dilema

I've seen a couple of threads that are similar to this question, but had trouble finding the solution to my specific question.
The code I am writing is for a teacher to input a student's name and grades and receive an output of the final letter grade.
The area I am having trouble with is the last name input gets grouped together with the first name input, instead of asking the question by itself. How do I prompt these to appear as separate questions?
import java.util.*;
public class AssignmentTest {
public static void main (String [] args) {
Scanner console = new Scanner (System.in);
int iStudentID = 0; //StudentID
String sLastName = ""; //Last Name
String sFirstName = ""; //First Name
int iAssignmentsScore = 0; //Assignment Input
int iQuizzesScore = 0; //Quizzes Input
int iMidtermsScore = 0; //Midterm Input
int iFinalScore = 0;; //Final Input
//USER INPUT
//StudentID Input
System.out.println("Please enter the StudentID: ");
iStudentID = console.nextInt() ;
//Last Name Input
System.out.println ("Please enter the student's last name: ");
sLastName = console.nextLine() ;
//First Name Input
System.out.println ("Please enter the student's first name: ");
sFirstName = console.nextLine() ;
//Assignment Input
System.out.println ("Please enter the student's assignment score: ");
iAssignmentsScore = console.nextInt() ;
//Quiz Input
System.out.println ("Please enter the student's quiz score: ");
iQuizzesScore = console.nextInt ();
//Midterm Input
System.out.println ("Please enter the student's midterm score: ");
iMidtermsScore = console.nextInt ();
//Final Input
System.out.println ("Please enter the student's final score: ");
iFinalScore = console.nextInt ();
}
}
Since sFirstName and lFirstName , each consist of a word you should use
next()
Finds and returns the next complete token from this scanner.
Instead of
nextLine()
Advances this scanner past the current line and returns the input that
was skipped.
#madprogrammer has better explanation:
Actually, the issue is after iStudentID = console.nextInt() ; returns,
there is still a new line character in the buffer, so that when
sLastName = console.nextLine() ; is called, it skips of this
automatically (returning an empty String) - this is a very common
mistake
try this
System.out.println ("Please enter the student's last name: ");
sLastName = console.nextLine() ;
console.nextLine();
I think this may be as simple as adding another new line character \n. For example
System.out.println ("\nPlease enter the student's first name: ");
you should use next() instead of nextLine()

Java skipping a line of code? VERY basic java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
If you look at the line of code where it says
System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();
It completely skips the firstname part and goes straight to surname? Any ideas why this is happenimh?
import java.util.*;
'class university{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
Person2 mPerson, fPerson;
String fFirstName, fSurname, mFirstName, mSurname;
int fAge, mAge;
System.out.println("Please enter the firstname of your favourite female author");
fFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
fSurname = scanner.nextLine();
System.out.println("Please enter her age");
fAge = scanner.nextInt();
System.out.println("Please enter the firstname of your favourite female author");
mFirstName = scanner.nextLine();
System.out.println("Please enter her second name");
mSurname = scanner.nextLine();
System.out.println("Please enter her age");
mAge = scanner.nextInt();
System.out.print(fPerson);
}
}
Add a nextLine() call after you call nextInt(). Because nextInt() doesn't finish the line. So the next call to nextLine() will return an empty string.
fAge = scanner.nextInt(); does not consume the line ending.
add scanner.nextLine() after that to absorb the end-of-line character and it will work.

Categories