Saturday 15 March 2014

validation - Having problems with hasNextDouble method in Java -


In my program, I have user input a radius value, and then the program outputs out and out the circumference.

I want to ensure a number of user types, so I have used the hasDextDouble () method. Although it is not working properly.

When the program runs for the first time, I was bolded in the code given below (apparently, I can not make bold code, hence it is code with nearby stars) Enter a number "and" gt; " Appears as

However, if the program runs the loop for the second time, which I bold (which is nested within the loop of time which tests the number of users is positive), "Please enter a number Do ", appears twice.

I can not know why these words are printing twice. Can anyone help?

  / ** * On the basis of the user-provided radius, the circle uses the class to calculate the area and circumference of the circle. * * Author @ Britney Gefrooh * @ version 1.0 * / // Import Scanner Class Import java.util.Scanner; Public square circuits {public static zero main (string [] args) // // start a scanner object scanner scan = new scanner (system.); // Create new circle object circle circle 1 = new circle (); // declare variable variables; String garbage; String reply; // Make / While the answer of the loop is Y or Y {// a radius value System.out.print ("Enter Radius Value";); ** While (! Scan.hasNextDouble ()) {garbage = scan.nextLine (); System.out.print ("\ n Please enter a number and";); } ** // input input input = scan. // test if input is a positive number (input & lt; = 0) {// signal user system.out.println for a new radius value ("radius must be greater than 0"); System.out.print ("\ nEnter a radius value";); ** While (! Scan.hasNextDouble ()) {garbage = scan.nextLine (); System.out.print ("\ n Please enter a number and";); } ** // input input input = scan. } // Radius Circle 1 .To change the set radius, run the setRadius method; // Print Space System.out.println (""); // Display Output is system.out.println ("radius is" + circle1.getRadius ()); System.out.println ("area is" + circle1.getArea ()); System.out.println ("circumference" + circle1.getPerimeter ()); // Print Space System.out.println (""); // Ask the user if he wants to try again System.out.print ("Would you like to try again? Y or N & gt;"); Answer = scan.next (); // Print Space System.out.println (""); } While (answer.equalsIgnoreCase ("Y")); }}    

Change:

  answer = scan .the upcoming();   

From:

  scan .nextLine (); Answer = scan.nextLine ();   

Maybe you should try to simplify this code by making a special method for double reading along with recognizing this code? Also try to think that you have this 'blank' next line () operation is it necessary?

Edit ...

The problem is that scans .nextDouble (); Do not remove EOL Marker (End of Line). With the same scan.next (); . This is your problem EOL marker is understood by the condition while and it shows:

  "Please enter a number"; & Lt; Immediate EOL answer which was left in the scanner & gt; "Please enter a number & gt; enter" & lt; Now we have user input & gt;    

No comments:

Post a Comment