1. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

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.

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Formatting a users answer

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

Regards,
Jim

3. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

Re: Formatting a users answer

Originally Posted by jim829
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 01:06 AM.

4. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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

5. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

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 = '/';
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("#.##");

System.out.println("\nWhat is " + randNum1 + " " + symbol + " " + randNum2 + " ? ");

num = (double)randNum1  / (double) randNum2;

String str_num = df.format(num);

//System.out.println("str_num is " + str_num);

System.out.println("Good job!");

else
System.out.println("Sorry thats incorrect");```
Last edited by ItchyJuffoWup; 11-20-2014 at 01:45 AM.

6. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

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.

7. Re: Formatting a users answer

Originally Posted by gimbal2
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

8. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

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 11:25 AM.

9. Re: Formatting a users answer

Originally Posted by gimbal2
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

10. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

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.

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Formatting a users answer

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

Regards,
Jim

12. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
13

Re: Formatting a users answer

Originally Posted by jim829
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 ;)

13. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

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. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

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 01:11 AM.

15. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

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. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

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 = '/';
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;
num = (int) ((num * 100) + .5) / 100.;

if (user_answer == num)

System.out.println("Good job!");

else
System.out.println("Sorry thats incorrect");
}```
Last edited by ItchyJuffoWup; 11-21-2014 at 01:32 AM.

17. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

Re: Formatting a users answer

Originally Posted by pbrockway2
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 01:35 AM.

18. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

Re: Formatting a users answer

Originally Posted by pbrockway2
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. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

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. Member
Join Date
Nov 2014
Posts
11
Rep Power
0

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 Last

Posting Permissions

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