Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-12-2011, 12:45 AM   PM User | #1
ICollectTheDead
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
ICollectTheDead is an unknown quantity at this point
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..
ICollectTheDead is offline   Reply With Quote
Old 10-12-2011, 03:15 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 10-12-2011, 05:17 AM   PM User | #3
ICollectTheDead
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
ICollectTheDead is an unknown quantity at this point
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);
        }
}
ICollectTheDead is offline   Reply With Quote
Old 10-12-2011, 02:37 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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]); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
ICollectTheDead (10-13-2011)
Old 10-13-2011, 12:10 AM   PM User | #5
ICollectTheDead
New to the CF scene

 
Join Date: Oct 2011
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
ICollectTheDead is an unknown quantity at this point
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
ICollectTheDead is offline   Reply With Quote
Reply

Bookmarks

Tags
beginner, double, split, string

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:46 AM.


Advertisement
Log in to turn off these ads.