Results 1 to 2 of 2
Thread: question in the book
- 09-29-2010, 08:06 AM #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?
- 09-29-2010, 09:59 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
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 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
Similar Threads
-
Is this book enough?
By dushmantha.e in forum Java CertificationReplies: 3Last Post: 04-06-2010, 04:13 AM -
Book suggestions
By Lil_Aziz1 in forum Java AppletsReplies: 3Last Post: 01-04-2010, 03:38 AM -
Looking for the best Book
By sirge in forum Forum LobbyReplies: 7Last Post: 12-24-2009, 05:08 PM -
best book for jsf
By raj17 in forum JavaServer Faces (JSF)Replies: 8Last Post: 08-04-2009, 09:08 AM -
need a book
By hno2005 in forum NetBeansReplies: 2Last Post: 03-20-2009, 11:57 AM
Bookmarks