Results 1 to 4 of 4
- 10-25-2010, 10:23 PM #1
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
parse string into 2 double variables
How can I parse a string into 2 double variables?
I am using JOptionpane to ask the user to enter 2 numbers separated by a space.
I want to put the first number into x and the second into y and just ignore the white space in between. I tried this code but it throws an error if I enter 2 numbers separated by a space.
inputMessage = "Enter 2 numbers separated by a space.";
inputString = JOptionPane.showInputDialog(inputMessage);
x = Double.parseDouble(inputString);
y = Double.parseDouble(inputString);
I can write a class doing this using scanner console but I want to do it with JOptionpane and all in one text box.
(Error message is Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1"
at sun.misc.FloatingDecimal.readJavaFormatString(Floa tingDecimal.java:1224)
at java.lang.Double.parseDouble(Double.java:510)
at TestJOtionStringParse.main(TestJOtionStringParse.j ava:31)
thanks for any suggestion.
- 10-25-2010, 10:36 PM #2
I would use the String Split method for this: String (Java 2 Platform SE 5.0)
And if you don't understand the usage, you could also look up String Tokenizer.Sincerely, Joshua Green
Please REP if I help :)
- 10-26-2010, 11:42 AM #3
Member
- Join Date
- Oct 2010
- Posts
- 2
- Rep Power
- 0
I am going to have to play with split and tokenizer to be able to figure out how to make one of them do what I want. Thanks for the help!
- 10-26-2010, 01:46 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
No need for that, you can get the input as a String, pass it into a Scanner, and call Scanners nextDouble() method twice. Scanner is smart enough to recognize the space between numbers as a delimiter, so no splitting or tokenizing is necesarry.
Ever seen a dog chase its tail? Now that's an infinite loop.
Similar Threads
-
Parse XMLfile as String using SAX parser
By Jack in forum XMLReplies: 4Last Post: 02-22-2009, 04:31 PM -
how to Parse int to a string variable
By raj reddy in forum Java ServletReplies: 10Last Post: 01-09-2009, 07:41 PM -
convert String to Double
By azurovyhrosik in forum CLDC and MIDPReplies: 5Last Post: 10-22-2008, 02:46 AM -
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 06:32 AM -
How to parse String effectively based on a dilimiter
By raghu408 in forum Advanced JavaReplies: 26Last Post: 04-10-2008, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks