Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2010
    Posts
    22
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    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

Similar Threads

  1. Is this book enough?
    By dushmantha.e in forum Java Certification
    Replies: 3
    Last Post: 04-06-2010, 04:13 AM
  2. Book suggestions
    By Lil_Aziz1 in forum Java Applets
    Replies: 3
    Last Post: 01-04-2010, 03:38 AM
  3. Looking for the best Book
    By sirge in forum Forum Lobby
    Replies: 7
    Last Post: 12-24-2009, 05:08 PM
  4. best book for jsf
    By raj17 in forum JavaServer Faces (JSF)
    Replies: 8
    Last Post: 08-04-2009, 09:08 AM
  5. need a book
    By hno2005 in forum NetBeans
    Replies: 2
    Last Post: 03-20-2009, 11:57 AM

Posting Permissions

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