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.


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

Results 1 to 3 of 3

Thread: boolean method not checking to see if within given tolerance to be true.

  1. #1
    Junior Member
    Join Date
    May 2012
    Thanked 0 Times in 0 Posts

    Default boolean method not checking to see if within given tolerance to be true.

    public boolean checkAnswer(String response) {
    		String Answer = "";
    		double NumAnswer = Double.parseDouble(response);
    		double CorrectAnswer = Double.parseDouble(super.getAnswer());
    		if (NumAnswer == CorrectAnswer - tolerance
    				|| NumAnswer == CorrectAnswer + tolerance
    				|| NumAnswer == CorrectAnswer) {
    			Answer = super.getAnswer();
    		return response.equals(Answer);

    This method is within a subclass for a math question. checkAnswer is supposed to take an answer in string form, converts it to a
    double, get the correct answer in string form from the super class Question, converts that to a
    double, and return true if NumAnswer is within the indicated tolerance to the CorrectAnswer. The tolerance i set up to be a private double and constructed to of them, one with the set tolerance of 0.01 and the other that accepts a double parameter.

    The method works in returning true or false but I can't seem to understand why my if statement doesn't return true when the given string response falls within the tolerance. I have tried giving it an else statement as well but that also seems to not work.

  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Thanked 206 Times in 182 Posts

    Default Re: boolean method not checking to see if within given tolerance to be true.

    You should follow Java coding conventions and begin variables with lowercase letters like answer, numAnswer etc: your code will be more readable that way.

    If you are checking for a result *within* some tolerance shouldn't you be using < and > somewhere? The result should be bigger than some lower limit and less than some upper one.

  3. #3
    Forum VIP
    Join Date
    Jul 2010
    Thanked 329 Times in 305 Posts

    Default Re: boolean method not checking to see if within given tolerance to be true.

    pbrockway2 is correct. You should be checking if NumAnswer is greater than or equal to CorrectAnswer-tolerance AND NumAnswer is less than or equal to CorrectAnswer+tolerance.

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. I need a little help with a boolean method.
    By bankston13 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 30th, 2012, 08:02 PM
  2. Setting Boolean to 'True' after X amount of Seconds?
    By uhKenKaniff in forum Java Theory & Questions
    Replies: 1
    Last Post: December 12th, 2011, 07:29 PM
  3. [SOLVED] isPrime Boolean Method returns true for squares of primes
    By Staticity in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 29th, 2011, 11:46 PM
  4. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  5. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM

Tags for this Thread