I am a student at a local tech in Ireland first year. We are as you guessed doing java and the at the moment we are handling Scanners at the moment. I have added my code at the bottom and a little confused.
When I run the program it works fine till I get to the "Enter country" and the "Enter course" and these two line land up printing together no matter if I change the scanner to f.nextLine or f.next.
Anyone able to help me out with this as I am missing something and I can't figure out what it is.
Many thanks
Matthew
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"#student.ait.ie");
System.out.println("Please review the information.");
}
nextDouble() scans the next token and parses it into a double. That is why you are facing this issue. You can simply move you nextLine() above the nextDouble(). Its a temporary solution to solve the problem.
Try below code. I just edited your code.
public static void main(String[] args)
{
String name; //declaring string name
String town; //declaring string town
double dist; //declaring double distance from AIT the person lives
int age; //declaring their age
double height; //declaring their height
double weight; //declaring their weight
String country; //declaring their country that they are from
String course; //declaring their course that they are taking
int years; //declaring the amount of years they are in the course
String sNum; //declaring there email address string
Scanner f = new Scanner(System.in);
//Scanner s = new Scanner(System.in);
// below is the prompt for all the information and there keyboard input with scanner
System.out.print("Enter your name here & press enter: ");
name = f.nextLine();
System.out.print("Enter the name of the town or city you live in & press enter: ");
town = f.nextLine();
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.nextLine();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.nextLine();
System.out.print("Enter the number of years the course is & press enter: ");
years = f.nextInt();
System.out.print("Enter your student number & press enter: ");
sNum = f.next();
System.out.print("Enter the distance from AIT that you live in kilometers & press enter: ");
dist = f.nextDouble();
System.out.print("Enter your age & press enter: ");
age = f.nextInt();
System.out.print("Enter your height & press enter: ");
height = f.nextDouble();
System.out.print("Enter your weight & press enter: ");
weight = f.nextDouble();
//all the outputs of the information collected
System.out.println("Your name: "+name+" Town: "+town);
System.out.println(town+" is "+dist+" away from AIT.");
System.out.println("You are "+height+" meters tall & "+weight+" kgs & you are a "+country+" citizen.");
System.out.println("Your studying "+course+" for "+years+" years at AIT.");
System.out.println("Your student number is "+sNum+" & your student email address is "+sNum+"#student.ait.ie");
System.out.println("Please review the information.");
}
}
If you use
System.out.print("Enter the name of country that you are from & press enter: ");
country = f.next();
System.out.print("Enter the name of course you are taking & press enter: ");
course = f.next();
it will work. You can do this for the other nextLines also. For some reason you read the student number as a string instead of a number, but since you treat all you input as strings, you could just use next for everything.
Edit:
Don't forget to call f.close() to close the scanner.
Related
This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 1 year ago.
I am working in the ParkingCarSimulator programming challenges of the starting out with java book but I found a problem and I can't really see what is wrong, it seems right and the same than all the other ones. So I am getting the user to input values for officer name, officer badge, car make, car model, etc. The program is compiling good and seems fine but when you run it and start entering the info, you will notice that entering the info for the officer name is good, same for the officer badge but next should be car's make but it just skips that and goes into car's model and I can't really see why, any ideas? here is the code(check 3rd println):
public class ParkingCarSimulator {
public static void main(String[] arsg)
{
String officerName, Make, carModel, carColor, carLicense;
int badgeNumber, minOnCar, minPurchased;
double fine = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter the officer's name");
officerName = keyboard.nextLine();
System.out.println("Enter officer's badge number");
badgeNumber = keyboard.nextInt();
System.out.println("Enter the car's make");
Make = keyboard.nextLine();
System.out.println("Enter the car's model");
carModel = keyboard.nextLine();
System.out.println("Enter the car's color");
carColor = keyboard.nextLine();
System.out.println("Enter the car's liscence number");
carLicense = keyboard.nextLine();
System.out.println("Enter minutes on car");
minOnCar = keyboard.nextInt();
if(minOnCar <= 0) {
System.out.println("Invalid Entry. Please try again.");
minOnCar = keyboard.nextInt();
}
System.out.println("Enter the number of minutes purchased");
minPurchased = keyboard.nextInt();
if(minPurchased <= 0) {
System.out.println("Invalid Entry. Please try again.");
minPurchased = keyboard.nextInt();
}
if(minOnCar > minPurchased) {
fine = 25.0;
}
if(minPurchased < minOnCar) {
System.out.println("Car parking time has expired");
System.out.println("\nTicket Data:");
System.out.println("\nMake: " + Make);
System.out.println("\nModel: " + carModel);
System.out.println("\nColor: " + carColor);
System.out.println("\nLiscence Number: " + carLicense);
System.out.println("\nOfficer Name: " + officerName);
System.out.println("\nBadge Number: " + badgeNumber);
System.out.println("\nFine: " + fine);
} else {
System.out.println("The car parking minutes are valid");
}
}
}
.nextLine() reads the "enter" you hit once you enter the value of badgeNumber. In order to fix this, you can do this in your code:
System.out.println("Enter officer's badge number");
badgeNumber = keyboard.nextInt();
System.out.println("Enter the car's make");
Make = keyboard.nextLine(); //this takes up the enter value when it is pressed
Make = keyboard.nextLine(); //changing the value of Make to the string the user enters.
Here's another link that could help you: 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");
}
}
public class FinalMarkCalculator
{
/**
* This method is used to caculate the students mark
*/
public static void main(String[] args)
{
//Assigning the string a variable that holds the students name
String studentName;
//Initalizing a keyboard scanner
Scanner keyboardStudentName = new Scanner(System.in);
//System printing out the question
System.out.print("Enter the students name: ");
//Assigns the string studentName to the users input
studentName = keyboardStudentName.nextLine();
//Assigning the double a variable that hold the students assignment marks as a integer
double assignmentMarks;
//Assigning the string a variable that hold the students assignment marks
String assignmentMarksInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardAssignmentMarks = new Scanner(System.in);
//Asking the user for assingment marks while printing students name
System.out.printf("Enter %s's mark for Assignments (Max. 140): ", studentName);
//Taking the user input and assigning it to our string assignmentMarksInput
assignmentMarksInput = keyboardAssignmentMarks.nextLine();
//Converting the user input from a string to a double
assignmentMarks = Double.parseDouble(assignmentMarksInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double midTermExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String midTermExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardExamMidTermMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Mid-Term Exam (Max. 60): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
midTermExamMarkInput = keyboardExamMidTermMark.nextLine();
//Converting the user input from a string to a double
midTermExamMark = Double.parseDouble(midTermExamMarkInput);
//Assigning the double a variable that hold the students Mid-Term exam marks as a integer
double finalExamMark;
//Assigning the string a variable that hold the students assignment marks from user input
String finalExamMarkInput;
//Initalizing a keyboard scanner with the variable
Scanner keyboardFinalExamMark = new Scanner(System.in);
//Asking the user for the Mid-Term Exam mark while printing students name
System.out.printf("Enter %s's mark for Final Exam (Max. 85): ", studentName);
//Taking the user input and assigning it to our midTermExamMarkInput
finalExamMarkInput = keyboardFinalExamMark.nextLine();
//Converting the user input from a string to a double
finalExamMark = Double.parseDouble(finalExamMarkInput);
//Printing grade report title
System.out.println("Grade report" + "\n------------");
//Printing the students name
System.out.println(studentName + "\n");
//Printing the assignments mark
System.out.printf("%.2f/140 worth 15%%\n", assignmentMarks);
//Printing the Mid-Term exam mark
System.out.printf("%.2f/60 worth 40%%\n", midTermExamMark);
//Printing the Final exam mark
System.out.printf("%.2f/85 worth 45%%\n", finalExamMark);
double finalMark = (assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45);
//Printing the total calculated final mark
System.out.printf("\n\nFinal Mark: " + finalMark);
}
}
My code runs fine but it does not do the math correctly and I can't seem to figure out why even if I put everything perfect it still doesn't equal 100%.
This is the equation I'm using and I think this is how it should be done:
(assignmentMarks*.15)+(midTermExamMark*.40)+(finalExamMark*.45)
The maths should be
double finalMark = (assignmentMarks/140*15)+(midTermExamMark/60*40)+(finalExamMark/85*45);
Also I think it is better to not declare (and then later) initialize your variables - do in one step
double assignmentMarks = Double.parseDouble(assignmentMarksInput);
Also just use one Scanner
Scanner scanner = new Scanner(System.in);
and use it for all input
String studentName = scanner.nextLine();
....
String assignmentMarksInput = scanner.nextLine();
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?
This is my code so far:
Scanner scanner = new Scanner(System.in); //Scanning all user input
// Dialogue
System.out.println("Welcome to jAddress!");
System.out.println("Please tell me your name and phone number?");
String personalInfo = scanner.nextLine();
System.out.println("So your name & number is " + personalInfo);
//-----------------------------------------------------------------------------
String[] theList; // setting the type and name of the Array
theList = new String[6];// giving the array account of how many
/*Inputting the Name & Number Of Each Individual and storing it*/
personalInfo = theList[0]; // This one stores your personalInfo into theLis[0]
System.out.println("Do you Have a Name & Number You'd Like to Input? y/n");
String yesNo = scanner.nextLine();
String y = "yes"; String n = "no";
if(yesNo == y){
System.out.println("Yay:D");
}else if(yesNo == n){
System.out.println("GoodBye");
//break;
}
System.out.println("Please Enter Name & Phone Number:");
theList[1] = scanner.nextLine(); //Storing Number & Name for theList[1]
System.out.println("Please Enter Name & Phone Number:");
theList[2] = scanner.nextLine(); //Storing Number & Name for theList[2]
System.out.println("Please Enter Name & Phone Number:");
theList[3] = scanner.nextLine(); //Storing Number & Name for theList[3]
System.out.println("Please Enter Name & Phone Number:");
theList[4] = scanner.nextLine(); //Storing Number & Name for theList[4]
System.out.println("Please Enter Name & Phone Number:");
theList[5] = scanner.nextLine(); //Storing Number & Name for theList[5]
System.out.println("If you want to access any name simply Press 0-5.");
int chooseContact = scanner.nextInt();
I managed to link both the array into the user input. So the user can manually input a name and phone number as a String which then will get stored in one of the arrays.
However, I want the user at the end to be able to search the array and have it print the name and phone number. For example, I want the user to input 1 which will then output the array theList[0] number and name.
How can I do this? I'm looking for a simple method of achieving this, such as assigning variables etc.
you can simply use the index input by the user (+/- one depending on the case of 0-5/1-6 numbering) theList[index]