Simple Java Program but not running??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • feda
    New Member
    • Mar 2008
    • 11

    Simple Java Program but not running??

    Hi everybody,
    I am new to java I installed netbeans 6.0.1
    I tried running the following simple program but it didnor work???


    import java.util.*;
    public class Hello
    {
    public static void main(String [] args)
    {
    Scanner reader = new Scanner(System. in);
    String name;

    System.out.prin t("What is your name? ");
    name = reader.readLine ();

    System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
    }
    }

    I got the following error:

    init:
    deps-jar:
    Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
    C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 28: class, interface, or enum expected
    --------------------------------------------------------------------------------
    1 error
    BUILD FAILED (total time: 0 seconds)
  • doobybug
    New Member
    • Mar 2008
    • 2

    #2
    Try using this code to read the name

    name= scanner.next();

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      The class you have shown us is not the class that was compiled.

      kind regards,

      Jos

      Comment

      • feda
        New Member
        • Mar 2008
        • 11

        #4
        Originally posted by JosAH
        The class you have shown us is not the class that was compiled.

        kind regards,

        Jos

        Thanks but how can I do it I just press the green triangle.

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by feda
          Thanks but how can I do it I just press the green triangle.
          I don't know; press the help item for netbeans?

          kind regards,

          Jos

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            If you are new to Java, I suggest ignoring Netbeans and all such IDEs for now and working with simple text editors like Notepad or Textpad until you've grasped the basic concepts.

            Comment

            • feda
              New Member
              • Mar 2008
              • 11

              #7
              Originally posted by r035198x
              If you are new to Java, I suggest ignoring Netbeans and all such IDEs for now and working with simple text editors like Notepad or Textpad until you've grasped the basic concepts.
              I ran the program ad I got the following error:

              init:
              deps-jar:
              Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
              C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 13: class Hello is public, should be declared in a file named Hello.java
              public class Hello
              C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 21: non-static method nextLine() cannot be referenced from a static context
              name =Scanner.nextLi ne();
              2 errors
              BUILD FAILED (total time: 1 second)


              reminding my code program is:

              package javaapplication 8;

              /**
              *
              * @author PAAET
              */
              import java.util.*;
              public class Hello
              {
              public static void main(String [] args)
              {
              Scanner reader = new Scanner(System. in);
              String name;

              System.out.prin t("What is your name? ");
              name =Scanner.nextLi ne();

              System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
              }
              }

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by feda
                I ran the program ad I got the following error:

                init:
                deps-jar:
                Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
                C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 13: class Hello is public, should be declared in a file named Hello.java
                public class Hello
                C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 21: non-static method nextLine() cannot be referenced from a static context
                name =Scanner.nextLi ne();
                2 errors
                BUILD FAILED (total time: 1 second)


                reminding my code program is:

                package javaapplication 8;

                /**
                *
                * @author PAAET
                */
                import java.util.*;
                public class Hello
                {
                public static void main(String [] args)
                {
                Scanner reader = new Scanner(System. in);
                String name;

                System.out.prin t("What is your name? ");
                name =Scanner.nextLi ne();

                System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                }
                }
                You put your code in a file with a wrong name. A public class Foo has to be
                stored in a file name Foo.java. You put a public class Hello in a file named
                Main.java. Also you have a Scanner instance 'reader'; that instance has a
                method nextLine() and that method isn't static so you can't call it on the class
                Scanner.

                kind regards,

                Jos

                Comment

                • feda
                  New Member
                  • Mar 2008
                  • 11

                  #9
                  Originally posted by JosAH
                  You put your code in a file with a wrong name. A public class Foo has to be
                  stored in a file name Foo.java. You put a public class Hello in a file named
                  Main.java. Also you have a Scanner instance 'reader'; that instance has a
                  method nextLine() and that method isn't static so you can't call it on the class
                  Scanner.

                  kind regards,

                  Jos
                  Thankyou very much for ur answer which one error has solved but what is the solution to the second error?? is there another static method ?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by feda
                    Thankyou very much for ur answer which one error has solved but what is the solution to the second error?? is there another static method ?
                    You created a 'reader' object that can do the job for you so don't call Scanner.readLin e()
                    but reader.readLine ()

                    kind regards,

                    Jos

                    Comment

                    • feda
                      New Member
                      • Mar 2008
                      • 11

                      #11
                      Originally posted by JosAH
                      You created a 'reader' object that can do the job for you so don't call Scanner.readLin e()
                      but reader.readLine ()

                      kind regards,

                      Jos
                      I changed my code as follows:

                      [code=java]
                      import java.lang.*;
                      import java.io.*;
                      public class Main
                      {
                      public static void main(String [] args)
                      {

                      String name;

                      System.out.prin t("What is your name? ");
                      BufferedReader reader=new BufferedReader (new InputStreamRead er(System.in));
                      name =reader.readLin e();

                      System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                      }
                      }
                      [/code]

                      But still not working I got the following error:

                      init:
                      deps-jar:
                      Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
                      C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 23: unreported exception java.io.IOExcep tion; must be caught or declared to be thrown
                      name =reader.readLin e();
                      1 error
                      BUILD FAILED (total time: 0 seconds)
                      Sorry for disturbing!!

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by feda
                        I changed my code as follows:

                        [code=java]
                        import java.lang.*;
                        import java.io.*;
                        public class Main
                        {
                        public static void main(String [] args)
                        {

                        String name;

                        System.out.prin t("What is your name? ");
                        BufferedReader reader=new BufferedReader (new InputStreamRead er(System.in));
                        name =reader.readLin e();

                        System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                        }
                        }
                        [/code]

                        But still not working I got the following error:

                        init:
                        deps-jar:
                        Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
                        C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 23: unreported exception java.io.IOExcep tion; must be caught or declared to be thrown
                        name =reader.readLin e();
                        1 error
                        BUILD FAILED (total time: 0 seconds)
                        Sorry for disturbing!!
                        No need to say sorry; you should read some tutorials first before you start coding
                        by just guessing. btw I still don't understand that line number 23 in the error
                        diagnostic message; your source file doesn't have that many lines. The way
                        you're reporting your problem is very confusing. For now change your line
                        where you defined your main method to:

                        [code=java]
                        public static void main(String[] args) throws IOException {
                        [/code]

                        Note that this is not a proper solution but it helps you to get started with your
                        first program. Please read the API documentation for the classes and their methods.

                        kind regards,

                        Jos

                        Comment

                        • feda
                          New Member
                          • Mar 2008
                          • 11

                          #13
                          Originally posted by JosAH
                          No need to say sorry; you should read some tutorials first before you start coding
                          by just guessing. btw I still don't understand that line number 23 in the error
                          diagnostic message; your source file doesn't have that many lines. The way
                          you're reporting your problem is very confusing. For now change your line
                          where you defined your main method to:

                          [code=java]
                          public static void main(String[] args) throws IOException {
                          [/code]

                          Note that this is not a proper solution but it helps you to get started with your
                          first program. Please read the API documentation for the classes and their methods.

                          kind regards,

                          Jos
                          package javaapplication 8;

                          /**
                          *
                          * @author PAAET
                          */
                          import java.io.*;
                          public class Main
                          {
                          public static void main(String [] args) throws IOExeption
                          {

                          String name;

                          System.out.prin t("What is your name? ");
                          BufferedReader reader=new BufferedReader (new InputStreamRead er(System.in));
                          name =reader.readLin e();

                          System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                          }
                          }


                          error code is :

                          init:
                          deps-jar:
                          Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
                          C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 15: cannot find symbol
                          symbol : class IOExeption
                          location: class javaapplication 8.Main
                          public static void main(String [] args) throws IOExeption
                          1 error
                          BUILD FAILED (total time: 0 seconds)

                          I read tutorials but the problem that I studied Pascal at my college which is much easier. So please be patient with me.
                          Thankyou

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by feda
                            package javaapplication 8;

                            /**
                            *
                            * @author PAAET
                            */
                            import java.io.*;
                            public class Main
                            {
                            public static void main(String [] args) throws IOExeption
                            {

                            String name;

                            System.out.prin t("What is your name? ");
                            BufferedReader reader=new BufferedReader (new InputStreamRead er(System.in));
                            name =reader.readLin e();

                            System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                            }
                            }


                            error code is :

                            init:
                            deps-jar:
                            Compiling 1 source file to C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\b uild\classes
                            C:\Documents and Settings\PAAET User\My Documents\NetBe ansProjects\Jav aApplication8\s rc\javaapplicat ion8\Main.java: 15: cannot find symbol
                            symbol : class IOExeption
                            location: class javaapplication 8.Main
                            public static void main(String [] args) throws IOExeption
                            1 error
                            BUILD FAILED (total time: 0 seconds)

                            I read tutorials but the problem that I studied Pascal at my college which is much easier. So please be patient with me.
                            Thankyou
                            You're still not showing us the code you are compiling: manually counting your
                            main() method starts at line 10 while the compiler finds it at line 15. The code
                            you're showing us includes the java.io.* entire package. That IOException is
                            a member of that package. I don't understand what you're doing, sorry.

                            kind regards,

                            Jos

                            Comment

                            • feda
                              New Member
                              • Mar 2008
                              • 11

                              #15
                              Originally posted by JosAH
                              You're still not showing us the code you are compiling: manually counting your
                              main() method starts at line 10 while the compiler finds it at line 15. The code
                              you're showing us includes the java.io.* entire package. That IOException is
                              a member of that package. I don't understand what you're doing, sorry.

                              kind regards,

                              Jos
                              [code=java]
                              /*
                              * To change this template, choose Tools | Templates
                              * and open the template in the editor.
                              */

                              package javaapplication 8;

                              /**
                              *
                              * @author PAAET
                              */
                              import java.io.*;
                              public class Main
                              {
                              public static void main(String [] args) throws IOExeption
                              {

                              String name;

                              System.out.prin t("What is your name? ");
                              BufferedReader reader=new BufferedReader (new InputStreamRead er(System.in));
                              name =reader.readLin e();

                              System.out.prin tln(name + " I look forward to a long and fruitful relationship.") ;
                              }
                              }
                              [/code]
                              That was the whole code
                              Thanks
                              Last edited by JosAH; Mar 14 '08, 05:59 PM. Reason: please use those [code=java] ... [/code] tags

                              Comment

                              Working...