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.
Related
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();
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
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
System.out.print("Enter name:");
name=sc.nextLine();
System.out.print("Enter nric: ");
nric=sc.nextLine();
System.out.print("Enter age: ");
age=sc.nextInt();
System.out.print("Enter gender(male/female): ");
gender=sc.nextLine();
System.out.print("Enter weight:");
weight=sc.nextInt();
System.out.print("Enter height: ");
height=sc.nextDouble();
Well, every time after I enter age, I cannot enter gender. I would really appreciate it if you could help me, thanks.
because Scanner#nextInt method does not consume the last newline character of your input, and thus that newline is consumed in the next call to Scanner#nextLine
so for your program you have to put another nextLine for consuming the nextLine
it will perfect when adding the sc.nextLine after enter age
System.out.print("Enter name:");
name=sc.nextLine();
System.out.print("Enter nric: ");
nric=sc.nextLine();
System.out.print("Enter age: ");
age=sc.nextInt();
sc.nextLine();// for consuming the nextLine
System.out.print("Enter gender(male/female): ");
gender=sc.nextLine();
System.out.print("Enter weight:");
weight=sc.nextInt();
System.out.print("Enter height: ");
height=sc.nextDouble();
nextInt() will stop after reading the int value, it wont go to new line to read values further.
you have to add a nextLine() after nextInt()
or
You can read age as String using nextLine() and later parse it as int using a wrapper class.
Simply use this before taking input of a line of a string.
sc= new Scanner(System.in);
Your code:
System.out.print("Enter name:");
name=sc.nextLine();
System.out.print("Enter nric: ");
sc= new Scanner(System.in);
nric=sc.nextLine();
System.out.print("Enter age: ");
age=sc.nextInt();
System.out.print("Enter gender(male/female): ");
sc= new Scanner(System.in);
gender=sc.nextLine();
System.out.print("Enter weight:");
weight=sc.nextInt();
System.out.print("Enter height: ");
height=sc.nextDouble();
When you invoke nextLine(), the line is removed from the buffer and effectively discarded from the Scanner.
So I am doing a project to put values into a scanner object and retrieve them. I am having a problem with the first input. The user is asked to input an ID and then should be asked for the last name.The problem is that when I run the first question it displays it but skips the first scanner input and jumps to the second question and input returns to normal. So what is happening here? And how do I solve this?
//Prompt user for each value
System.out.println("Enter employee Id number:");
String inputId = scanner.nextLine();
System.out.println("Enter employees last name:");
String inputLastName = scanner.nextLine();
System.out.println("Enter employees first name:");
String inputFirstName = scanner.nextLine();
System.out.println("Enter employees salary:");
String inputSalary = scanner.nextLine();
Double inSalary = Double.parseDouble(inputSalary);
i have same problem with you, and i change .nextLine() with .next()
and that is how i fix my problem
try this
System.out.println("Enter employee Id number:");
String inputId = scanner.next();
System.out.println("Enter employees last name:");
String inputLastName = scanner.next();
System.out.println("Enter employees first name:");
String inputFirstName = scanner.next();
System.out.println("Enter employees salary:");
String inputSalary = scanner.next();
Double inSalary = Double.parseDouble(inputSalary);
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()