Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Post Issue with method split(java.lang.String,double,double)

    Hello, I am doing a homework assignment for computer science in which I ask the user their height (in feet and inches) and weight, then I tell them their body mass index.
    I'm having issues converting height into an easily computable form.
    Right now I'm trying to split feet from inches and then change feet to inches and add it all together for the final calculation.
    I'm using the String.split() method to try to break it apart but coming up with an error I seem unable to find any understandable help on.
    The Error is:

    BodyMassIndex.java:13: cannot find symbol
    symbol : method split(java.lang.String,double,double)
    location: class java.lang.Double
    height.split("'", ft, inches);
    ^
    1 error

    (The arrow is pointing at the period incase it looks differently once posted.)
    The following is my current code.

    Code:
    import java.util.Scanner;
    import java.lang.String;
    public class BodyMassIndex {
            public static void main (String[] args) {
                    System.out.print("How many pounds do you weight? (lbs.)");
                    Scanner keyboard = new Scanner(System.in);
                    double weight = keyboard.nextDouble();
                    double ft = 0;
                    double inches = 0;
                    System.out.print("How many inches tall are you?");
                    Double height = keyboard.nextDouble();
                    Double.toString(height);
                    height.split("'", ft, inches);
                    double tinyinches = ft * 12 + inches;
                    double totalinches = tinyinches * tinyinches;
                    double prebmi = weight / totalinches;
                    double bmi = prebmi * 703;
            String health = "zero";
            if (bmi >= 30.0) {health = "Obese";}
            if (bmi >= 25.0 && bmi <=29.9) {health = "Overweight";}
            if (bmi >= 18.5 && bmi <=24.9) {health = "Normal";}
            if (bmi <= 18.4) {health = "Underweight";}
            System.out.printf("Your Body Mass Index is %.1f and you are %s", bmi, health);
            }
    }
    Thank you for your help in advance.
    Last edited by ICollectTheDead; 10-13-2011 at 12:11 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You cannot use split on a double. Nor can you enter a double with a ' in it, the next double will only go to that point and not beyond. Capture it using keyboard.nextLine instead, and use Double.parseDouble to create a double after you've split it up and calculated it.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, tried to follow your instructions Fou-lu, but I am far too inexperienced and have VERY little idea what I'm doing. I'm struggling to use Double.parseDouble() but I really don't know how to use it. I don't know how to seperate and then pull my integers now that I've removed Double height = keyboard.nextDouble();
    Double.toString(height);
    height.split("'", ft, inches);

    with
    String height = keyboard.next();
    String[] newheight = height.split("'");
    Double.parseDouble(newheight);

    Is there perhaps a simpler way to take 5'10" or 7'4" and convert them to straight inches that I've overlooked?

    Code:
    import java.util.Scanner;
    import java.lang.String;
    public class BodyMassIndex {
            public static void main (String[] args) {
                    System.out.print("How many pounds do you weight? (lbs.)");
                    Scanner keyboard = new Scanner(System.in);
                    double weight = keyboard.nextDouble();
                    double ft = 0;
                    double inches = 0;
                    double tall = 0;
                    System.out.print("How many inches tall are you?");
                    String height = keyboard.next();
                    String[] newheight = height.split("'");
                    Double.parseDouble(newheight);
                    double tinyinches = ft * 12 + inches;
                    double totalinches = tinyinches * tinyinches;
                    double prebmi = weight / totalinches;
                    double bmi = prebmi * 703;
            String health = "zero";
            if (bmi >= 30.0) {health = "Obese";}
            if (bmi >= 25.0 && bmi <=29.9) {health = "Overweight";}
            if (bmi >= 18.5 && bmi <=24.9) {health = "Normal";}
            if (bmi <= 18.4) {health = "Underweight";}
            System.out.printf("Your Body Mass Index is %.1f and you are %s", bmi, health);
            }
    }

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You need to calculate it. newHeight is an array of string, you need to convert it to an array of double. Actually, an array of int would work just fine, or you can just add it up since each part isn't really relevant:
    PHP Code:
    double dHeight 0.0;
    dHeight += Double.parseDouble(newHeight[0]) * 12;
    dHeight += Double.parseDouble(newHeight[1]); 

  • Users who have thanked Fou-Lu for this post:

    ICollectTheDead (10-13-2011)

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Alright, Arrays of double.
    That worked excellently, Thank you Fou-Lu.
    You have been a great deal of help in what I am sure was a frustrating situation


  •  

    Tags for this Thread

    Posting Permissions

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