I'm working on a switch statement that takes inputs with Scanner, and when I run in NetBeans, one of the inputs takes an extra blank input. The extra input happens at variable dateAdmission when the user is asked to input "Enter date of admission".
case "patient":
{
System.out.println("Enter name: ");
String name = in.nextLine();
System.out.println("Enter Address: ");
String address = in.nextLine();
System.out.println("Enter date of birth: ");
String dob = in.nextLine();
System.out.println("Enter MCP number: ");
int mcp = in.nextInt();
in.nextLine();
System.out.println("Enter date of admission: ");
String dateAdmission = in.nextLine();
System.out.println();
String hospital = in.nextLine();
System.out.println("Enter name of doctor: ");
String doctor = in.nextLine();
System.out.println("Enter room number: ");
int roomNum = in.nextInt();
a[i] = new Patient(name, address, dob, mcp, dateAdmission, hospital, doctor, roomNum);
break;
}
You might have forgotten to provide System.out.println("Enter hospital");
before the
String hospital = in.nextLine();
What happened was it printed nothing and took the extra input for hospital?
Related
import java.util.Scanner;
public class MadLibs {
public static void main(String[] args) {
String name, place, college, profession, animal, petName;
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
name = keyboard.nextLine();
System.out.print("Enter a number: ");
number = keyboard.nextInt();
System.out.print("Enter a place: ");
place = keyboard.nextLine();
System.out.print("Enter a college: ");
college = keyboard.nextLine();
System.out.print("Enter a profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
petName = keyboard.nextLine();
keyboard.close();
}
}
console results
Can't figure out why "enter a college" and "enter a place" are printing on the same line and not acting as separate inputs.
Try below snippet it will definitely work :
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
String name = keyboard.nextLine();
System.out.print("Enter a number: ");
int number = keyboard.nextInt();
System.out.print("Enter a place: ");
keyboard.next();
String place = keyboard.nextLine();
System.out.print("Enter a college: ");
keyboard.next();
String college = keyboard.nextLine();
System.out.print("Enter a profession: ");
String profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
String animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
String petName = keyboard.nextLine();
}
The nextLine() method of java.util.Scanner class advances this scanner past the current line and returns the input that was skipped. This function prints the rest of the current line, leaving out the line separator at the end.
enter code here
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String name, place, college, profession, animal, petName;
int number;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a name: ");
name = keyboard.nextLine();
System.out.print("Enter a number: ");
number = keyboard.nextInt();
System.out.print("Enter a place: ");
place = keyboard.nextLine();
keyboard.nextLine(); //brings to next line
System.out.print("Enter a college: ");
college = keyboard.nextLine();
System.out.print("Enter a profession: ");
profession = keyboard.nextLine();
System.out.print("Enter a animal: ");
animal = keyboard.nextLine();
System.out.print("Enter a pet name: ");
petName = keyboard.nextLine();
keyboard.close();
}
}
Im still learning java and i want to understand more about array. Im still in school and we were asked to create an asean phonebook which can store, edit, delete, and view/search the information that are inputted. Im still doing the storing of information block of code and im struggling how to save multiple input in an array that is limited only to 100 contacts and can be searched later in the code. Can someone help me with my task and make me understand??
import java.util.Scanner;
public class Phonebook
{
public static void main (String [] args)
{
Scanner input = new Scanner (System.in);
int i = 0;
String studentNumber, surName, firstName, occuPation, countryCode, areaCode, numBer;
char genDer;
do
{
Menu();
System.out.println(" ");
System.out.print("Go to: ");
String choice = input.next();
if (choice.equals("1") || choice.equals("2") || choice.equals("3") || choice.equals("4") || choice.equals("5"))
{
i++;
switch(choice)
{
case "1":System.out.println("Enter student number: ");
studentNumber = input.next();
System.out.println("Enter surname: ");
surName = input.next();
System.out.println("Enter first name: ");
firstName = input.next();
System.out.println("Enter occupation: ");
occuPation = input.next();
System.out.println("Enter gender (M for male, F for female): ");
genDer = input.next();
System.out.println("Enter counter code: ");
countryCode = input.next();
System.out.println("Enter area code: ");
areaCode = input.next();
System.out.println("Enter number: ");
numBer = input.next();
break;
case "2":System.out.println("2");break;
case "3":System.out.println("3");break;
case "4":System.out.println("4");break;
case "5":System.out.println("5");break;
}
}
else
{
System.out.println("Invalid keyword, Please try again");
}
}
while (i < 1);
}
static void Menu()
{
System.out.println("[1] Store to ASEAN phonebook");
System.out.println("[2] Edit entry in ASEAN phonebook");
System.out.println("[3] Delete entry from ASEAN phonebook");
System.out.println("[4] View\\search ASEAN phonebook");
System.out.println("[5] Exit");
}
}
I am writing a simple code that takes number,name,surname from the user with scanner.
But when user enters name with spaces in it(two or more names) the code thinks string after the first space is surname.
I tried using input.nextLine(); in that case it skipped name completely and took only surname from the user.
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
int num = input.nextInt();
System.out.println("Enter Name");
String name = input.next();
System.out.println("Enter Surname");
String surname = input.next();
Try this:
Scanner input = new Scanner(System.in);
System.out.println("Enter number");
int num=input.nextInt();
input.nextLine(); // add this
System.out.println("Enter Name");
String name=input.nextLine();
System.out.println("Enter Surname");
String surname=input.nextLine();
System.out.println(num + " - " + name + " - " + surname);
Sample input/output:
Enter number
1
Enter Name
user name
Enter Surname
sur name
1 - user name - sur name
Use sc.nextLine() instead of sc.next() to read String with spaces
I am trying with user input to the values firstname, middlename, lastname, age for by using scanner class, but the below program only takes firstname, middlename, lastname and not the value of age.
public void inputEmployeeDetails(){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the firstname ");
firstname= scanner.nextLine();
System.out.println("Enter the middlename ");
middlename= scanner.nextLine();
System.out.println("Enter the lastname");
lastname= scanner.nextLine();
System.out.println("Enter the age");
age= scanner.nextInt();
}
I need to take all the values one after the other through the user prompt.
Based on the entered data I am displaying the employee details. Could someone let me know what I am missing.
I would like to also like to take multiple inputs from the user. Please let me know if I am right in the below
System.out.println("Do you like to fetch more records press "Yes" or "No");
String input=scanner.nextLine();
if(input="Yes")
inputEmployeeDetails();
The age variable takes the next ENTER key that you press, so nothing is stored. Since the datatype of ENTER is different than the data type of age variable, hence your code was getting InputMismatchException. You need to check if the next value is an int then assign it to variable. Please refer below once:
import java.util.Scanner;
public class Test{
public static void main(String []args){
String firstname, middlename, lastname;
int age;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the firstname ");
firstname= scanner.nextLine();
System.out.println("Enter the middlename ");
middlename= scanner.nextLine();
System.out.println("Enter the lastname");
lastname= scanner.nextLine();
System.out.println("Enter the age");
if(scanner.hasNextInt()) {
age= scanner.nextInt();
}else
age = 0;
System.out.println(firstname + " " + middlename + " " + lastname + " " + age);
}
}
Check this program:
String firstname,middlename,lastname,choice;
int age, flag;
Scanner scanner = new Scanner(System.in);
do{
choice=null;
System.out.println("Enter the firstname ");
firstname= scanner.nextLine();
System.out.println("Enter the middlename ");
middlename= scanner.nextLine();
System.out.println("Enter the lastname");
lastname= scanner.nextLine();
System.out.println("Enter the age");
flag=0;age=0;
while(flag==0){
try{
age= scanner.nextInt();
flag=1;
}
catch(Exception e){
scanner.nextLine();
System.out.println("Wrong entry, please enter digits only:");
}
}
System.out.println(firstname+" "+middlename+" "+lastname+" "+age);
System.out.println("Do you like to fetch more records press Yes or No");
scanner.nextLine();
choice=scanner.nextLine();
}while(choice.contains("Y")||choice.contains("y"));
System.out.println("Program terminated.");
scanner.close();
System.exit(0);
How do I read user inputs, one line at a time to create an object. Which then goes into a ArrayList? As you can see, when I enter the name, it's breaks it up and only stores the last name. Or if I do: first-middle-last name, it stores the middle and last name.
I enter the name the first time, which calls the search method first, to see if the name already exists in the ArrayList. That works fine. And if the name isn't in the list, the search returns null. Which then prompts the inputs and add method.
case 1: {
System.out.print("Enter the Students name: ");
String nameSearch = kbd.next();
Student stu = dc.search(nameSearch );
if (stu != null) {
System.out.println(stu);
}
else {
System.out.print("Enter name AGAIN: ");
String nameAdd= kbd.nextLine();
System.out.print("Enter grade (freshman, sophomore, junior, senior: ");
String categoryAdd = kbd.nextLine();
System.out.print("Major: ");
String majorAdd = kbd.nextLine();
System.out.print("Enter graduating year: ");
int yearAdd = kbd.nextInt();
System.out.print("Enter student ID: (xxxx.xxxx: ");
double idAdd= kbd.nextDouble();
dc.add(nameAdd, categoryAdd , majorAdd ,
yearAdd , idAdd);
}
break;
}
My input:
Enter the Students name: John Smith
Not Found(Search Method)
TEST
Enter name AGAIN: TEST2
Enter grade (freshman, sophomore, junior, senior: senior
Major: Computer Science
Enter graduating year: 2013
Enter student ID: 1234.4567
How it's stored inside the object when I print ArrayList:
Name: Smith
Grade: senior
Major: Computer Science
Graduating Year: 2013
Student ID: 1234.4567
The add method:
public Student add(String name, String grade,
String major, int year, double id) {
Student newStu = new Student(addName, addGrade, addMajor, addYear, addId);
studentList.add(newStu );
System.out.println("Added!");
return null;
}
This line:
String nameSearch = kbd.next();
Should probably be
String nameSearch = kbd.nextLine();
right?
Make kbd a BufferedReader (e.g. BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));), then do this...
case 1: {
System.out.print("Enter the Students name: ");
String nameSearch = kbd.readLine();
Student stu = dc.search(nameSearch );
if (stu != null) {
System.out.println(stu);
}
else {
System.out.print("Enter name AGAIN: ");
String nameAdd= kbd.readLine();
System.out.print("Enter grade (freshman, sophomore, junior, senior: ");
String categoryAdd = kbd.readLine();
System.out.print("Major: ");
String majorAdd = kbd.readLine();
System.out.print("Enter graduating year: ");
int yearAdd = Integer.parseInt(kbd.readLine());
System.out.print("Enter student ID: (xxxx.xxxx: ");
double idAdd= Double.parseDouble(kbd.readLine());
dc.add(nameAdd, categoryAdd , majorAdd ,
yearAdd , idAdd);
}
break;
}
It should be String nameSearch = kbd.nextLine();. I read on the multiline java tutorial.
Hope this helps. In some way.