# Thread: question in the book

1. Member
Join Date
Apr 2010
Posts
22
Rep Power
0

## question in the book

New To Java - question in the book

please read the problem.

I have updated the code.

Java Code:
```package absolute_;
import java.util.Scanner;

public class CountingCaloriesMethods

{
Scanner Readinput = new Scanner(System.in);

public static final double ECONSTANT = 0.0175;
private double weight,sleephours,Calories,Running,Basketball,Sleeping,runninghours,basketballhours,x,y,z;

public void enterweight()
{
System.out.println("Please enter your weight in pounds");
weight = Readinput.nextDouble();
weight = weight /2.2;
System.out.printf("Your weight in kilos is" + "%6.2f",weight);
System.out.println();

}
public  void running()
{
System.out.println( "Did you run today: please answer YES OR NO");
String yesorno = Readinput.next();
String Yes = "Yes";

if (yesorno.equalsIgnoreCase(Yes))
{	System.out.println("Please enter number of hours you ran");
runninghours = Readinput.nextDouble();
x = hourstominutes(runninghours);
Running = 9;

}
else
System.out.println("please answer next question:");
}

public void basketball()
{
System.out.println( "Did you play basketball today: please answer YES OR NO");
String yesorno = Readinput.next();
String Yes = "Yes";

if (yesorno.equalsIgnoreCase(Yes))
{	System.out.println("Please enter number of hours you played basketball");
basketballhours = Readinput.nextDouble();
y = hourstominutes(basketballhours);
Basketball = 8;
}
else
System.out.println("please answer next question:");

}

public void sleeping()
{
System.out.println( "Did you sleep today: please answer YES OR NO");
String yesorno = Readinput.next();
String Yes = "Yes";

if (yesorno.equalsIgnoreCase(Yes))
{	System.out.println("Please enter number of hours you slept");
sleephours = Readinput.nextDouble();
z = hourstominutes(sleephours);
Sleeping = 0.9;
}
else
System.out.println("Thank you");

}

public double hourstominutes(double hours)
{
hours = hours * 60;
return hours;

}

public void calculation()
{

double totalminutes = x + y + z;
Calories = (totalminutes) * (ECONSTANT) * (Running + Basketball + Sleeping)* weight;
System.out.printf("Total calories burned: " + "%6.2f",Calories );
System.out.println("Total minutes = " + totalminutes);
System.out.println("ECONSTANT = " + ECONSTANT);
System.out.println("Running = " + Running);
System.out.println("Basketball = " + Basketball);
System.out.println("Sleeping =" + Sleeping);
System.out.println("weight= " + weight);

System.exit(0);```

Java Code:
```package absolute_;

public class CountingCalories
{

public static void main(String[] args)

{
CountingCaloriesMethods Obj = new CountingCaloriesMethods();
Obj.enterweight();
Obj.running();
Obj.basketball();
Obj.sleeping();
Obj.calculation();

}

}```

my question is code seems to work ok.. but i have issues about input... if i enter "any" string or two seperate words it tends to skip steps...please help me how can i improve my code for console input?

2. Note that the nextDouble() method doesn't read newline characters; e.g. if you want to read two double values and you type:

Java Code:
```1.23
4.56```
The inputbuffer contains the characters 1.23<enter>4.56<enter> the first nextDouble() call reads and converts the first double and leaves the following characters in the inputbuffer: <enter>4.56<enter>. The next nextDouble() call 'sees' the <enter> character in the inputbuffer and decides that it can't read a double value.

The cure is simple: put a spurious nextLine() call in the code after you have read a double with nextDouble(); the nextLine() method will remove the <enter> character from the inputbuffer.

kind regards,

Jos

#### Posting Permissions

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