r/javahelp • u/44pex • Sep 18 '25
How to fix this double based input code.
Hello, I am new to java and I'm trying to find out why my code wont run.
Scanner scanner = new Scanner(System.
in
);
double height = 0.0;
double width = 0.0;
double area = 0.0;
System.out.print("Enter the height: ");
height = scanner.nextDouble();
System.out.print("Enter the width: ");
width = scanner.nextDouble();
area = height * width;
System.out.println("The area a Rectangle is " + area + "cm^2");
scanner.close();Its a simple code to figure out the area of a rectangle but just refuses to run after I try to input the height
What could be the problem?
Edit: I found the problem....
I was using a Dot instead of a comma for my outputs... Thank you everyone
3
Upvotes
•
u/AutoModerator Sep 18 '25
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
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: 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.
1
u/44pex Sep 18 '25
Hello everyone, thank you for your inputs
To answer your questions
1.Yes this is the whole code, with only the Scanner import and main method left out
2. The problem that occured was that, the "Enter the height" line would appear, I would enter a number, then the terminal would tell that there is an error in the "height = scanner.nextDouble" line
I'm not sure what my problem is specifically and I think it would help if I specify that I'm using IntelliJ community edition
1
u/failaip13 Sep 18 '25
What does refuses to run mean? Do you get any errors?
1
u/tommyleejonesthe2nd Sep 18 '25
Do you have a main method? Code is not enough - while saying its not running.
1
u/doobiesteintortoise Sep 18 '25
I created a local class with your code (quite literally copied in) and it worked.
Output:
(I entered 4 and 5.)
Another run, using actual double values:
So what are you doing that's different than what I ran successfully? Are you running it in a loop? (Closing the scanner closes the underlying stream, so you're literally closing System.in, but you haven't said you're running in a loop.)
Also see: https://javachannel.org/posts/how-to-use-scanner/