I was writing a code for class as follows:

import java.util.Scanner;
public class Salary2 {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String name;
        float salary;
        float hours;

        System.out.print("What is the employees name?");
        name=input.nextLine();

        while (!name.equals("stop"))
        {
        	System.out.printf("What is %ss salary per hour?", name);
        	salary=input.nextFloat();
        	System.out.printf("How many hours did %s work?", name);
        	hours=input.nextFloat();
        	System.out.printf("%ss salary for this week is $%.2f", name,hours*salary);
        	System.out.print("\nWhat is the employees name?");
        	input = new Scanner(System.in);
        	name=input.nextLine();
        }
    }
}

It took me forever to figure out that I had to redeclare "input" on line 21. Why is this necessary? Why can't I just overwrite "input" like a normal variable?

Recommended Answers

All 2 Replies

Well, let me tell you how nextLine() works.

After u use nextFlot() the second time, the input stream will have a new line character( linefeed) left on the stream. What the nextLine() does is, it reads that that character and stores it into the name input variable. So it seems like it skips the input. But actually it reads the new line character and goes to the next line.

So What u have to do is u have to flush whatever remains in the stream after the next Float(). So to do that u just make a call to nextLine() and then proceed for reading name.

import java.util.Scanner;
public class Salary2 {

    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        String name;
        float salary;
        float hours;

        System.out.print("What is the employees name?");
        name=input.nextLine();

        while (!name.equals("stop"))
        {
        	System.out.printf("What is %ss salary per hour?", name);
        	salary=input.nextFloat();
        	System.out.printf("How many hours did %s work?", name);
        	hours=input.nextFloat();
        	System.out.printf("%ss salary for this week is $%.2f", name,hours*salary);
                input.nextLine();
        	System.out.print("\nWhat is the employees name?");
          	name=input.nextLine();
        }
    }
}

reinitializing it creates a new instance of scanner initialized to start of the stream. So it has the same flush effect. But is inefficient.

Hi David,

An alternative method instead of having to re-construct the scanner class is changing your while argument.

Where you have while next equals stop, I would have:

while(in.hasNextLine()){

Then you can use if statements to characterise between the types of input:

(inside while)

System.out.print("What is the employees name?");        
name=input.nextLine();

if(in.hasNextFloat()) {

System.out.printf("What is %ss salary per hour?", name);
salary=input.nextFloat();        	
System.out.printf("How many hours did %s work?", name);        	
hours=input.nextFloat();        	
System.out.printf("%ss salary for this week is $%.2f", name,hours*salary);
}

You could easily have another if to check whether the input says "stop".

Hope you find this helpful.

Regards,
Cleo

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.