I am to get an integer radius from a user and then display the area and circumference of the circle. If the user enters a negative integer or something other than an integer (a or 5.6) it asks the user to re-enter.
Below is the code for catching the wrong inputs, but it is using Scanner and I need to use JOptionPane.
import java.util.Scanner;
public class GetCircle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius = 0;
System.out.print("Please enter size of radius (Must be integer): ");
while (true){
if(input.hasNextInt()){
radius = input.nextInt();
if (radius < 0){
System.out.println("That is not a valid number, please try again :");
} else {
System.out.println("The radius is: " + radius);
}
} else {
System.out.println("That is not a valid number, please try again :");
input.next();
}
}
}
}
I do not know how to use hasNextInt with JOptionPane....can anyone help?
here is what i have for JOptionPane for now:
import javax.swing.JOptionPane;
public class Radius
{
public static void main(String[] args)
{
String radius = JOptionPane.showInputDialog(null,"Enter
Radius","Radius",JOptionPane.QUESTION_MESSAGE);
Integer.parseInt(radius);
JOptionPane.showMessageDialog(null,"the string you entered is: " + radius, "results",JOptionPane.PLAIN_MESSAGE);
}
}