Trying to learn Java. Why doesn't this work?

Discussion in 'Plugin Development' started by OhhhhhDayummm, Sep 28, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    OhhhhhDayummm

    So I've watched a few of thenewboston's videos and I'll continue watching them. I'm stuck at this part.
    Code:
    import java.util.Scanner;
     
    class apples {
        public static void main(String args[]){
            Scanner orange = new Scanner(System.in);
            System.out.println(orange.nextLine());
        }
    }
    When I run it in Eclipse it says
    Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at apples.apples.main(apples.java:4)
    Help please? Thanks!
     
  2. Offline

    ZeusAllMighty11

    args[] would mean that args is an object with an array.. but since it isn't..
    Do what CaptainBern said.

    In a java program, the main method is called.
    Code:
    public static void main(String[] args)
    {
        System.out.println("Hello world");
    }
    
    Similar to C++
    Code:
    int main()
    {
        cout << "Hello world" << endl;
        return 0;
    }
    

    What IDE are you using?
     
  3. TheGreenGamerHD He mentioned eclipse in his post so I suppose eclipse :p , weird tho, I don't think you can write his code in eclipse without eclipse giving an error...
     
  4. Offline

    OhhhhhDayummm

    I changed it to what CaptainBern said but it still has the same error.
     
  5. Offline

    The_Doctor_123

  6. Offline

    OhhhhhDayummm

    But do you know why I get this error?
     
  7. Offline

    The_Doctor_123

  8. The_Doctor_123 Ah yes indeed, it's been a while since I used it like that...
    OhhhhhDayummm Maybe try changing class apples to public class apples...
     
  9. Offline

    OhhhhhDayummm

    Okay. I still get an error that says: Exception in thread "main" java.lang.Error: Unresolved compilation problem:

    at apples.apples.main(apples.java:4)
    My code is
    Code:
    import java.util.Scanner;
     
    public class apples {
        public static void main(String[] args){
            Scanner orange = new Scanner(System.in);
            System.out.println(orange.nextLine());
        }
    }
    Sorry for being such a newbie.
     
  10. Offline

    The_Doctor_123

    OhhhhhDayummm
    I tested your code, it worked completely fine..

    Console:
    Code:
    hello
    hello
     
  11. Offline

    OhhhhhDayummm

    It didn't work for me though?
    [​IMG]

    Yes.
     
  12. Offline

    The_Doctor_123

    OhhhhhDayummm
    I really have no clue why it's not working properly for you. Obviously it's something else that you're doing wrong. Try creating a package other than the default package.
     
  13. Offline

    OhhhhhDayummm

    Which kind should I use?
     
  14. Offline

    The_Doctor_123

  15. Offline

    OhhhhhDayummm

    package. Also are you using Eclipse?
     
  16. Offline

    The_Doctor_123

    OhhhhhDayummm
    Last time I checked, there was only one kind of package.. And yes, I am as well.
     
  17. Offline

    OhhhhhDayummm

    Which version?
     
  18. Offline

    The_Doctor_123

  19. Well as you've shown in the picture, you put your class inside the 'apples' package, however, you don't say inside the file that this class is inside that given package.
    At the start of your file, just define the package your class is in. In this case it should be in 'apples' thus it should be:
    package apples;
    and done.
     
    amhokies likes this.
  20. Offline

    amhokies

    And really, all you have to do is hover over the error on the top line and it should say something like "add package declaration" and if you click it, it will do it for you. Anytime you have an error in eclipse, just hover over the little red line and it should tell you what is wrong.
     
  21. Offline

    The_Doctor_123

    kumpelblase2
    Oh dang! I didn't see him put that picture there. You're completely correct, put that at the very top of your class. Why it didn't auto-generate, I don't know.. Unless you removed it for whatever reason..
     
  22. Offline

    OhhhhhDayummm

Thread Status:
Not open for further replies.

Share This Page