Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What's wrong with my code?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default What's wrong with my code?

    import java.util.Scanner;

    public class Lab8_Ex2
    {
    public static void main(String[] args)
    {
    int age;
    int beatsPerMinute;


    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please enter your age. ");
    age = keyboard.nextInt();

    System.out.print("Please enter the number of heartbeats " +
    "counted in one minute. ");
    beatsPerMinute = keyboard.nextInt();

    heartRate = targetHeartRate(heartRate);

    if(heartRate == true)
    {
    System.out.print("You are in your target heart rate.");
    }
    else
    {
    System.out.print("You are not in your target heart rate.");
    }

    }

    public static boolean targetHeartRate(int age, int beatsPerMinute)
    {
    int maxHeartRate = 0;
    double targetRate = 0.0;
    boolean heartRate;

    maxHeartRate = 220 - age;
    targetRate = maxHeartRate * .65;

    if(targetRate == beatsPerMinute)
    {
    heartRate = true;
    }

    else
    {
    heartRate = false;
    }
    return heartRate;
    }
    }[/CODE]

    What did I do wrong?


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    sorry I pasted it wrong. here it is!

     
     
    import java.util.Scanner;
     
    public class Lab8_Ex2
    {
    	public static void main(String[] args)
    	{
    		int age;
    		int beatsPerMinute;
     
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("Please enter your age. ");
    		age = keyboard.nextInt();
     
    		System.out.print("Please enter the number of heartbeats " +
    			"counted in one minute. ");
    		beatsPerMinute = keyboard.nextInt();
     
    		heartRate = targetHeartRate(heartRate);
     
    		if(heartRate == true)
    		{
    			System.out.print("You are in your target heart rate.");
    		}
    		else
    		{
    			System.out.print("You are not in your target heart rate.");
    		}
     
    	}
     
    	public static boolean targetHeartRate(int age, int beatsPerMinute)
    	{
    		int maxHeartRate = 0;
    		double targetRate = 0.0;
    		boolean heartRate;
     
    		maxHeartRate = 220 - age; 
    		targetRate = maxHeartRate * .65;
     
    		if(targetRate == beatsPerMinute)
    		{
    			heartRate = true;
    		}
     
    		else
    		{
    			heartRate = false;
    		}
    		return heartRate;
    	}
    }

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: What's wrong with my code?

    Quote Originally Posted by mjballa View Post
    What did I do wrong?
    How would we know? Feel like providing more details? Such as error messages. Output you get, output you want and explain the difference.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    Lab8_Ex2.java:22: cannot find symbol
    symbol : variable heartRate
    location: class Lab8_Ex2
    targetHeartRate(heartRate);
    ^


    Lab8_Ex2.java:24: cannot find symbol
    symbol : variable heartRate
    location: class Lab8_Ex2
    if(heartRate == true)
    ^


    those are the two errors I'm getting.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default Re: What's wrong with my code?

    The error message is clear. The compiler cannot find the declaration for the heartRate variable.
    public static void main(String[] args)
    {
        int age;
        int beatsPerMinute;
     
        Scanner keyboard = new Scanner(System.in);
     
        System.out.print("Please enter your age. ");
        age = keyboard.nextInt();
     
        System.out.print("Please enter the number of heartbeats " + "counted in one minute. ");
        beatsPerMinute = keyboard.nextInt();
     
        heartRate = targetHeartRate(heartRate);
     
        if(heartRate == true)
        {
            System.out.print("You are in your target heart rate.");
        }
        else
        {
            System.out.print("You are not in your target heart rate.");
        }
    }
    Where in that code have you declared the variable?
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

     
     
    import java.util.Scanner;
     
    public class Lab8_Ex2
    {
    	public static void main(String[] args)
    	{
    		int age;
    		int beatsPerMinute;
    		boolean heartRate;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("Please enter your age. ");
    		age = keyboard.nextInt();
     
    		System.out.print("Please enter the number of heartbeats " +
    			"counted in one minute. ");
    		beatsPerMinute = keyboard.nextInt();
     
    		heartRate = targetHeartRate(heartRate);
     
    		if(heartRate == true)
    		{
    			System.out.print("You are in your target heart rate.");
    		}
    		else
    		{
    			System.out.print("You are not in your target heart rate.");
    		}
     
    	}
     
    	public static boolean targetHeartRate(int age, int beatsPerMinute)
    	{
    		int maxHeartRate = 0;
    		double targetRate = 0.0;
    		boolean heartRate;
     
    		maxHeartRate = 220 - age; 
    		targetRate = maxHeartRate * .65;
     
    		if(targetRate == beatsPerMinute)
    		{
    			heartRate = true;
    		}
     
    		else
    		{
    			heartRate = false;
    		}
    		return heartRate;
    	}
    }

    I added the initialization.
    but now I am getting this error when attempting to compile:

    Lab8_Ex2.java:23: targetHeartRate(int,int) in Lab8_Ex2 cannot be applied to (boolean)
    heartRate = targetHeartRate(heartRate);

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What's wrong with my code?

    heartRate = targetHeartRate(heartRate);
    Check the definition of targetHeartRate(). It's not accepting boolean anywhere. You must be able to trace the error on your own.
    targetHeartRate() is accepting two integers, but you are passing it boolean. What i guess is you misunderstood the parameters and return type.
    It's returning boolean but accepting two integers.

  8. #8
    Junior Member
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What's wrong with my code?

    I made some changes but I don't know if this is what you want as the result
     
    import java.util.Scanner;
     
    public class Lab8_Ex2
    {
    	private static boolean heartRate;
    	public static void main(String[] args)
    	{
    		int age;
    		int beatsPerMinute;
     
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.print("Please enter your age. ");
    		age = keyboard.nextInt();
     
    		System.out.print("Please enter the number of heartbeats " +
    			"counted in one minute. ");
    		beatsPerMinute = keyboard.nextInt();
     
    		heartRate = targetHeartRate(age, beatsPerMinute);
     
    		if(heartRate == true)
    		{
    			System.out.print("You are in your target heart rate.");
    		}
    		else
    		{
    			System.out.print("You are not in your target heart rate.");
    		}
     
    	}
     
    	public static boolean targetHeartRate(int age, int beatsPerMinute)
    	{
    		int maxHeartRate = 0;
    		double targetRate = 0.0;
     
     
    		maxHeartRate = 220 - age; 
    		targetRate = maxHeartRate * .65;
     
    		if(targetRate == beatsPerMinute)
    		{
    			heartRate = true;
    		}
     
    		else
    		{
    			heartRate = false;
    		}
    		return heartRate;
    	}
    }

  9. #9
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,102
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: What's wrong with my code?

    Quote Originally Posted by grimrader22 View Post
    ...but I don't know if this is what you want as the result
    Which is exactly why we discourage spoonfeeding. Please read the forum rules and the following article
    The Problem with Spoon-feeding

Similar Threads

  1. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 06:28 PM
  2. what is wrong with my code?
    By bmb in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 7th, 2011, 09:03 AM
  3. what's wrong with this code
    By gcsekhar in forum Java Networking
    Replies: 5
    Last Post: July 21st, 2011, 06:01 AM
  4. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 02:13 PM
  5. Wrong code
    By whattheeff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 4th, 2011, 04:30 PM