Page 1 of 2 12 LastLast
Results 1 to 20 of 28
  1. #1
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Formatting a users answer

    I am writing a simple learning program that does basic math. Everything is working as needed except for when it comes to division. I need to know if there is a way to format the users input to two decimal places and that when the program checks the users answer against the division, it gives it a yay or nay. Right now, it wants the answer out to 12 decimal places.

    So for 5/3
    I would like it to accept the answer 1.67

    not 1.666666666667

    Is there a way to do this?

    If you need more information, please let me know, I can upload a code snipet if needed as well.

    Thanks in advance!

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Formatting a users answer

    System.out.printf(). Check the https://docs.oracle.com/javase/8/doc...intStream.html for more info.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    Quote Originally Posted by jim829 View Post
    System.out.printf(). Check the https://docs.oracle.com/javase/8/doc...intStream.html for more info.

    Regards,
    Jim


    So I thought about using this, but I don't want to print out the users answer. All I would like to do is take the users answer and check it against the math.

    For example

    5/3 = 1.666666666666667

    if the user enters 1.67 as their answer I would like it to accept this as correct.


    Would it help to convert both the computer generated answer and the users answer to a string?
    Last edited by ItchyJuffoWup; 11-20-2014 at 02:06 AM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Formatting a users answer

    Sorry, I didn't read the post carefully enough. There are several ways. One way is to simply define an acceptable tolerance of their answer. So 1.67 - 1.666666... is <= .003333.... So if the absolute value of the difference is < .003 then accept the answer. Another way would be to be to use String.format() with the same printf() arguments and compare the two values as string representations.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    I was able to get it done correctly going this route. It just seems like a lot of extra stuff.

    Java Code:
    public static void division(int randNum1, int randNum2)
        {
    	char symbol = '/';
    	double user_answer, num;
    	Scanner input = new Scanner(System.in);
    	DecimalFormat df = new DecimalFormat("#.##");
    	
    
    	System.out.println("\nWhat is " + randNum1 + " " + symbol + " " + randNum2 + " ? ");
    
    	num = (double)randNum1  / (double) randNum2;
    	user_answer = input.nextDouble();
    
    	String str_num = df.format(num);
    	String str_user_answer = df.format(user_answer); 
    
    	//System.out.println("user answer is " + str_user_answer);
    	//System.out.println("str_num is " + str_num);
    
    	if (str_user_answer.equals(str_num))
    	    System.out.println("Good job!");
    
    	else 
    	    System.out.println("Sorry thats incorrect");
    Last edited by ItchyJuffoWup; 11-20-2014 at 02:45 AM.

  6. #6
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Formatting a users answer

    I would also try to make it work in the other way that Jim suggested, resorting to string comparisons is a bit of a dirty workaround IMO.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Formatting a users answer

    Quote Originally Posted by gimbal2 View Post
    I would also try to make it work in the other way that Jim suggested, resorting to string comparisons is a bit of a dirty workaround IMO.
    But that involves math, no matter how simple and that should be avoided like the plague. It is preferred to move this little problem to the String domain where it can be 'solved' (mind the quotes) with some hacking and fiddling ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Formatting a users answer

    You've given up on humanity, haven't you? ;)

    I spot a repeating pattern:

    http://www.java-forums.org/new-java/...tml#post399825
    Last edited by gimbal2; 11-20-2014 at 12:25 PM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Formatting a users answer

    Quote Originally Posted by gimbal2 View Post
    You've given up on humanity, haven't you? ;)
    Yes I have; as long as reading and math are two things that are 'not done', humanity is doomed to reinvent what has already been done many times again. We'll all end up totally silly: connected to google and stupid social meda where we consume knowledge, piecemeal, without understanding anything of it ... copy/paste/like/don't like generations ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Formatting a users answer

    The day actual knowledge is shared through social media is the day I start tolerating its existence. It would be a step up.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Formatting a users answer

    Quote Originally Posted by gimbal2 View Post
    resorting to string comparisons is a bit of a dirty workaround IMO.
    Yeah. I wish I hadn't mentioned it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Formatting a users answer

    Quote Originally Posted by jim829 View Post
    Yeah. I wish I hadn't mentioned it.

    Regards,
    Jim
    You can only show people the door, they have to be the ones to walk through it ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    So I do have a question regarding all of this.

    I understand where the string comparisons, and well the use of doubles can cause issues later on, but isn't that only the case where you don't want a rounding error, for example, banking software, where doubles aren't exact.

    Since this is just a simple learning program for basic elementary math, Rounding to a second decimal place shouldn't be an issue correct? I could understand if I were to be creating a more in-depth program that needs exact numbers rather than rounded estimates.

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Formatting a users answer

    I don't believe the issue was with the merits of rounding numbers. The issue was using a string comparison to check for valid user input instead of a small delta difference between the two numbers. You can always round to some number and do the comparison. This is faster than the string compare version. The code you see (less the print statements) does the job as does the delta version. The String.format() version and the equals() methods hide a whole lot of code that makes it less efficient.

    Java Code:
    double a = 1.67;
    double b = 5 / 3.;
    System.out.println(a == b); // false
    b = (int) ((b * 100) + .5) / 100.; // round to two decimal places.
    System.out.println(a == b); // true
    The above won't work for all values (if a was set to 1.667 for instance). A defined acceptable difference or delta is a better approach.

    Regards,
    Jim
    Last edited by jim829; 11-21-2014 at 02:11 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Formatting a users answer

    Personally I'm not averse to a string comparison in this case. The user is tapping their keyboard which going to result in a numeral (a string).

    More to the point, if you are going to pose a problem like 5/3 you ought to specify what counts as a correct answer. ("Answer to 2 decimal places"...) As a user I wouldn't be much impressed with "answer with something that when parsed with Java's Double.parseDouble() results in a value within epsilon=... of the correct answer."

    In real life we want to be both accurate about whether the user knows the answer and as tolerant as we can be of their quirks of expression. If that involves us in some messy parsing and other string manipulation so be it.

  16. #16
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    Ok, I appreciate the breakdown. I spoke with my instructor about this and he told me that the way I have it would work yes, but the better option is the way that you explained. (I showed him the forum conversation). This isn't for an assignment, it's something that i've decided to start working on myself while learning along the way.


    I was able to incorporate this into my code and it works this way

    Java Code:
    public static void division(int randNum1, int randNum2)
        {
    	char symbol = '/';
    	double user_answer, num;
    	Scanner input = new Scanner(System.in);
    		
    	System.out.println("\nWhat is " + randNum1 + " " + symbol + " "
    			   + randNum2 + " ? " + " Please round " 
    			   + "your answer to 2 decimal places.");
    
    	num = (double)randNum1  / (double) randNum2;
    	user_answer = input.nextDouble();
    	num = (int) ((num * 100) + .5) / 100.;
    
    	if (user_answer == num)
    	
    	    System.out.println("Good job!");
    
    	else 
    	    System.out.println("Sorry thats incorrect");
    	//check_answer(user_answer, randNum1, randNum2, symbol);
        }
    Last edited by ItchyJuffoWup; 11-21-2014 at 02:32 AM.

  17. #17
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Formatting a users answer

    Quote Originally Posted by pbrockway2 View Post
    Personally I'm not averse to a string comparison in this case. The user is tapping their keyboard which going to result in a numeral (a string).
    Except the String comparison method must be exact if you use what the user typed in. The original requirement of the OP was to allow "close enough" answers. Which I also took to mean if you had 1.67 and the user typed in 1.666666667 the answer should be accepted. So I was responding to that particular requirement. Specifying the accuracy of what the user must supply might be a better way to proceed.

    Edit: this reminds me of a quiz I used to take on old Unix systems (early 80's). It was called quiz and had a variety of built-in quizzes. One was a country/capital quiz. You were prompted with the country and asked for the capital. Knowing the answer wasn't good enough. You had to know the spelling, punctuation, capitalization, and whether it was hyphenated.

    Regards,
    Jim
    Last edited by jim829; 11-21-2014 at 02:35 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  18. #18
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    Quote Originally Posted by pbrockway2 View Post
    Personally I'm not averse to a string comparison in this case. The user is tapping their keyboard which going to result in a numeral (a string).

    More to the point, if you are going to pose a problem like 5/3 you ought to specify what counts as a correct answer. ("Answer to 2 decimal places"...) As a user I wouldn't be much impressed with "answer with something that when parsed with Java's Double.parseDouble() results in a value within epsilon=... of the correct answer."

    In real life we want to be both accurate about whether the user knows the answer and as tolerant as we can be of their quirks of expression. If that involves us in some messy parsing and other string manipulation so be it.
    I fully understand what you're saying, although my setback currently is, I'm a first quarter programming student. This project that i'm working is of personal interest to further my own studies alongside what i'm learning in college. I am unfamiliar thus far with the Double.parseDouble() command. I have surpassed on multiple categories where we are in the class, but there are still a million and one things for a young programmer to learn. But because you have brought it to my attention, i'll look into how this would and could be used!

    /R
    Nick

  19. #19
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,716
    Rep Power
    17

    Default Re: Formatting a users answer

    @Jim: I hadn't read closely enough. Yes, the "close enough" requirement does complexify things.

    What's worrying me is that I'd accept 5/3=2.67 but I would want to reject 5/3=2.6668. So I don"t like the "tolerance" approach. I think it was the OP who pointed out that things depend on context and someone like a bank would be really fussy about correctness. Now if I were writing this for small child I would check the length of their answer (and maybe other stuff like the index of the decimal character), String.format() the correct answer appropriately and compare the strings. I would go to this trouble because the small child is more important than a bank.

    @OP I am not suggesting you actually change what you're happy with. And, although I wave the flag for strings here, don"t use strings where numeric values (or dates etc) are the appropriate type: it's a common error.

  20. #20
    ItchyJuffoWup is offline Member
    Join Date
    Nov 2014
    Posts
    11
    Rep Power
    0

    Default Re: Formatting a users answer

    Ok, thank you for the advice. And to better clarify, I'm currently creating a project or program that solidifies a childs learning of basic mathematics. It gives them the option to select the category and the level of 'toughness'. So at this level, or level 1, it only uses the numbers 1 - 10. The child would still have to perform the mathematics on pen an paper, then simply input their answers in the program. So in the instance of long division, it would be acceptable to enter in the answer of only two decimal places. It seems that each person has their own opinion on what the best way to do it. Is there more an industry standard that is more acceptable than the other? or is it dependent upon the application in which it's being used?

Page 1 of 2 12 LastLast

Similar Threads

  1. Answer me !
    By johnmecanse34 in forum New To Java
    Replies: 2
    Last Post: 11-20-2013, 01:49 PM
  2. What is the Right Answer :
    By sachin218 in forum New To Java
    Replies: 2
    Last Post: 11-04-2012, 03:41 AM
  3. Compare true Answer /Player answer
    By Eilime in forum New To Java
    Replies: 1
    Last Post: 04-24-2011, 09:51 PM
  4. Replies: 3
    Last Post: 02-27-2011, 01:03 AM
  5. Could anyway answer me this?
    By Jojomofoman in forum New To Java
    Replies: 14
    Last Post: 12-17-2010, 12:14 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
  •