Computer problems,Computer help
*AX SOFT>>>Programming & Design

Java help! Do while boolean is false loop?


I want to program to keep asking for user inputs, until they input a number. But this - if the first input number is not a number, it will infinitely print the output statement, not allowing the user to type a new line.
Boolean good = true;
System.out.println("Input base number (floating point):");
if (keyboard.hasNextDouble())
a = keyboard.nextDouble();
else
do
{
System.out.println("Input base number (floating point):"); //must be numbers.
a = keyboard.nextDouble();
if (keyboard.hasNextDouble())
{
a = keyboard.nextDouble();
break;
}
else
good = false;
}while (!good)

import java.util.Scanner;
public class ans1
{

public static void main (String [] args)
{
Boolean good = false;

Scanner keyboard = new Scanner(System.in);

Double a;
String b;
System.out.println("Input base number (floating point):"); //asking for your input

good=false; //keep good=false to tell the computer that there is no valid input
while (!keyboard.hasNextDouble()) // if no number is entered then keep asking for it
{
if (!good) //this if statement is here to ask for the input
{
good=true;
System.out.println("Input base number (floating point):"); //<----------------------
// |
} // |
// |
if (keyboard.hasNextDouble()) //if it is a number then assign the value to ur variable |
a = keyboard.nextDouble(); // ...
else // |
good=false; //tell the computer to output the message again --------------------


b = keyboard.next(); //dumby input to wait for a value

}



}
}

I think you error is this:

You declare good as true in the beginning, so if your first input is not a number it is changed to false but nowhere in the while loop do you turn it back to true, so the while loop will never end.

What you should do is this:

if (keyboard.hasNextDouble())
{
a = keyboard.nextDouble();
good = true;
}

That should solve your problem... I think =)

Good luck

Here's a solution, but try to either get yours to work or understand my solution by stepping through it with a debugger.

package com.kaydell.answers;

import java.util.Scanner;

public class ReadData {

// The method readDouble was made to be generally useful.

public static double readDouble(Scanner in) {
double d = 0.0;
boolean good = false;
do {
System.out.println("Input a decimal number: \n");
String input = in.next();
try {
d = Double.parseDouble(input);
good = true;
} catch (Exception e) {
good = false;
}
System.out.println("You entered the input'" + input + "'");
if (!good) {
System.out.println("The string: '" + input + "' is not a valid decimal number. Try again.");
}
} while (!good);
return d;
}

// this main method is just to test the readDouble() method.

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
double d = readDouble(keyboard);
System.out.println("You entered the decimal number: " + d);
}


}

Tags
  General - Computers & Internet   Software   Security   Programming & Design   Facebook   Flickr   Google   MSN   MySpace
Related information
  • HTML and Javascript Program?

    <script type="text/javascript"> function leng() { var str=document.form1.getinput.value; alert("length of String : " +str.length); } </script> <form name...

  • I need a logo for my Construction firm?

    Why don't you search existing logo sites to get ideas? Here are some to try - Logo Search - ...

  • How do you make this into an animated gif?

    i got the flash file from ...

  • I.E. 7 View Problem?

    Use Mozilla Firefox bro!

    ...
  • Web Design Languages?

    It's always good to know as many languages as possible. Of course you won't have the time to master everyone, but you don't need to - that would be time prohibitive. PHP is a good...

  • How do you cut an avi. file into 2 pieces?

    OK, Basically you require a movie splitter rather than a file splitter. File splitters cut the files into required fragments logically. You may or may not open the file individually. Movi...

  • Adobe Indesign Serial Code?

    you should just be able to replace your original "Public.dll" [without the quotation marks] with the backup you have. Public.dll contains the key but is really hard to extract. better ...

  • Help with designing a new font?

    There are softwares available which will help you in converting this font which you have ( screen shot ) into TTF ( Windows ). Search google forr tutorials/articles/blog log on to ...

  •  

    Categories--Copyright/IP Policy--Contact Webmaster