Results 1 to 14 of 14
  1. #1
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Exclamation 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 07:29 PM. Reason: wrong typing

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    For starters, have you actually tried COMPILING this and fixing the problems highlighted by the compiler?

  3. #3
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    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 compile
    Last edited by low224; 12-07-2008 at 08:15 PM.

  4. #4
    Popedreadlock is offline Member
    Join Date
    Dec 2008
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by low224 View Post
    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????
    I think that one of the things are missing a closing brace ( } ).

  5. #5
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    pls tell missing in which line???

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default 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);
      } 
    }

  7. #7
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    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!

  8. #8
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    thxxxx it very useful thxxxxx

  9. #9
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by neilcoffey View Post
    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);
      } 
    }
    Finally,do u think this code useful 2 science student???

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    low224 is offline Member
    Join Date
    Dec 2008
    Posts
    17
    Rep Power
    0

    Default

    thxxx I will study hard in java

  12. #12
    rdbennett is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    [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.

  13. #13
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i think editor should at least able to show text in difference color for reserved word

  14. #14
    rdbennett is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by mtyoung View Post
    i think editor should at least able to show text in difference color for reserved word
    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

  1. What's wrong with this code?
    By Doctor Cactus in forum New To Java
    Replies: 4
    Last Post: 11-29-2008, 06:44 PM
  2. What is wrong with this code
    By rosh72851 in forum New To Java
    Replies: 13
    Last Post: 10-31-2008, 02:50 AM
  3. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM
  4. what is wrong with this code
    By masaka in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 09:27 AM
  5. What's wrong with this code?
    By Wizard wusa in forum New To Java
    Replies: 14
    Last Post: 01-23-2008, 12:55 AM

Posting Permissions

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