Click to See Complete Forum and Search --> : Reading in strings and integers...BufferedReader???


Jane
October 6th, 1999, 10:21 PM
please help me. I'm not sure how to read in the strings and integers. I'd like another class named Reader, but i'm not sure of the correct syntax. Below is what the program is supposed to do:

package simpleIO;

import java.io.*;
import java.text.*;

public class BodyMass
{
public static void main (String[]args)
{
person girl=new person ();
girl.getData();
girl.displayData();
girl.displayBMI();
}
}//class BodyMass

public class person
{
private String name;
private double height, weight, heightFeet, heightInches;
public void getData();
}

class Reader ?????
{

BufferedReader stdin=new BufferedReader(new InputStreamReader(System.in));
???????????????????
}//class Reader

public void getData();
{
System.out.print("Name:");
name=Reader.readString();
System.out.print("Height in inches:");
heightInches=Reader.readheightInches;
System.out.print("Weight:");
weight=Reader.readweight;
}
}//void getData();

public void displayData();
{
System.out.println("Your name is " + name);
System.out.println("Your height is " + height);
System.out.println("Your weight is " + weight);
}
}//displayData

public void displayBMI();
{
BMI=(weight * 705)/heightInches * heightInches;

System.out.printline("Your body mass index is " + BMI);
}
}//displayBMI

==================
Java application that has two classes, one containing the main method that starts the action and the other that describes a person. The data for this class should be a person's name, weight in pounds and height in feet and inches. It should have a method that reads in the data from the keyboard and another that displays the information.

A third method should calculate (and return) the person's body mass index. To compute this, multiply the person's weight in pounds by 705. Then divide this result by the person's height in inches squared. For example, if the person weighs 120 pounds and is 5 feet 2 inches tall (62 inches), his or her body mass index is (120 * 705) / (62 * 62) or 22.

The main method should declare a person and then call the appropriate methods to read in and display the data. It should then display the person's body mass index.

amaresh
October 7th, 1999, 12:36 PM
Please refer the book "Core java" by Cay Horstmann and Gary Cornell and they discuss (infact have given the code) how to read the input from keyboard. You can copy that code and that will take care of getData() function.
You need to write the other functions "displayData()" and "displayBMI()" in your classes. They are fairly straight forward. All that you need to do is, create a StringBuffer and write the name,height,weight etc in the desired format. For displayBMI, all that you need to do is, to return a integer from the function that carries the BMI.
Now, coming to the errors:
(Error 1)
The first error is due to the fact that you have not provided the implementation for the function "getData()" in your "Person" class
(Error 2)
I think there is one extra parenthesis (}) in your "Reader" class.Check that
(Errors 3 & 4):
A source file cannot have more than one public classe in java. The source file must be named with it's public class name. So, remove that "public" modifier from the "Person" class declaration and name the file BodyMass.java.
This should take care of all the errors.
Let me know if this is not clear.
Thanks and Regards,
Amaresh

Sachin
October 15th, 1999, 05:47 AM
to read integer varible you can use :
int x;
x=Integer.ParseInt(in.readLine());
and you go through the book java complete reference