Results 1 to 14 of 14
- 12-07-2008, 06:27 PM #1
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
pls tell wat wrong with my code???
import java.util.Scanner;
public class E=mc2//class name
{
{
public static void main( String args[] )//main method of class
{Scanner input=new Scanner (System.in);
Char number1;//1st variable
Char number2;//2st variable
Char energy;//calculation end
System.out.print("ENTER M:");//prompt
number1=input.nextChar();//read 1st number
number2=300000*300000;//CONSTAnT variable
Energy=number1*number2;//formula
System.out.printf("The Energy is %d\n,Energy");}//sum
}
Tell me what wrong with my code????Last edited by low224; 12-07-2008 at 06:29 PM. Reason: wrong typing
- 12-07-2008, 06:30 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
For starters, have you actually tried COMPILING this and fixing the problems highlighted by the compiler?
Neil Coffey
Javamex - Java tutorials and performance info
- 12-07-2008, 07:10 PM #3
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
i dun noe the compiler say what...
it say i have some error in public.
i using notepad to edit.
i using commad prompt to compileLast edited by low224; 12-07-2008 at 07:15 PM.
- 12-07-2008, 07:19 PM #4
Member
- Join Date
- Dec 2008
- Posts
- 6
- Rep Power
- 0
- 12-07-2008, 07:24 PM #5
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
pls tell missing in which line???
- 12-07-2008, 07:30 PM #6
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
A few errors
OK, here's a few things, but in general it's easier for people to help you if you state EXACT error messages rather than saying you got "some error":
- Use the normal conventions for class names: letters and numbers, starting the name with a captial letter. Don't try and put silly things like = signs inside class or variable names (the compiler won't let you!). So in this case, EMC2 could be a good name (and you need to call your file EMC2.java in that case)
- You've got a spurious brace { at the start of the class. If you format your code properly (see examples in books -- a common convention in Java is to put the opening brace at the END of the line), it will help you to spot this.
- The numbers you want to deal with clearly aren't chars. In fact there's not even such a thing as a Char (with capital letter) in Java. You need to use one of the standard Java data types suitable for numbers, int, long, float or double. In this case, I'd recommend 'double': this is the type to use for numbers that can be decimals, unless you've any reason to use something else. If you want to only use whole numbers, then use long in this case, as int allows a maximum value of around 2 billion, and your multiplying by a large number.
- Be careful about CASE: if you declare a variable as "energy", it's no good later trying to refer to it as "Energy". (By the way, the convention in Java is that class names have capitals, variables small letters.)
- Be careful about position of arguments in the call to printf() -- you had the parameter inside the quotes
- Call your variables useful things: if something represents "m" or "c", why not call it "m" or "c" rather than something meaningless like "number1" or "number2"?
So a corrected version could be:
Java Code:public class EMC2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter m: "); double m = input.nextDouble(); double c = 300000; double cSquared = c * c; double energy = m * cSquared; System.out.printf("Energy is %f", energy); } }Neil Coffey
Javamex - Java tutorials and performance info
- 12-07-2008, 07:32 PM #7
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
By the way, if you use a proper development environment (check out Eclipse) rather than notepad, the environment will help you spot some of your mistakes!
Neil Coffey
Javamex - Java tutorials and performance info
- 12-07-2008, 07:33 PM #8
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
thxxxx it very useful thxxxxx
- 12-07-2008, 07:43 PM #9
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
- 12-07-2008, 09:00 PM #10
Some suggestions ...
Neil... a couple suggestions:
- Although you did tell the OP what was wrong with his code (which is great and what this forum is about), it would be better the let the OP do the suggested changes himself and that way he will learn from experience and his retention will be better. It's just too much temptation to do a cut a paste and not learn.
- Observing the level of Java knowledge that the OP has, I would not suggest the OP getting involved with Eclipse right now. It might be overwhelming to learn an IDE plus Java at the same time. I definately would suggest using an IDE once the OP has more experience in Java and is comfortable with taking on the learning of an IDE.
Cheers,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 12-08-2008, 01:45 PM #11
Member
- Join Date
- Dec 2008
- Posts
- 17
- Rep Power
- 0
thxxx I will study hard in java
- 01-10-2009, 05:30 AM #12
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
[QUOTE=CJSLMAN;46673]Neil... a couple suggestions:
- Observing the level of Java knowledge that the OP has, I would not suggest the OP getting involved with Eclipse right now. It might be overwhelming to learn an IDE plus Java at the same time.
As a fellow newbie, I couldn't agree more. However, there are tools far less sophisticated, and therefore much easier to learn, that would provide at least some of the error checking. I'm using Crimson Editor, and I highly recommend it for beginners in any of the languages it supports. I have used it extensively in other work (e.g., HTML, DOORS XL, PHP), too. It's a great little editor without the steep learning curve of a full-blown IDE.
- 01-10-2009, 05:55 AM #13
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
i think editor should at least able to show text in difference color for reserved word
- 01-11-2009, 07:40 AM #14
Member
- Join Date
- Jan 2009
- Posts
- 2
- Rep Power
- 0
Couldn't agree with you more. Crimson Editor does that. It also has some additional functionality I find particularly useful, like matching braces and parens. You can also write macros for it if you're doing repetitive editing, global replace across all files you have open, and so on. It's quite a useful editor.
Similar Threads
-
What's wrong with this code?
By Doctor Cactus in forum New To JavaReplies: 4Last Post: 11-29-2008, 05:44 PM -
What is wrong with this code
By rosh72851 in forum New To JavaReplies: 13Last Post: 10-31-2008, 01:50 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM -
what is wrong with this code
By masaka in forum New To JavaReplies: 5Last Post: 04-16-2008, 08:27 AM -
What's wrong with this code?
By Wizard wusa in forum New To JavaReplies: 14Last Post: 01-22-2008, 11:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks