I honestly have no idea while my do-while loop is not working. Every time I run the program it just skips over it and moves on without even asking for the reply.
import java.util.*;
public class CourseApp {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList <Course> courseList = new ArrayList<Course>();
String reply;
//boolean boolVal = true;
do {
System.out.print("Enter new course number: ");
String tempCourseNum = s.nextLine();
System.out.print("Enter course name: ");
String tempCourseName = s.nextLine();
System.out.print("Enter instructor's last name: ");
String tempLastName = s.nextLine();
System.out.print("Enter instructor's first name: ");
String tempFirstName = s.nextLine();
System.out.print("Enter instructor's username: ");
String tempUsername = s.nextLine();
//new Instructor(tempLastName, tempFirstName, tempUsername);
Instructor tempInstruc = new Instructor(tempLastName, tempFirstName, tempUsername);
System.out.print("Enter textbook title: ");
String tempTitle = s.nextLine();
System.out.print("Enter textbook author: ");
String tempAuthor = s.nextLine();
System.out.print("Enter textbook price (no $ sign): ");
double tempPrice = s.nextDouble();
//new TextBook(tempTitle, tempAuthor, tempPrice);
TextBook tempText = new TextBook(tempTitle, tempAuthor, tempPrice);
courseList.add(new Course(tempCourseNum, tempCourseName, tempInstruc, tempText ));
System.out.print("\nEnter another course? (y/n): ");
reply = s.nextLine();
} while (reply.equalsIgnoreCase("Y"));
for (int i =0; i < courseList.size(); i++) {
System.out.println("\n\tCourse #" + (i+1)+":");
System.out.println(courseList.get(i));
}
}
}
If anyone could tell me what the problem is that would be great
Problem is with this line
double tempPrice = s.nextDouble();
It only reads a double value and not the newline after that, and your newline will be read by the nextLine() after the nextDouble(). In order to prevent that add a s.nextLine() after s.nextDouble() to read the newline
double tempPrice = s.nextDouble();
s.nextLine();
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();
}
}
Here is a basic while loop program, where the program at the end should ask the user if you want to keep going or not. The bug is that the program doesn't let me input (y/n) which is the last String Input.
This does not happen when the last input is an integer value.
import java.util.Scanner;
public class lol {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int age;
String name = "";
String height = "";
String userOption = "";
while (!userOption.equals("n"))
{
System.out.println();
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.println();
System.out.print("Enter your age: ");
age = sc.nextInt();
System.out.println();
System.out.print("Enter your height: ");
height = sc.nextLine();
System.out.println();
System.out.println("Do you want to keep going? (y/n)");
// The program over looks this line of code
userOption = sc.nextLine();
if(userOption.equals("y"))
{
System.out.println("Breaking");
break;
}
else
{
continue;
}
}
}
}
See this topic, additional nextLine() call could be a workaround
Scanner is skipping nextLine() after using next() or nextFoo()?
Minor adjustments to the code will fix its behaviour ;)
Scanner sc = new Scanner(System.in);
int age;
String name = "";
String height = "";
String userOption = "";
while (true) {
System.out.print("Enter your name: ");
name = sc.nextLine();
System.out.println();
System.out.print("Enter your age: ");
age = sc.nextInt();
System.out.println();
System.out.print("Enter your height: ");
height = sc.nextLine();
System.out.println();
System.out.println("Do you want to keep going? (y/n)");
// The program over looks this line of code
userOption = sc.nextLine();
if (userOption.equals("y")) {
// nothing
System.out.println("Continuing");
} else {
System.out.println("Stopping");
break;
}
}
System.exit(0);
I would however agree that you should take a look at Scanner is skipping nextLine() after using next() or nextFoo()?
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");
}
}
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 2 years ago.
import java.util.Scanner;
public class NoteIt {
public static void main(String[]args) {
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.nextLine();
System.out.print("\nBody: ");
Main[i-1][1]=s.nextLine();
}
}
}
I don't know why it is not asking for Title?
According to #Rohit Jain, That's because the Scanner.nextInt method does not read the newline character in your input created by hitting "Enter," and so the call to Scanner.nextLine returns after reading that newline.
Description here: check this question too
Try this it may help...
Scanner s = new Scanner(System.in);
int Answer;
int i=2;
System.out.print("\nPlease Enter your Name: ");
String Name = s.nextLine();
System.out.println("Welcome to Note-It "+Name+", We hope you'll enjoy our application. ");
String[][] Main = new String[2][2];
Main[0][0]="Create new Note";
Main[1][0]="View My Notes";
System.out.println("\nPlease select what to do: \n");
for(int n=0; n<2; n++){
System.out.println((n+1)+") "+Main[n][0]);
}
System.out.print("\nPlease enter your response: ");
Answer = s.nextInt();
if(Answer == 1){
i++;
Main = new String[i][2];
System.out.print("\nTitle: ");
Main[i-1][0]=s.next();
System.out.print("\nBody: ");
Main[i-1][1]=s.next();
}
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?