Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By lyss13

Thread: Scanner reading in doubles

  1. #1
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Angry Scanner reading in doubles

    Hi, I am having difficulties reading in a double using the Scanner class. Can anyone tell me why this code keeps executing the "else" statement even if the user inputs a number, such as 23?

    (Alternatively, removing the if-else statement gives an InputMismatch Exception at what is currently line 16 in the code)

    Java Code:
    import java.util.Scanner;
    
    public class mytest {
     public static void main(String[] args) {
      
      Scanner info = new Scanner(System.in).useDelimiter(",|\n");
     
      //Initialize
      double age1 ; 
      
      //Get User Input
      
     System.out.println("Enter a number");
     if (info.hasNextDouble()) 
     {
    	  age1 = info.nextDouble();
    	  System.out.println(age1); 
    	  }
      else 
          System.out.println("Value is not a real value.");   
    };
    }
    The output of the program is always "Value is not a real value." even if I enter numbers at the prompt.... Please help....
    Last edited by DarrylBurke; 09-01-2013 at 08:06 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Scanner reading in doubles

    I've removed the italic formatting from your post as it makes it difficult to read. Use bold and italic for stress, not for entire posts.

    I see you set a custom delimiter to the Scanner. What was the reasoning that led to that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Scanner reading in doubles

    I have a slightly more complicated program that reads in several strings and several doubles, comma-delimited. In trying to figure out what is wrong, I stripped it down to the bare minimum code that gives me the error, but just left the custom delimiter - I can take it out, if necessary.

  4. #4
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Scanner reading in doubles

    Wow, taking out the custom delimiter fixed the problem... But I need that delimiter, so now what?

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Scanner reading in doubles

    Hint: all operating systems don't use "\n" as the line separator. There's a System property that lets you get the line separator appropriate for that OS.

    db
    Last edited by DarrylBurke; 09-01-2013 at 08:30 AM.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Smile Re: Scanner reading in doubles

    GOT IT! :)

    The delimiter should be

    Java Code:
    Scanner info = new Scanner(System.in).useDelimiter(",|\n|\r");
    so that it doesn't add the return at the end of the number inputted.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Scanner reading in doubles

    I would prefer this delimiter:
    Java Code:
    ",|" + System.getProperty("line.separator")
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Scanner reading in doubles

    Ok, I see, that's better. Except, if I do that, then I run into a problem in the bigger program.

    Here is the bigger program:

    Java Code:
    import java.util.Scanner;
    
    public class mytest {
     public static void main(String[] args)  {
    	 
       Scanner info = new Scanner(System.in).useDelimiter("," + System.getProperty("line.separator"));
       // Scanner info = new Scanner(System.in).useDelimiter(",|\r\n");
       
      // Define Variables:
      
      int length = 3;     // number of people 
      
      String[] names = new String[length]; 
      double[] ages = new double[length]; 
      
      
      // Get User Input:
      
      System.out.println("Enter three names separated by commas.");
      
      for(int i=0; i<length; i++){  
    	  names[i] = info.next();	 	  
      }
      
      System.out.println("Enter their ages, respectively, separated by commas.");
      
      for(int i=0; i<length; i++){
    	  ages[i] = info.nextDouble();	  
      }
      
      // Display Table: 
      System.out.println("\n\n");
      for(int i=0; i<length; i++){
    	  System.out.println(names[i] + "\t" + ages[i]);
    	  
      }
     }
     
     }
    If I use System.getProperty("line.separator") (as in line 6), then I never get the prompt for the ages (I seem to get stuck in the for-loop ending on line 23).

    But if instead I use the syntax on line 7, then I get both prompts and all is well.
    Last edited by lyss13; 09-01-2013 at 09:11 AM.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Scanner reading in doubles

    Quote Originally Posted by lyss13 View Post
    If I use System.getProperty("line.separator") (as in line 6), then I never get the prompt for the ages (I seem to get stuck in the for-loop ending on line 23).

    But if instead I use the syntax on line 7, then I get both prompts and all is well.
    Are you running that code from a command prompt or within an IDE?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  10. #10
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Default Re: Scanner reading in doubles

    I'm running the program in Eclipse.

    If I try to compile javac mytest.java at the command prompt, I get the error message

    'javac' is not recognized as an internal or external command, operable program or batch file.

    Note, I previously ran all this on a mac (both in Eclipse and at the terminal) and was fine; but on my new Windows 8 machine is when the problem started...
    Last edited by lyss13; 09-01-2013 at 07:38 PM.

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Scanner reading in doubles

    Quote Originally Posted by lyss13 View Post
    I'm running the program in Eclipse.
    The 'console' of an IDE may not use the system line separator. I know NetBeans on Windows doesn't.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    lyss13 is offline Member
    Join Date
    Sep 2013
    Posts
    7
    Rep Power
    0

    Thumbs up Re: Scanner reading in doubles

    Oh, my mistake! I had forgotten the OR, and had written

    Java Code:
     Scanner info = new Scanner(System.in).useDelimiter("," + System.getProperty("line.separator"));
    instead of

    Java Code:
     Scanner info = new Scanner(System.in).useDelimiter(",|" + System.getProperty("line.separator"));
    With this correction, it now works. Thank you very much!
    DarrylBurke likes this.

Similar Threads

  1. Reading a file with Scanner
    By Games2Design in forum New To Java
    Replies: 5
    Last Post: 10-04-2012, 10:10 AM
  2. File reading with Scanner
    By mik in forum New To Java
    Replies: 2
    Last Post: 08-31-2012, 09:44 AM
  3. Scanner not reading every character in a file?
    By Brandonhspace in forum New To Java
    Replies: 5
    Last Post: 06-07-2012, 07:57 AM
  4. Reading file problem using Scanner
    By nfsmwbe in forum New To Java
    Replies: 18
    Last Post: 01-04-2012, 03:26 PM
  5. Java scanner reading txt documents
    By csisdifficult in forum New To Java
    Replies: 3
    Last Post: 04-21-2011, 08:24 PM

Tags for this Thread

Posting Permissions

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