Results 1 to 4 of 4
  1. #1
    woodsie2523 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    5

    Default

    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 :)

  3. #3
    woodsie2523 is offline Member
    Join Date
    Oct 2010
    Posts
    2
    Rep Power
    0

    Default

    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!

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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

  1. Replies: 4
    Last Post: 02-22-2009, 05:31 PM
  2. how to Parse int to a string variable
    By raj reddy in forum Java Servlet
    Replies: 10
    Last Post: 01-09-2009, 08:41 PM
  3. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 03:46 AM
  4. how to Parse int to a string variable (pls hlp)
    By raj reddy in forum Threads and Synchronization
    Replies: 5
    Last Post: 06-10-2008, 07:32 AM
  5. How to parse String effectively based on a dilimiter
    By raghu408 in forum Advanced Java
    Replies: 26
    Last Post: 04-10-2008, 11:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •