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);
Related
The Following code is a simple java program where I am just getting an input of a student details but the program doesn't do anything it just stays like this no compilation error or does not take any input
import java.util.Scanner;
public class Scannerexample {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int Rollno = scan.nextInt();
String firstname = scan.nextLine();
String lastname = scan.nextLine();
String Department = scan.nextLine();
Boolean Result = scan.hasNext();
char gender = scan.next().charAt(0);
System.out.println("Enter Rollno "+ Rollno);
System.out.println("Enter Firstname "+ firstname);
System.out.println("Enter Lastname "+ lastname);
System.out.println("Enter Department "+ Department);
System.out.println("Enter Result "+ Result);
System.out.println("Enter Gender "+ gender);
scan.close();
}
}
Each call of scan.nextSomething waits for input. So the way you have written the program you wait for inputting of all the fields and then print them. Try typing some fields and press enter to see the result in the end.
What you probably wanted is something like:
System.out.println("Enter rollno: ");
int rollno = scan.nextInt();
System.out.println("You entered rollno: "+ rollno);
Also there are some java conventions which are nice and make the code readable :) For example variables start with lowercase and classes are in CamelCase
So what your Code does is every time you call
scan.nextLine();
The Program waits for your Input, until you hit 'Enter'.
So if you want to write something before the Input, you have to do it like this:
System.out.print("Enter Rollno: ");
int rollno = scan.nextInt();
System.out.print("Enter Firstname: ");
String firstname = scan.next();
// Any more.
Keep an eye that you only use 'print()' instead of 'println()' so that your input is on the same Line.
Also use scan.next(), to only grab the input, and not the complete line in terminal.
be carefull with scan.next() or scan.nextLine()
System.out.print("Enter Rollno: ");
int rollno = scan.nextInt();
System.out.print("Enter Firstname: ");
String firstname = scan.next();
System.out.println(firstname);
// Any more.
System.out.print("Enter Lastname: ");
String lastname = scan.next();
System.out.println(lastname);
in this case, if you input Firstname as: John Doe, it will not let you to input Lastname, because it will read next string in line.
scan.next() read strings up to "space", then another scan.next() will read next strings up to next "space".
Here is my code
Scanner keyboard = new Scanner(System.in);
System.out.print("Last name: ");
lastName = keyboard.nextLine();
System.out.print("First name: ");
firstName = keyboard.nextLine();
System.out.print("Email address: ");
emailAddress = keyboard.nextLine();
System.out.print("Username: ");
username = keyboard.nextLine();
and it outputs this
Last name: First name:
Basically it skips letting me enter lastName and goes straight to the prompt for firstName.
However, if I use keyboard.next() instead of keyboard.nextLine(), it works fine. Any ideas why?
Let me guess -- you've got code not shown that uses the Scanner above the attempt to get lastName. In that attempt, you're not handling the end of line token, and so it's left dangling, only to be swallowed by the call to nextLine() where you attempt to get lastName.
For example, if you have this:
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = keyboard.nextInt(); // dangling EOL token here
System.out.print("Last name: ");
lastName = keyboard.nextLine();
You're going to have problems.
One solution, whenever you leave the EOL token dangling, swallow it by calling keyboard.nextLine().
e.g.,
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = keyboard.nextInt();
keyboard.nextLine(); // **** add this to swallow EOL token
System.out.print("Last name: ");
lastName = keyboard.nextLine();
I have a question. I know you can prompt a user multiple times with scanner as so
public static void main(String[] args) {
String First;
String Last;
int Age;
Scanner input = new Scanner(System.in);
System.out.print("What is the First name of person?");
First = input.next();
System.out.print("What is the Last name of person?");
Last = input.next();
System.out.print("What is the Age of person?");
Age = input.next();
}
But is there a way there to prompt all in one line?
For example I want to enter
Console-What is the First, Last, and Age of the person?
User- First Last Age
First, Java variables start with a lower case letter by convention (yours look like class names). Second, this
Age = input.next();
gives you a compiler error. Because Age is an int. You can certainly split the single line as others have suggested, but you can also construct a Scanner(String) and use it with something like
Scanner input = new Scanner(System.in);
System.out.println("Please enter the first name last name and age of the person: ");
System.out.println("(first last age)");
String line = input.nextLine();
Scanner scan = new Scanner(line);
String first = scan.next();
String last = scan.next();
int age = scan.nextInt();
System.out.printf("Person: %s, %s (%d)%n", last, first, age);
Grab a line and split the string:
Scanner input = new Scanner(System.in);
System.out.print("What is the First, Last, and Age of the person?");
String line = input.nextLine();
String[] parts = line.split(" ");
if(parts.length < 3){
//error, ask again
}
else{
String first = parts[0];
String last = parts[1];
String age = parts[2];
}
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.
import java.util.Scanner;
class MyClass
{
public static void main(String args[])
{
Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
System.out.println("Enter employee name:");
name = scanner.next();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();
}
}
I got this exception while trying to enter a first name and last name.
Enter employee ID:
101
Enter employee name:
firstname lastname
Enter supervisor ID:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at com.controller.Menu.<init>(Menu.java:61)
at com.tests.Employeetest.main(Employeetest.java:17)
but its working on if I only enter the first name.
Enter employee ID:
105
Enter employee name:
name
Enter supervisor ID:
501
What I want is to read the full string whether it is given as name or as firstname lastname. What's the problem here?
Scanner scanner = new Scanner(System.in);
int employeeId, supervisorId;
String name;
System.out.println("Enter employee ID:");
employeeId = scanner.nextInt();
scanner.nextLine(); //This is needed to pick up the new line
System.out.println("Enter employee name:");
name = scanner.nextLine();
System.out.println("Enter supervisor ID:");
supervisorId = scanner.nextInt();
Calling nextInt() was a problem as it didn't pick up the new line (when you hit enter). So, calling scanner.nextLine() after that does the work.
What you can do is use delimeter as new line. Till you press enter key you will be able to read it as string.
Scanner sc = new Scanner(System.in);
sc.useDelimiter(System.getProperty("line.separator"));
Hope this helps.
Replace:
System.out.println("Enter EmployeeName:");
ename=(scanner.next());
with:
System.out.println("Enter EmployeeName:");
ename=(scanner.nextLine());
This is because next() grabs only the next token, and the space acts as a delimiter between the tokens. By this, I mean that the scanner reads the input: "firstname lastname" as two separate tokens. So in your example, ename would be set to firstname and the scanner is attempting to set the supervisorId to lastname
You are entering a null value to nextInt, it will fail if you give a null value...
i have added a null check to the piece of code
Try this code:
import java.util.Scanner;
class MyClass
{
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
int eid,sid;
String ename;
System.out.println("Enter Employeeid:");
eid=(scanner.nextInt());
System.out.println("Enter EmployeeName:");
ename=(scanner.next());
System.out.println("Enter SupervisiorId:");
if(scanner.nextLine()!=null&&scanner.nextLine()!=""){//null check
sid=scanner.nextInt();
}//null check
}
}