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?