Reputation: 11
I am extremely new to programming Java and have what I'm sure is a very easy fix. I have the following code, and it works, but I was wondering how I could change it so if the user inputs something other than an int, it would loop back to the top after it gives the error message, so it would ask for a temp again. Thanks in advance.
Scanner in = new Scanner(System.in);
//User input of temperature
System.out.print("Enter temperature in Celsius: ");
if (in.hasNextInt())
{
int temperature = in.nextInt();
//Now determine what state the water will be in, either ice, gas, or water.
if (temperature >=100)
{
System.out.print("Gas!");
}
else if ((temperature <100) && (temperature >0))
{
System.out.print("Water!");
}
else if (temperature <=0)
{
System.out.print("Ice!");
}
}
else
{
System.out.println("Error: Not an Integer");
System.out.print("Please enter temperature in Celsius: ");
}
Upvotes: 1
Views: 21520
Reputation: 22633
The most simple solution to your answer is to stick your code into an infinite "run" loop.
Scanner in = new Scanner(System.in);
while (true) {
//User input of temperature
System.out.print("Enter temperature in Celsius: ");
if (in.hasNextInt())
{
int temperature = in.nextInt();
//Now determine what state the water will be in, either ice, gas, or water.
if (temperature >=100)
{
System.out.print("Gas!");
}
else if ((temperature <100) && (temperature >0))
{
System.out.print("Water!");
}
else if (temperature <=0)
{
System.out.print("Ice!");
}
}
else
{
System.out.println("Error: Not an Integer");
System.out.print("Please enter temperature in Celsius: ");
}
}
Run loops, or main loops as they're often called, provide a consistent loop that accepts user input and provides feedback. If/when you get into multi-threading/GUI programming, this is where most of your UI code goes. However, most project-based environments (Eclipse for Android apps, Xcode for iOS apps, etc.) have more complex and idiomatic run loops than while (true)
.
Upvotes: 0
Reputation: 2537
wrap your code with a loop, I would use while(...) for this purpose, exit condition would be an 'int' input. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
To validate user input use eg. regexp something like this: userInput.matches("[-+]?\d+(\.\d+)?");
Upvotes: 0
Reputation: 257
You have to use a loop. Look here for an introduction: http://www.leepoint.net/notes-java/flow/loops/loops.html
Upvotes: 0
Reputation:
It looks like you should look into using a while()
loop. You can use a snippet like this:
Scanner scanner = new Scanner(System.in); int input = -1; input = scanner.nextInt(); while (input != 0) { System.out.println("Enter 0 to exit the loop."); input = scanner.nextInt(); //without this, you will hit a loop that never ends. {
Where you can add your code to convert the temperature within that loop.
Upvotes: 1
Reputation: 13755
move it into a method
public static void main( String args[] ){
Scanner in = new Scanner(System.in);
readInput( in );
}
public static void readInput( Scanner in ){
System.out.print("Enter temperature in Celsius: ");
if ( in.hasNextInt() ){
// do your stuff here
}
else {
// print the errors
readInput( in );
}
}
Upvotes: 5