this code actually inside if statements (to decide if it a lecturer or student, but the problem are same for both, the input name got skipped)
Lecturer a = new Lecturer();
System.out.print("Please enter your Lecturer Identification Number, Sir : ");
input.nextLine();
a.setLect_num(input.nextLine());
System.out.print("\nPlease insert your Name, Sir : ");
a.setName(input.nextLine());
System.out.print("\nPlease enter your Age, Sir : ");
a.setAge(input.nextInt());
System.out.print("\nPlese enter the course you are in charge, Sir : ");
input.nextLine();
a.setCourse(input.nextLine());
a.setOccupation("Teacher");
a.introduction();
In line 2 of your code snippet, you are collecting input from the scanner and discarding it:
System.out.print("Please enter your Lecturer Identification Number, Sir : ");
input.nextLine(); // this line is gathering input and discarding it
a.setLect_num(input.nextLine());
Related
So I have my code compile but when I run it it combines my lines
System.out.print("Enter ID Number");
double ID=s.nextDouble();
if(ID >9999|| ID<0)
ID=0;
System.out.print("Enter Make of Vehicle");
String make=s.nextLine();
System.out.print("Enter Model of Vehicle");
String model=s.nextLine();
System.out.print("Enter Color of Vehicle");
String color=s.nextLine();
System.out.print("Enter Year");
double year=s.nextDouble();
if(year <2000 || year>2017)
year=0;
When I run the program it combines the lines to say
enter make of vehicle enter model of vehicle
How do I stop this as they are supposed to be separate entries
Use System.out.println instead of System.out.print
Put s.nextLine() after reading double. The problem is that s.nextDouble(); does not consume new line character, so s.nextLine(); after System.out.println("Enter Make of Vehicle"); is called automatically, you don't see anything more because there is only this one new line character (and it is being consumed).
double ID=s.nextDouble();
s.nextLine();
if(ID >9999|| ID<0)
ID=0;
Second thing is to replace System.out.print with System.out.println to print output in new line.
Call s.nextLine() after double ID=s.nextDouble(), couse nextDouble() doesn't consume the \n
I'm trying to get the user to enter two strings and store them in two variables called studentName and studentNum, but when I run the program, the first string input is skipped over and only the second string input is allowed to be entered.
It prints out normally, but won't allow me to enter a value for studentName. could anyone help me with this problem?
System.out.print("Please enter your name: ");
studentName = in.nextLine();
System.out.print("\nPlease enter your student number: ");
studentNum = in.nextLine();
Has to do with where the scanner starts and where it puts up a newline character
You can fix this if you change System.out.print to System.out.println
Here's my code - The objective is to enter some basic info (age, name, gender) for x number of patients.
public static void main(String[] args) {
int numPatients = 2;
int[] age = new int[numPatients];
String[] gender = new String[numPatients];
String[] name = new String[numPatients];
Scanner in = new Scanner(System.in);
/*
* Obtaining patients details: name, gender, age
* First create a Scanner input variable to read the data
*/
for (int i = 0; i < numPatients; i++)
{
System.out.println("Enter name of patient " + (i+1));
name[i] = in.nextLine();
System.out.println("Enter gender (male/female) of patient " + (i+1));
gender[i] = in.nextLine();
System.out.println("Enter age of patient " + (i+1));
age[i] = in.nextInt();
}
The issue I'm having is when the loop goes to the 2nd variable, i.e. I'm not able to enter a value for the name of the patient. It seems to skip taking the input there and goes directly to the next input, which is gender.
Enter name of patient 1
Mark
Enter gender (male/female) of patient 1
Male
Enter age of patient 1
34
Enter name of patient 2 //Skipped. Could not enter input here
Enter gender (male/female) of patient 2
Jenna
Any idea why that happens? Is it better to use BufferedReader instead?
If you must use Scanner, then always use nextLine(). The problem is that nextInt() reads only the integer part of the input and stops before it reads the Enter keypress. Then the next call to nextLine() sees the Enter keypress in the buffer and things you entered an empty name.
So you can do something like:
age[i] = Integer.parseInt(in.nextLine());
and be prepared to handle the exception that will happen if the user types something other than a number.
If you are assured that name will be a single word (not an issue for male or female)then you can modify the scanner input to just get the string;
in.next();
this works fine (only if name's a single word ).
We all know that using method input.nextLine() will allow spaces for the string while running the program, but.. When I use this method inside a loop the run skips the statement. Can anyone please explain why?
I'm trying it with menu:
Code:
do {
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch (choice) {
case 4:
System.out.println("Please give the full information of the project ");
System.out.print("Project code: "); int code = input.nextInt();
System.out.print("Project Title: "); String title = input.nextLine();
System.out.print("Project Bonus in Percent: "); double bip = input.nextDouble();
Project proj4 = new Project(code4,title4,bip4);
System.out.print("Employee ID you're adding project to: "); String id4=input.next();
ers.addProjectToEmployee(id4,proj4);
break;
/* more cases */
}
} while (choice !=13);
Check statement 3 in case 4.
Here's what happened while running the program:
Enter your choice: 4
Please give the full information about the project
Project code: 66677
Project Title: Project Bonus in Percent:
input.nextInt() does not read the end-of-line, so that' why the effect that .nextLine() is being escaped, while actually that .nextLine() is reading the end-of-line left unread in the input-stream by the .nextInt(). You need an extra .nextLine() after .nextInt().
Update:
Do the following:
System.out.print("Project code: "); int code = input.nextInt();
input.nextLine(); // this is what you need to add
System.out.print("Project Title: "); String title = input.nextLine();
This question already exists:
Scanner issue when using nextLine after nextXXX [duplicate]
Closed 9 years ago.
Here is the skeleton of some basic code I am writing to make a simple game:
Scanner in = new Scanner(System.in);
String name;
String playing;
int age;
do {
System.out.println("Enter your name");
name = in.nextLine();
System.out.println("Enter your age");
age = in.nextInt();
System.out.println("Play again?");
playing = in.nextLine();
} while (true);
The code does not work as expected, for example, here is the expected functioning of the code:
Enter your name
John
Enter your age
20
Play again?
Yes
Enter your name
Bill
...
However, there is an issue with reading the Play again line, this is the actual output:
Enter your name
John
Enter your age
20
Play again?
Enter your name
As you can see "Enter your name" is being displayed again before "Play again?" is able to accept input. When debugging the playing variable is set to "", so there is no input that I can see and I cannot figure out what is being consumed.
Any help would be appreciated, thanks!
nextInt() doesn't consume the end-of-line, even if the int is the only thing in there.
Add another nextLine() after reading the int, and either discard its value completely, or check that it is empty if you want to prevent people from entering anything but an int.
The problem is that after calling nextInt() there is still a '\n' in the buffer and so that is what is passed. use in.next() instead of in.nextLine().
Use next() method in Scanner class instead of nextLine() method.
Scanner in = new Scanner(System.in);
String name;
String playing;
int age;
do {
System.out.println("Enter your name");
name = in.next();
System.out.println("Enter your age");
age = in.nextInt();
System.out.println("Play again?");
playing = in.next();
} while (true);
for more information refer Java Documentation for Scanner class
Output :
Enter your name
Kanishka
Enter your age
23
Play again?
Yes
Enter your name
....
You should use the following code for the next line reading.
Scanner scanner = new Scanner(System.in).useDelimiter("\n");
And for reading the line you should write
scanner.next();