Note that there are some explanatory texts on larger screens.

plurals
  1. POcorrect way to check user input with scanner
    primarykey
    data
    text
    <p>I'm very new to programming, I've wrote an app for working the area out of certain shapes.</p> <p>I have the <code>AreaCalculations</code> in another class can provide if needed but that works fine.</p> <p>My problem is checking when the user types a character instead of a double. As you can see from my code i got it to work by using a while loop and <code>(!reader.NextDouble())</code> .</p> <p>This works but i then have to repeat the question. I could do this throughout the program but is there an easier/tidier way of doing this???</p> <p>Thanks,</p> <p>Craig </p> <p>My code so far :</p> <pre><code>package areaprog; import java.util.Scanner; public class Mainprog { public static void main (String [] args){ //Area Menu Selection System.out.println("What shape do you need to know the area of?\n" + "1: Square?\n" + "2: Rectangle?\n" + "3: Triangle?\n" + "4: Circle? \n" + "5: Exit\n" ); //User input for menu Scanner reader = new Scanner(System.in); System.out.println("Number: "); //Menu syntax checking while (!reader.hasNextDouble()) { System.out.println("Thats not a number you tool.\n"); System.out.println("Now pick again\n" + "1: Square?\n" + "2: Rectangle?\n" + "3: Triangle?\n" + "4: Circle? \n" + "5: Exit\n" ); reader.next(); //ask for next token? } double input = reader.nextDouble(); reader.nextLine(); //Depending on user selection, depends on what method is called using switch. Scanner scan = new Scanner(System.in); //Square selection if (input == 1){ System.out.println("What is a length of 1 side of the Square?\n"); double s1 = scan.nextDouble(); double SqAns = AreaCalculator.getSquareArea(s1); System.out.println("The area of you square is: " + SqAns); } //Rectangle selection if (input == 2){ System.out.println("What is the width of your rectangle?.\n"); double r1 = scan.nextDouble(); System.out.println("What is the height of your rectangle?\n"); double r2 = scan.nextDouble(); double RecAns = AreaCalculator.getRectArea(r1, r2); System.out.println("The area of your rectangle is: " + RecAns); } //Triangle selection if (input == 3){ System.out.println("What is the base length of the triangle?."); double t1 = scan.nextDouble(); System.out.println("What is the height of your triangle?"); double t2 = scan.nextDouble(); double TriAns = AreaCalculator.getTriArea(t1, t2); System.out.println("The area of your triangle is " + TriAns); } //Circle selection if (input == 4){ System.out.println("What is the radius of your circle?."); double c1 = scan.nextDouble(); double CircAns = AreaCalculator.getCircleArea(c1); System.out.println("The area of your circle is " + CircAns); } //Exit application if (input == 5){ System.out.println("Goodbye."); } } } </code></pre> <p>Ok so I've added in a Exception to catch the error. So its a bit cleaner now in the way it handles people not using intergers.</p> <p>Number: 1 What is a length of 1 side of the Square?</p> <p>a Why are you trying to be clever? use an interger.</p> <p>But then the program just ends.... How would i either get them back to the main menu or even get them to re-input there last effort?</p> <p>Thanks,</p> <p>Craig</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload