My code is below. Not sure how to eliminate this problem. I tried using next.Line(), but that did not work.
String demoEmpName;
int demoIdNum;
double demoPayRate;
int demoHoursWorked;
Scanner keyboard = new Scanner(System.in);
System.out.print("What is your name?");
demoEmpName = keyboard.nextString();
System.out.println("What is your ID number?");
demoIdNum = keyboard.nextInt();
System.out.println("What is your hourly pay rate?");
demoPayRate = keyboard.nextDouble();
System.out.println("How many hours did you work?");
demoHoursWorked = keyboard.nextInt();
Payroll pyrll = new Payroll(demoEmpName, demoIdNum, demoPayRate, demoHoursWorked);
Change keyboard.nextString() to keyboard.next().
There is no nextString() method in Scanner class.
Oh, and in case all this code you posted is not enclosed in some method, that would be another problem.
There is no existence of nextString() method in the Scanner class.If the employee's name consists of just a single word,you should use:-
demoEmpName=keyboard.next();
If the name consists of more than one words,use demoEmpName=keyboard.nextLine();
Related
I'm making a java program that has to store data using classes and objects, My question is how do input characters like a name ( billy ) into my code.
Here is the class i did.
class bank
{
int AccountID;
int HolderName;
double AccountBalance;
}
And i'm assigning here
angel.AccountID = 7532;
angel.HolderName = 753; // angel
angel.AccountBalance = angelbalance;
I know that i can input integers using the following code
System.out.println("Set Balance for Angel: ");
int angelbalance = sc.nextInt();
My question is how do i input text/characters in a way (scanner) does with integer
Sorry for the bad explanation.
Do you mean getting the name input using the Scanner object? Because you've imported java.util.Scanner, you can do:
Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
This will read the next line.
You can use (JOptionPane.showInputDialog) and it is easy to use if you want input from user
String name= JOptionPane.showInputDialog("Enter your name: "));
You need to either call the Scanner.nextLine() method or the Scanner.next() method to store String input such as a name like "Billy". Here is an example below:
//You can use the Scanner.next() method or the Scanner.nextLine() method to store Strings
Scanner scan = new Scanner(System.in);
System.out.println("Enter a name: ");
String billy = scan.next();
System.out.println("You entered: " + billy);
And here is your output:
Enter a name:
Billy
You entered: Billy
I'd check out the Scanner documentation here: https://docs.oracle.com/javase/8/docs/api/java/util/class-use/Scanner.html
i was trying to do some practice for my school. Please help me because i wanted to get both integers and string in a single scanner. Is it possible?
Scanner pal = new Scanner (System.in);
System.out.print("Enter Temperature you want to convert: ");
temp = pal.nextInt();
System.out.print("Convert to?: ");
convert_to = pal.next();
If I understood your requirement properly - you want to parse an input like '12 C'. To do this we can read the input and then parse it based on each word. So in this case the first index will contain the degree and the second index will contain the unit.
Scanner pal = new Scanner (System.in);
System.out.print("Enter Temperature you want to convert: ");
String values[] = pal.nextLine().split(" ");
int temp = Integer.parseInt(values[0]);
char unit = values[1].charAt(0);
You can then use this for the convert question, thereby we are using just one Scanner object to read all the input.
System.out.print("Convert to?: ");
String convertTo = pal.next();
I am having a problem where my program is scanning for two different inputs at the same time.
import java.util.Scanner;
public class Person {
public static void main(String[] args){
Person p1 = new Person();
System.out.println("1: Add Person");
System.out.println("2: Delete Person");
System.out.println();
System.out.print("Please make a selection: ");
Scanner keyboard = new Scanner(System.in);
int selection = keyboard.nextInt();
switch(selection){
case 1:
System.out.print("Please enter name: ");
String name = keyboard.nextLine();
p1.addPerson(name);
break;
}
}
public Person(){
}
public void addPerson(String name){
int day, month, year;
Scanner keyboard = new Scanner(System.in);
System.out.print("Please enter date of birth in the format dd mm yyyy: ");
day = keyboard.nextInt();
month = keyboard.nextInt();
year = keyboard.nextInt();
}
}
This is the output:
1: Add Person
2: Delete Person
Please make a selection: 1
Please enter name: Please enter date of birth in the format dd mm yyyy:
The program does not wait for the name to be entered, how do i fix this?
The problem is when you do nextInt() it scans an integer, but not the new line character(\n), so when you call nextLine() it just consumes \n you typed when was doing selection and returns empty string.
Several ways to fix it:
First is to call nextLine() after nextInt. To fix your code you would do this:
int selection = keyboard.nextInt();
keyboard.nextLine();
Second option is to call nextLine() but when you need int wrap in in Integer.parseInt(). So, for example, your selection would look like:
int selection = Integer.parseInt(keyboard.nextLine());
Other option would be to use next() instead of nextLine, however, this approach wouldn't work if name contains spaces.
You should use keyboard.next()
From the java docs:
next(): Finds and returns the next complete token from this scanner.
This is what keyboard.nextLine() does:
nextLine(): Advances this scanner past the current line and returns the input that was skipped.
So I have my code compile but when I run it it combines my lines
System.out.print("Enter ID Number");
double ID=s.nextDouble();
if(ID >9999|| ID<0)
ID=0;
System.out.print("Enter Make of Vehicle");
String make=s.nextLine();
System.out.print("Enter Model of Vehicle");
String model=s.nextLine();
System.out.print("Enter Color of Vehicle");
String color=s.nextLine();
System.out.print("Enter Year");
double year=s.nextDouble();
if(year <2000 || year>2017)
year=0;
When I run the program it combines the lines to say
enter make of vehicle enter model of vehicle
How do I stop this as they are supposed to be separate entries
Use System.out.println instead of System.out.print
Put s.nextLine() after reading double. The problem is that s.nextDouble(); does not consume new line character, so s.nextLine(); after System.out.println("Enter Make of Vehicle"); is called automatically, you don't see anything more because there is only this one new line character (and it is being consumed).
double ID=s.nextDouble();
s.nextLine();
if(ID >9999|| ID<0)
ID=0;
Second thing is to replace System.out.print with System.out.println to print output in new line.
Call s.nextLine() after double ID=s.nextDouble(), couse nextDouble() doesn't consume the \n
I'm doing an assignment for class. For some reason the program completely skips the part where the variable name is supposed to be typed in by the user. I can't think of any reason why it's behaving this way, since the rest of my code that is after the cardType part (which asks for things such as String and int types work fine and in order.
System.out.println("Enter the card information for wallet #" +
(n+1) + ":\n---\n");
System.out.println("Enter your name:");
String name = scan.nextLine();
name = capitalOf(name);
System.out.println("Enter card type");
String cardType = scan.nextLine();
cardType = capitalOf(cardType);
You probably need to consume the end of the last line you read prior to trying to get the user name :
scan.nextLine(); // add this
System.out.println("Enter the card information for wallet #" +
(n+1) + ":\n---\n");
System.out.println("Enter your name:");
String name = scan.nextLine();
name = capitalOf(name);
System.out.println("Enter card type");
String cardType = scan.nextLine();
cardType = capitalOf(cardType);
It is behaving this way because I am quite sure you used the same scanner object to scan for integers/double values before you used it to scan for name.
Having said that does not mean you have to create multiple scanner objects. (You should never do that).
One simple way to over come this problem is by scanning for strings even if you are expecting integers/doubles and cast it back.
Scanner scn = new Scanner(System.in);
int numbers = scn.nextInt(); //If you do this, and it skips the next input
scn.nextLine(); //do this to prevent skipping
//I prefer this way
int numbers = Integer.toString(scn.nextLine());
String str = scn.nextLine(); //No more problems