Welcome to Dream.In.Code
Become a Java Expert!

Join 150,357 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,884 people online right now. Registration is fast and FREE... Join Now!




White Space

 
Reply to this topicStart new topic

White Space, Entering more than one number

sp3shk
3 Feb, 2008 - 05:34 PM
Post #1

New D.I.C Head
*

Joined: 3 Feb, 2008
Posts: 2

[size=3]

Hi, I'm having a little problem, I have to create a program in which the user enters in three numbers with any amount of spaces between each number, capture them, then display them and add them up, I have it so that if you enter only one number it works fine, but if I enter more than one I get an error which is

"Exception in thread "main" java.lang.NumberFormatException: For input string: "1 2 3"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:456)
at java.lang.Integer.parseInt(Integer.java:497)
at ThreeNumbers.main(ThreeNumbers.java:19)"

I have no idea what that means, can anyone help me so that I can grab all three numbers, my code goes as such.

CODE

/**
* @(#)ThreeNumbers.java
*
* ThreeNumbers application
*
* @author
* @version 1.00 2008/2/1
*/
import java.util.*;
import javax.swing.*;

public class ThreeNumbers
{
    public static void main(String[] args)
        {
            String firstString;
            firstString =     
                JOptionPane.showInputDialog(null, "Enter three numbers with any amount of spaces between each");
              int firstNum = Integer.parseInt(firstString.trim());
              int secondNum = Integer.parseInt(firstString.trim());
              int thirdNum = Integer.parseInt(firstString.trim());
            
            int total = firstNum + secondNum + thirdNum;
            JOptionPane.showMessageDialog(null, "The original data was " + firstString + ".\n"
            + "You have entered " + firstNum + ", " + secondNum + " and " + thirdNum + ".\n"
            + "The sum of the numbers is " + (total) + ".");
        
            System.exit(0);
        }
}
~edit: added code tags PB

This post has been edited by PennyBoki: 3 Feb, 2008 - 05:57 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: White Space
3 Feb, 2008 - 06:22 PM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi, please next time when posting code code.gif Thanks.

You get a number format exception because you try to make an int by parsing a string that contains chars different than numbers.

Check what the trim() function does before you parse the string try this:

System.out.println(firstString.trim());

Now, one way to solve this is using the StringTokenizer class.

So give it a try.
User is offlineProfile CardPM
+Quote Post

GWatt
RE: White Space
3 Feb, 2008 - 07:26 PM
Post #3

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
Instead of using the tokenizer class, you can use the .split() method.
int nums = firstString.split("\\s");
"\\s" means match any whitespace.
User is offlineProfile CardPM
+Quote Post

sp3shk
RE: White Space
3 Feb, 2008 - 09:11 PM
Post #4

New D.I.C Head
*

Joined: 3 Feb, 2008
Posts: 2

Thanks for the help, what import would i need to use the .split class? and also this is for an JOptionPane type program
User is offlineProfile CardPM
+Quote Post

GWatt
RE: White Space
3 Feb, 2008 - 09:36 PM
Post #5

human inside
Group Icon

Joined: 1 Dec, 2005
Posts: 2,360



Thanked: 31 times
Dream Kudos: 500
My Contributions
QUOTE(GWatt @ 3 Feb, 2008 - 10:26 PM) *

Instead of using the tokenizer class, you can use the .split() method.
int nums = firstString.split("\\s");
"\\s" means match any whitespace.



QUOTE(sp3shk @ 4 Feb, 2008 - 12:11 AM) *

Thanks for the help, what import would i need to use the .split class? and also this is for an JOptionPane type program


I called it the split METHOD. Not class.

You call it like any other non-static method.
User is offlineProfile CardPM
+Quote Post

bhandari
RE: White Space
4 Feb, 2008 - 05:07 AM
Post #6

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
You will call the split() method on the string variable 'firstString' and it will return you a string array (one similar to that in main), then iterate the string array to get individual elements as strings.

Note:split() is a method of string class and it needs a delimiter which determines the spliting.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:06PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month