This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 9 years ago.
The below given program is for reading inputs using scanner. The problem i am facing here is if i try to give an integer value first then it wont allow me to give string value next. it skips automatically to the next line. Any help will be appreciated
import java.io.*;
import java.util.Scanner;
class Employee
{
int empid,salary;
String empname,designation;
public static void main(String args[])
{
Employee bha=new Employee();
bha.details();
bha.display();
}
void details()
{
Scanner s=new Scanner(System.in);
System.out.println("enter the empid");
empid=s.nextInt();//i'm am able to give the value here
System.out.println("enter the employee name");
empname=s.nextLine();// automatically skips to the next line(unable to give value)
System.out.println("enter the employee designation");
designation=s.nextLine();
System.out.println("enter the employee salary");
salary=s.nextInt();
}
void display()
{
System.out.println("the employee id is"+empid);
System.out.println("the employee name is"+empname);
System.out.println("the employee designation
is"+designation);
System.out.println("the employee salary is"+salary);
}
}
Try this way:
System.out.println("enter the empid");
Scanner s = new Scanner(System.in);
empid = Integer.parseInt(s.next());
System.out.println("enter the employee name");
empname = s.next();
System.out.println("enter the employee designation");
designation = s.next();
System.out.println("enter the employee salary");
salary = Integer.parseInt(s.next());
Related
I am writing a small program to create a password based off what the user inputs their first middle last name and the birthday and create a password off that and i don't know what i am doing but believe i need multiple scanners but when i do i get this error "variable scan is already defined in method main(string[]) Scanner scan = new Scanner(System.in);"
here is my code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.print("Please Enter your first name: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
System.out.print("Please Enter your last name: ");
Scanner scan = new Scanner(System.in);
String name = scan.nextLine();
scan.close();
}
}
anythoughs on what could work?
You do not need multiple Scanner objects to accept multiple inputs. You should create one Scanner object and use it to collect as many inputs as you want.
Here is an example:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter your first name: ");
String fname = input.nextLine();
System.out.println("Enter your last name: ");
String lname = input.nextLine();
System.out.println("Enter your age: ");
int age = Integer.parseInt(input.nextLine());
System.out.println("Your details as entered:");
System.out.println("First Name: " + fname);
System.out.println("Last Name: " + lname);
System.out.println("Age: " + age);
}
}
Also some extra resources for you (for Scanner class): https://www.w3schools.com/java/java_user_input.asp
Edit:
replaced next() with nextLine()
replaced nextInt() with Integer.parseInt(input.nextLine());
Thank you very much for the information #Arvind Kumar Avinash. Reference thread: Scanner is skipping nextLine() after using next() or nextFoo()?.
That is because you cannot have two variables with the same name within the same scope.
You should rename one of them ..
Note that you did the same with the String variable name ;)
If you want to keep the same name for both use, then you should not re-declare the variable(s):
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan;
String name;
System.out.print("Please Enter your first name: ");
scan = new Scanner(System.in);
name = scan.nextLine();
scan.close();
System.out.print("Please Enter your last name: ");
scan = new Scanner(System.in);
name = scan.nextLine();
scan.close();
}
}
Notes:
in this example, you will loosen the first value entered by the user. You might want to use an other variable name to store the last name.
there is obviously no reason to instantiate two different Scanner objects with the same InputStream.
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
I have added a for loop in size of 3 it should scan for 3 times but it is scanning for 4 time.for size of 4 its scanning for 6 times help me out.
public class Control {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<Student> studs = new ArrayList<Student>();
for (int i = 1;i < 3;i++) { //Here the for loop
System.out.println("Enter age and name :");
Student stu1 = new Student(sc.nextInt(),sc.nextLine());
System.out.println("Enter age and name :");
Student stu2 = new Student(sc.nextInt(),sc.nextLine());
studs.add(stu1);
studs.add(stu2);
Collections.sort(studs,new StudAge());
}
for(Student stud : studs) {
System.out.println(stud);
}
}
}
import java.awt.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Student> studs = new ArrayList<Student>();
for (int i = 1;i < 3;i++) { //Here the for loop
System.out.println("Enter age and name :");
int age = sc.nextInt();
//nextInt does not account for the enter that you press when you submit the int so you have to catch the unused enter keystroke
sc.nextLine();
String name = sc.nextLine();
Student stu1 = new Student(age, name);
age = sc.nextInt();
sc.nextLine();
name = sc.nextLine();
System.out.println("Enter age and name :");
Student stu2 = new Student(age, name);
studs.add(stu1);
studs.add(stu2);
Collections.sort(studs,new StudAge());
}
for(Student stud : studs) {
System.out.println(stud);
}
}
}
nextInt does not account for the enter that you press when you submit the int so you have to catch the unused enter keystroke
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
Here i am trying to Enter and Print some details of employe and everything looks fine but i am getting an Exception why?
import java.util.Scanner;
class EmpDet
{ //here details
int age;
String name;
int ssn;
public EmpDet(int age,String name,int ssn)
{
//assign to constructor
this.age = age;
this. name = name;
this.ssn = ssn;
System.out.println(age+" "+name+" "+ssn); //printing details
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
String name = sc.nextLine(); //InputMismatchException in this line
int ssn = sc.nextInt();
EmpDet det = new EmpDet(age,name,ssn);
}
}
You might want to grab line by line instead. Once you have the line, then you can try to parse the input into an Integer.
Scanner sc = new Scanner(System.in);
int age = Integer.parseInt(sc.nextLine());
String name = sc.nextLine();
int ssn = Integer.parseInt(sc.nextLine());
EmpDet det = new EmpDet(age,name,ssn);
It would be also wise to put a try catch around the parsing in case the input from the user doesn't qualify as being an Integer.
Note, nextInt() only grabs the number and not the new line (enter)
I want to know how I can add an object with user input in an array list. I wrote a method called createEmployee(), but I want to know how to do it in a list. So I tried it with employees.add(new Employee()), but there I have to write my own input. However, I want the user to write his own input. I want something like this: employees.add(new Employee(identity, empname, empsalary)), but it won't work. So how can I add it with my own input and without the method createEmployee() being static?
I tried the following:
public class Main extends ReusaxCorp {
public Main(String ID, String name, double grosssalary) {
super(ID, name, grosssalary);
}
public static void main(String[] args){
createEmployee();
ArrayList<Employee> employees = new ArrayList<Employee>();
employees.add(new Employee());
}
public static Employee createEmployee(){
Scanner input = new Scanner (System.in);
System.out.print("Please enter the ID of the employee: ");
String identity = input.nextLine();
System.out.print("Please enter the name of the employee: ");
String empname = input.nextLine();
System.out.print("Please enter the gross salary of the employee: ");
double empsalary = input.nextDouble();
Employee Employee = new Employee(identity, empname, empsalary);
return Employee;
}
}
public class Main extends ReusaxCorp {
public Main(String ID, String name, double grosssalary) {
super(ID, name, grosssalary);
}
public static void main(String[] args){
ArrayList<Employee> employees = new ArrayList<Employee>();
Employee e = createEmployee();
employees.add(e);
}
public static Employee createEmployee(){
Scanner input = new Scanner (System.in);
System.out.print("Please enter the ID of the employee: ");
String identity = input.nextLine();
System.out.print("Please enter the name of the employee: ");
String empname = input.nextLine();
System.out.print("Please enter the gross salary of the employee: ");
double empsalary = input.nextDouble();
Employee Employee = new Employee(identity, empname, empsalary);
return Employee;
}
}
you can use the constructor direct,do something like that in main method (a you mentioned you dont want to use create employee method )
java.util.Scanner scanner=new java.util.Scanner(System.in);
boolean addingEmployee=true;
while(addingEmployee)
{
System.out.println("Please enter the employee info ID,NAME,SALARY respectively ");
employees.add(new Employee(scanner.next(),scanner.next(),scanner.nextDouble()));
System.out.println("Employee has been added" );
System.out.println("do you want to add new employee : Y,N");
if(scanner.next().charAt(0)=='N') {addingEmployee=false;}
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 7 years ago.
I've been debugging the following program from hours yet I cannot find why is the sanner not taking the input for the field name
This is my source code:
import java.util.Scanner;
public class Student
{
int rollNo;
String name;
double percentageMarks;
static Scanner input = new Scanner(System.in);
public void accept()
{
System.out.print("Enter roll no: ");
rollNo = input.nextInt();
System.out.print("Enter Name: ");
name = input.nextLine();
System.out.print("Enter percentageMarks: ");
percentageMarks = input.nextDouble();
}
public void display()
{
System.out.println("Name: " +name);
System.out.println("roll no: " +rollNo);
System.out.println("Percentage marks: " +percentageMarks);
}
public static void main(String[] args)
{
Student s1 = new Student(), s2 = new Student();
s1.accept();
s2.accept();
if(s1.percentageMarks>s2.percentageMarks)
s1.display();
else if (s2.percentageMarks>s1.percentageMarks)
s2.display();
else
System.out.println("Both students has same marks");
}
}
This is an output sample:
Enter roll no: 1
Enter Name: Enter percentageMarks:
As seen, without it allowing to enter student name, its prompting to enter student percentage marks.
Any suggestion please?
This is because you first have the input.nextInt() and after it is finished it is not going to the next line. Just parse the whole first line as Integer.parseInt(scanner.nextLine()) and you will get your input for the name after that.