Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-03-2008, 11:26 PM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
Hunkpapa is on a distinguished road
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.

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);
	}	
}
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-03-2008, 11:48 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: Heredia, Costa Rica
Posts: 2,225
Rep Power: 4
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-04-2008, 01:17 AM
Member
 
Join Date: Oct 2008
Posts: 2
Rep Power: 0
Hunkpapa is on a distinguished road
Default
Originally Posted by Norm View Post
What happens if you take that statement out?
Umm, it works perfectly. Thanks! Appreciate the help.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting JAR file Java Tip Java Tips 0 02-08-2008 09:17 AM
Getting tokens using Scanner class Java Tip Java Tips 0 02-05-2008 09:11 AM
tokens Gilgamesh New To Java 5 12-02-2007 11:30 PM
How to use StringTokenizer for multiple tokens javaplus New To Java 2 11-29-2007 09:38 AM
tokens Gilgamesh New To Java 3 11-25-2007 02:39 AM


All times are GMT +2. The time now is 01:47 AM.



VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org