[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 PBThis post has been edited by PennyBoki: 3 Feb, 2008 - 05:57 PM