Results 1 to 3 of 3
  1. #1
    Hunkpapa is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default [SOLVED] Extracting Tokens Help

    Hello. I'm stuck on something and could use some help...

    I'm trying to gather numbers from a user and provide a sum, using a StringTokenizer class to extract the input. I've written the below code which compiles without incident, but crashes when I run it with the following errors:

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1,2,3"
    at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
    at java.lang.Double.parseDouble(Double.java:482)
    at SumOfNumbers.main(SumOfNumbers.java:24)


    Can someone take a look and help me get back on track? Thanks.

    Java Code:
    import javax.swing.JOptionPane;
    import java.util.StringTokenizer;
    
    public class SumOfNumbers
    {
    	public static void main(String[] args)
    	{
    		String input;			// User input
    		double sum = 0.0;		// Accumulator
    		
    		// Get a list of numbers.
    		input = JOptionPane.showInputDialog("Enter a series of " + 
    		                      "numbers separated only by commas: ");
    		
    		
    		// Convert string to numeric data and assign to variable.							 
    		sum = Double.parseDouble(input);
    		
    		// Create a StringTokenizer object.
    		StringTokenizer strtok = new StringTokenizer(input, ",");
    		
    		// Get the numbers and sum them.
    		while (strtok.hasMoreTokens())
    		{
    			input = strtok.nextToken();
    			sum += Double.parseDouble(input);
    			sum++;
    		}
    		
    		// Display the sum.
    		JOptionPane.showMessageDialog(null, "The sum of those " + 
    		                              "numbers is " + sum);
    
    		// Exit the applicaton.
    		System.exit(0);
    	}	
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    I assume that this is the statement at line 24:
    sum = Double.parseDouble(input);

    What does the API doc say for that method? IF it were smart enough to scanout the separate digits separated by commas,what would it do with all of them? It only returns a single value.
    What happens if you take that statement out?
    Why do you have that line there if you don't expect a double string?
    If you are going to read in a String of digits sep by commas, then you must scan out the digits one by one before converting them to double.

  3. #3
    Hunkpapa is offline Member
    Join Date
    Oct 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    What happens if you take that statement out?
    Umm, it works perfectly. Thanks! Appreciate the help.

Similar Threads

  1. Extracting JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 09:17 AM
  2. Getting tokens using Scanner class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-05-2008, 09:11 AM
  3. tokens
    By Gilgamesh in forum New To Java
    Replies: 5
    Last Post: 12-02-2007, 11:30 PM
  4. How to use StringTokenizer for multiple tokens
    By javaplus in forum New To Java
    Replies: 2
    Last Post: 11-29-2007, 09:38 AM
  5. tokens
    By Gilgamesh in forum New To Java
    Replies: 3
    Last Post: 11-25-2007, 02: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
  •