Results 1 to 9 of 9
  1. #1
    marshalthrone is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Check if double is double

    How do i check to see if the user entered a valid double, and if wrong tells them to try again??

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You could try converting the value to a double using Double.parseDouble. If the conversion fails then it's not a double.

  3. #3
    marshalthrone is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    How would i do that i have never used that, an example would be great thx.

  4. #4
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Are you reading the input from the keyboard, a file, or what?

    If you just want to check if a String is a valid double, try parsing it. If it parses, yeah! If not, an exception is thrown, catch it, and return false.

    Java Code:
    public class Test
    {
    	/**
    	 * Main function
    	 * 
    	 * @param args
    	 *            (not used)
    	 */
    	public static void main(String[] args)
    	{
    		String input = "1.7e-6";
    		double myDouble = Double.parseDouble(input);
    
    		System.out.println(myDouble);
    
    		// throws a NumberFormatException
    		double invalidDouble = Double.parseDouble("INVALID!");
    
    		System.out.println(invalidDouble);
    	}
    }
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

  5. #5
    marshalthrone is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    I'm reading in numbers like 4 and 89.78 and dont want "hi" or "seventy"

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by marshalthrone View Post
    How would i do that i have never used that, an example would be great thx.
    Read about Exceptions then.

  7. #7
    marshalthrone is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    public class Sort3 {

    public static Scanner in = new Scanner (System.in);

    public static void main (String [] args) {

    double a = GetDouble();

    double b = GetDouble();

    double c = GetDouble();


    }
    public static double GetDouble() {

    System.out.print ("Enter a double: ");

    double x = in.nextDouble ();


    return x;
    }
    }

    Heres my code i want to check if the double entered is a number like 4 and 78.34, not "seventy two" or "what"

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    ... and you never bothered to read the responses posted already?

  9. #9
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    6

    Default

    Check out the API for the Scanner class. There's a method that does exactly what you need.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  2. Double
    By Xystus777 in forum New To Java
    Replies: 2
    Last Post: 01-21-2009, 11:39 AM
  3. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM
  4. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 01:11 PM
  5. Divison - Double
    By new person in forum New To Java
    Replies: 1
    Last Post: 11-17-2008, 11:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •