Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 16 of 16

Thread: Player input not working

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Player input not working

    Hello!

    I'm making a BMI calculator but it's not working when I try to type in my height and weight.

    this is the error I get
    Exception in thread "main" java.lang.NumberFormatException: For input string: "1.72 59"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatSt ring(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(Floa tingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:549)
    at com.company.Main.main(Main.java:13)
    String s = JOptionPane.showInputDialog("height and weight");
            int space = weight.indexOf(' ');
            int bar = height.indexOf(' ');
            String first = height.substring(0, space);
            String second = height.substring(space+1);
            String third = weight.substring(0, bar);
            String fourth = weight.substring(bar+2);
            double l = Double.parseDouble(s); //L is for height
            double b = Double.parseDouble(s); //b is for weight
            double bmi = Math.sqrt(l * l / b);
            JOptionPane.showMessageDialog(null, "your bmi" + bmi);

    Thank you in advance
    Last edited by northernlightifly; October 31st, 2019 at 09:08 AM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    Exception in thread "main" java.lang.NumberFormatException: For input string: "1.72 59"
    The String passed to the parseDouble method is not a valid double value. It looks like there are two values there separated by a space.
    To get at the separate values in the String, read the user's response into a String and split it using the String class's split method.
    Then work on each part separately.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    Thank you for replying! If you mean by slipt it into an array I'm not allowed to do that or use regex.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    Another option - have the user only enter one number at a time.
    Otherwise describe how your instructor expects the program to get the user's input.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    Quote Originally Posted by Norm View Post
    Another option - have the user only enter one number at a time.
    Otherwise describe how your instructor expects the program to get the user's input.
    He said to use substring.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    Ok. To use substring you need to know what columns to access. Did he say how to determine what columns to use with the substring method?
    There are several useful methods in the String class:
    indexOf to find the first space
    lastIndexOf to find the last space

    I assume there could be more than one space between the numbers.

    Read the API doc for the class to see what they can do.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    Quote Originally Posted by Norm View Post
    Ok. To use substring you need to know what columns to access. Did he say how to determine what columns to use with the substring method?
    There are several useful methods in the String class:
    indexOf to find the first space
    lastIndexOf to find the last space

    I assume there could be more than one space between the numbers.

    Read the API doc for the class to see what they can do.

    something about fetching the words height and weight for a substring?
    for example:
    int space = weight.indexOf(' ');
    int bar = height.indexOf(' ');

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    Does it work now?

    If not, please post any further questions or problems.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    no it doesn't work >.<

    here's the error: "Exception in thread "main" java.lang.NumberFormatException: For input string: "59 1.72"
    at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatSt ring(FloatingDecimal.java:2054)
    at java.base/jdk.internal.math.FloatingDecimal.parseDouble(Floa tingDecimal.java:110)
    at java.base/java.lang.Double.parseDouble(Double.java:549)
    at com.company.Main.main(Main.java:30)"

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    "Exception in thread "main" java.lang.NumberFormatException: For input string: "59 1.72"

    That looks like the original problem.

    Did you use substring to get the two numbers from the input String?
    Add some print statements that print out the two String values so you can see if the substring code was working.
    When the two extracted Strings look good then try passing each one of them to the parseDouble method one at a time.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    Quote Originally Posted by Norm View Post
    "Exception in thread "main" java.lang.NumberFormatException: For input string: "59 1.72"

    That looks like the original problem.

    Did you use substring to get the two numbers from the input String?
    Add some print statements that print out the two String values so you can see if the substring code was working.
    When the two extracted Strings look good then try passing each one of them to the parseDouble method one at a time.
    something like this?

    String first = height.substring(0, space);

    I get this error "Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 0, end -1, length 5"

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    What is in the String: height? What is the value in space?
    Print them out so you can see what their values are.

    I suggest that you write a small test program with these 5 lines:
    Define a String with two numbers: "333 444"
    get the index of the space between the two numbers into an int
    get the number before the space into a String using substring method
    get the number after the space into a String using substring method
    print out the value of the index, the value of the first String and the value of the second String
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    My mad I forgot to add numbers based on their position like this
    String first = height.substring(0, 5);

    String third = weight.substring(0, 4);

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    Did you try the test program I suggested?
    That is how I check out new classes and methods - write a small test print and print out the results.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Oct 2019
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Player input not working

    No but it helped me figure it out so it can write out height and weight in the terminal but now I have to make a string with both weight and height

    int space = s.indexOf(' ');
    int bar = s.lastIndexOf(' ');
    String first = s.substring(space-2, space);

    String third = s.substring(space, space+5);
    System.out.println(third);

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Player input not working

    It would be useful if you made the test program and worked with it to learn how to use indexOf and substring. The posted code does not show what is in the String s or print out the value of space or bar or first or third so I can't say if anything is wrong with the code.

    One problem I see is space-2? Why the value -2? What if the number has a different number of digits?
    Same with space+5 What if the value is longer or shorter than expected? substring with a single value will return the String from that value to the end of the String.

    Make the simple test program, compile it and execute it. If you have any questions about what it does, post the code and the full contents of the console from when you execute it. Add some comments asking your questions.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Automatic audio file player not working
    By summit45 in forum What's Wrong With My Code?
    Replies: 26
    Last Post: August 29th, 2013, 12:39 AM
  2. [SOLVED] Collsion Not Working Between Player And Enmey ...Java
    By hwoarang69 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 28th, 2012, 06:47 AM
  3. How can i make my Player attack another Player?
    By Graser in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 31st, 2012, 05:01 PM
  4. java game bullets hit player 1 but not player 2
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 22nd, 2011, 08:19 AM
  5. java game, both players move for player 2 but not player 1
    By ajakking789 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 21st, 2011, 12:52 PM