r/javahelp • u/Fragrant_Baseball776 • Jul 20 '21
Java Error
i have just started learning and writing a sample code to convert gallons into liters, i wrote below from a book
class Example4 {
public static void main(String args[]) {
double gallons = 10.0;
double liters=gallons*3.7854;
System.out.println("10 gallons ="+liters+"liters");
}
}
but when i do javac Example4.java. i get following error, i can't understand where did i make mistake in such a simple code
Error:
':' expected
javac Example4.java
1
Jul 20 '21
There is nothing wrong with your code. I even ran it myself, and can't reproduce your error, so there must be something outside of what you pasted into your post, if you're still seeing that error.
1
u/Fragrant_Baseball776 Jul 20 '21
i copied the code directly from Example4.java and i keep getting the error, how is that possible?
2
Jul 20 '21
Example4.java...where?
-2
u/Fragrant_Baseball776 Jul 20 '21
thats my file and i use javac Example4.java to compile and get the error
i have the file at c:\java
5
u/desrtfx Out of Coffee error - System halted Jul 20 '21
Sorry, but all you tell us does not help us to identify the issue in the faintest.
You are not even telling us what learning resource you are using, what IDE, how you compile the code, etc.
As such, we cannot tell you anything other than the code that you posted here is error free and compiles just fine.
Maybe try a different tutorial. We commonly recommend the MOOC Java Programming from the University of Helsinki. It is one of the best free Java courses around.
3
2
Jul 20 '21
The code you provided in your post compiles and runs fine. There is nothing else any of us can say to that. Check your file and make sure it matches what you're pasting here.
1
u/morhp Professional Developer Jul 21 '21
Make sure that you execute javac from the folder where that file is in. C:\java is an unusual folder. I'd put it somewhere in your user directory like c:\users\yourname\javaprojects\example
1
u/desrtfx Out of Coffee error - System halted Jul 20 '21
Please, copy the very code you run into here.
It seems as if you have retyped the code here.
2
1
u/ML-newb Jul 20 '21
Can you post some snapshots for where you are running and the error message? As others have said, there’s nothing wrong with the code. It maybe how you are executing it.
0
u/VinceGhii Jul 20 '21
Please formatte the code...
1
u/Fragrant_Baseball776 Jul 22 '21
i done the coding in word pad.
named the file Example4.java and saved under c:\java
i run cmd and go to c:\java and then run jshell and enter
javac Example4.java
i get following
Error:
';' expected
javac IfDemo.java;
•
u/AutoModerator Jul 20 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
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://imgur.com/a/fgoFFis) 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.