I am trying to make a program that converts temperature to celsius and I am stuck on how to give the user an error if they try to enter something other than a number. Does anyone have any suggestions to help me? here is my code:
import java.text.DecimalFormat;
import java.util.Scanner;
public class FinalTemp {
public static void main(String[] args)
float celsius;
int temp;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
temp = keyboard.nextInt();
while (confirm.equalsIgnoreCase("N"));
celsius =(float)((temp-32)*5.0/9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp " degrees in Fahrenheit is equal to " celsius "
degrees celsius");
if (celsius >= 0)
System.out.println(temp " degrees in Fahrenheit is equal to " celsius "
degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment
for the class");
}
keyboard.close();
}
}
CodePudding user response:
A possible solution:
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
Then your entire code would be like that:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
float celsius;
int temp = 0;
String confirm = "Y/N";
Scanner keyboard = new Scanner(System.in);
do {
System.out.println("Enter temperature in fahrenheit: ");
boolean invalid = true;
while (invalid) {
try {
String tempStr = keyboard.next();
temp = Integer.parseInt(tempStr);
invalid = false;
}
catch (Exception x) {
System.out.println("Invalid value, inform a valid temperature");
}
}
while (confirm.equalsIgnoreCase("N")) ;
celsius = (float) ((temp - 32) * 5.0 / 9.0);
celsius = Float.parseFloat(new DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0)
System.out.println(temp " degrees in Fahrenheit is equal to " celsius " degrees celsius");
if (celsius >= 0)
System.out.println(temp " degrees in Fahrenheit is equal to " celsius " degrees celsius");
System.out.println("Would you like to enter another temperature?");
System.out.println("Please type Y/N.");
confirm = keyboard.next();
}
while (confirm.equalsIgnoreCase("Y")); {
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 Final Assignment for the class ");
}
keyboard.close();
}
}
CodePudding user response:
Another way you could do this is to accept a String from the User and check it with the String#matches() method with a small Regular Expression (regex) as an argument so as to validate that what was supplied is either a signed or unsigned Integer or floating point value, for example:
String ls = System.lineSeparator();
String confirm = "";
// Allow both fahrenheit & celsius to be entered as floating point values.
float temp, celsius;
Scanner keyboard = new Scanner(System.in);
do {
String temperature = "";
while (temperature.isEmpty()) {
System.out.print("Enter temperature in fahrenheit (c to cancel): -> ");
temperature = keyboard.nextLine().trim();
// Is Cancel desired?
if (temperature.equalsIgnoreCase("c")) {
System.out.println(ls "Tempurture Convertion Canceled!");
confirm = "n";
break;
}
// Validate entry...
if (!temperature.matches("-?\\d (\\.\\d )?")) {
System.out.println(ls "Invalid Tempurature Entry (" temperature
")! Try again..." ls);
temperature = "";
}
}
if (confirm.equalsIgnoreCase("n")) {
break;
}
temp = Float.parseFloat(temperature);
celsius = ((temp - 32.0f) * (5.0f / 9.0f));
celsius = Float.parseFloat(new java.text.DecimalFormat("##.##").format(celsius));
System.out.println();
if (celsius <= 0.0f) {
System.out.println(temp " degrees Fahrenheit is equal to "
celsius " degrees Celsius.");
}
else if (celsius >= 0) {
System.out.println(temp " degrees in Fahrenheit is equal to "
celsius " degrees celsius");
}
while(confirm.isEmpty()) {
System.out.println(ls "Would you like to enter another temperature?");
System.out.print("Please enter Y or N: -> ");
confirm = keyboard.nextLine().trim();
// Validate entry...
if(!confirm.matches("(?i)[yn]")) {
System.out.println("Invalid Responce (" confirm ")! Try again...");
confirm = "";
}
}
System.out.println();
} while (confirm.equalsIgnoreCase("Y"));
System.out.println();
System.out.println("Hello Instructor Fekete, This it the INF231 "
"Final Assignment for the class.");
The Regular Expressions used:
Expression: "-?\\d (\\.\\d )?"
visit regex101.com for explanation
Expression: "(?i)[yn]"
visit regex101.com for explanation