Reputation: 113
The user would need to enter a number for the meter reading, a try catch block will initialize if the user enters anything other than a number and an error message pops out if the user has a negative number, doing this in do-while would be simple but I am stumped on how this would be a for-loop
public static int readStartReading(){
int reading = 0;
Scanner keyboard = new Scanner(System.in);
boolean problemFlag = false;
do {
problemFlag = false;
try {
System.out.print("Enter the meter reading at the beginning of the year: ");
String input = keyboard.nextLine();
reading = Integer.parseInt(input);// 7. Assign a value to reading through the input device
} catch (NumberFormatException x){
problemFlag = true;
System.out.println("You have to enter a number.");
} if (reading < 0){
System.out.println("The beginning meter reading cannot be negative."); }
} while (reading < 0 || problemFlag);
return reading;
}
I have noticed my mistake. Thank you for the responses
Upvotes: 0
Views: 98
Reputation: 2619
What about
public static int readStartReading()
{
int reading = -1;
Scanner keyboard; //remove first reading here
while(1)
{
try
{
System.out.print("Enter the meter reading at the beginning of the year: ");
String input = keyboard.nextLine();
reading = Integer.parseInt(input);// 7. Assign a value to reading through the input device
//if control reaches here, you have a valid integer, test it
if (reading >= 0)
break;
else
System.out.println("The beginning meter reading cannot be negative.");
}
catch (NumberFormatException x)
System.out.println("You have to enter a number.");
}
return reading;
}
Upvotes: 2
Reputation: 56
You can convert it to a for loop very simply but it will not be any difference. In your case a do-while is better suited.
public static int readStartReading(){
int reading = 0;
Scanner keyboard = new Scanner(System.in);
boolean problemFlag = false;
for(;reading < 0 || problemFlag;)
{
problemFlag = false;
try {
System.out.print("Enter the meter reading at the beginning of the year: ");
String input = keyboard.nextLine();
reading = Integer.parseInt(input);// 7. Assign a value to reading through the input device
} catch (NumberFormatException x){
problemFlag = true;
System.out.println("You have to enter a number.");
} if (reading < 0){
System.out.println("The beginning meter reading cannot be negative."); }
}
return reading;
}
There are also couple small issues
you don't need the problemFlag
you are doing the initial reading twice, (user will have to input value twice)
I would rewrite it like so:
public static int readStartReading()
{
int reading = 0;
Scanner keyboard; //remove first reading here
do
{
try {
System.out.print("Enter the meter reading at the beginning of the year: ");
String input = keyboard.nextLine();
reading = Integer.parseInt(input);// 7. Assign a value to reading through the input device
//if control reaches here, you have a valid integer, test it
if (reading < 0)
System.out.println("The beginning meter reading cannot be negative.");
} catch (NumberFormatException x){
reading = -1; //set reading to a negative invalid value
System.out.println("You have to enter a number.");
}
} while(reading < 0)
return reading;
}
Upvotes: 1