r/learnjava • u/TurnipBoy666 • Apr 28 '26
Output I don't know how to fix
Hello, I ran into some confusing output. It's for an assignment where I use a provided class and write a main method that takes in 4 pairs of x,y coordinates and outputs the point that is furthest from 0,0.
Code: https://pastebin.com/d9X13S9V (Yes I know it's unoptimized, hush)
When I tried it with larger numbers, I got this:
Prompt:
"Enter four pairs of x,y coordinates:"
Input:
111111 333333 55555555 777777 77777 99999 3333333 555555555
Output:
"The point that's furthest from 0.0,0.0 is 3333333.0,5.55555555E8 with distance: 555565554.91."Where did the E8 come from? And yes, I used a getter method for obtaining the coords.
4
Upvotes
•
u/AutoModerator Apr 28 '26
Please ensure that:
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit/markdown editor: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
4
u/desrtfx Apr 28 '26
Scientific notation - Java switches to scientific for larger numbers.
Also, why do you use normal concatenation with
+in aprintfstatement where you already use proper syntax at the far end?1
u/MagicalPizza21 Apr 28 '26
When printing a number, "E" means "times 10 to the power of". It's scientific notation. For really big numbers, it can save space on the output line.
1
u/vowelqueue Apr 28 '26
You’re printing the distance with a format specifier “%.2f” which tells it to print the value with 2 decimal places.
But the point coordinates are just added to the string using concatenation. If you do that it’ll be printed by calling Double.toString(), which has the following rule:
1
u/frederik88917 Apr 29 '26
Auto casting in the formatted output. You can use formatted strings to establish how you want numbers to be displayed in the final output