Results 1 to 9 of 9
  1. #1
    jjitss is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Question Double with Scanner

    Hi,

    I have a problem with loading a double with a Scanner..

    Scanner content:

    sometext 0.4
    someothertext 0.3
    text2 12.3

    My code:

    Java Code:
    Scanner input = new Scanner(file);
    input.useDelimiter("");
    
    <Code to read the text and space>
    
    //To here, everything goes right!
    
    double number = 0;
    while (input.hasNextDouble()) {
        number += input.nextDouble();
    }
    I want the whole number '0.3' into the double number? How do I get to do that? Now stops the while loop because there is a point in it..

    Thanks!

    Jjitss
    Last edited by jjitss; 02-10-2012 at 11:18 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Double with Scanner

    Java Code:
    		Scanner input = new Scanner(file).useLocale(Locale.ENGLISH);
    		double number = 0;
    		while (input.hasNext()) {
    			if(input.hasNextDouble()){
    				number += input.nextDouble();
    			}else{
    				input.next();
    			}
    		}

  3. #3
    jjitss is offline Member
    Join Date
    Nov 2011
    Posts
    5
    Rep Power
    0

    Default Re: Double with Scanner

    Thanks you for answering, but here comes another problem...

    In the first part, I used a delimeter statement, to read Chars one by one (that's how I programmed the part of 'reading' the text)

    Java Code:
    Scanner input = new Scanner(file);
    input.useDelimiter("");
     
    <Code to read the text and space>
    This delimeter-statement does not coincide with the code to read the double?! Are their better posibilities to read the text? Or is it better to switch between different delimeter-options (How?)?

    Thanks! jjitss

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

    Default Re: Double with Scanner

    The question is why you
    used a delimeter statement, to read Chars one by one
    As for the how, you can answer that for yourself by reading the API for Scanner.

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

  5. #5
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Double with Scanner

    how about:

    Java Code:
    Scanner input = new Scanner(file);
     
    double number = 0;
    while (input.hasNextLine()) {
        number += Double.valueOf(input.split(" ")[1]);
    }
    Note: I didn't test the code

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Double with Scanner

    @anoorally
    I didn't test the code
    Why not test and be sure that the code you post is correct?

  7. #7
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Double with Scanner

    here is the tested and working code with the same inputs in the file:
    Java Code:
    	public static void main(String[] args) throws FileNotFoundException {
    		Scanner input = new Scanner(new File("myFile.txt"));
    		  
    		double number = 0;
    		while (input.hasNextLine()) {
    		    number += Double.valueOf(input.nextLine().split(" ")[1]);
    		}
    		System.out.print(number);
    	}
    output is 13.0 :P

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Double with Scanner

    How about adding some comments to explain what you are doing and why. Explain what the input file looks like and why [1]

  9. #9
    anoorally is offline Member
    Join Date
    Jan 2012
    Posts
    28
    Rep Power
    0

    Default Re: Double with Scanner

    Java Code:
    public static void main(String[] args) throws FileNotFoundException { // to catch error if the specified file is not found
    		Scanner input = new Scanner(new File("myFile.txt")); //reading the input file
    		
    		/* file data:
    		sometext 0.4
    		someothertext 0.3
    		text2 12.3
    		 */
    		  
    		double number = 0;//initializes the variable number to zero
    		while (input.hasNextLine()) {//while the input file has next line that can be read
    		    number += Double.valueOf(input.nextLine().split(" ")[1]);
    		    /*above line reads the next line by calling 'input.nextLine()' which returns a String as type
    		     * on which the method 'split' with a delimiter can be given as parameter (in this case space is the delimiter)
    		     * 'split' method returns an array of Strings which are separated in the original text so...
    		     * as each line in the text file has two items separated by space, the array index 0 will give the text
    		     * and array index '1' will give the double as String,
    		     * so we use the static method 'valueOf' in the class Double to convert the String to type Double
    		     * and we add the resulting Double to the variable 'number'
    		     * */
    		   
    		}
    		System.out.print(number);//we print the value that teh variable 'number' holds, in this case, its 13.0
    	}
    If anything is still not clear, do ask :P

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2012, 02:10 PM
  2. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 11:40 AM
  3. Check if double is double
    By marshalthrone in forum New To Java
    Replies: 8
    Last Post: 09-30-2009, 03:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 10:32 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 09:39 AM

Posting Permissions

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