Adding 2 Number In Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LOVE4EVER
    New Member
    • Apr 2008
    • 5

    Adding 2 Number In Java

    HI PPL

    I DID A SIMPLE PROGRAM THAT ADD 2 NUMBER BUT I AM HAVING PROBLEM IN ENTER THE NUMBER FROM THE USER

    Code:
    import java.io.*;
    public class cal{
    
    
    int a=0;
    int b=0;
    int c=0;
    
    
    a =reader.readLine();
    b =reader.readLine();
    c =a+b;
    system.out.println(c);
    }
    >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>
    the error in the reader.readline ();

    in c++ you just have to write cin>>a>>

    i am new in java and i really dont know what to do
  • sukatoa
    Contributor
    • Nov 2007
    • 539

    #2
    Originally posted by LOVE4EVER
    HI PPL

    I DID A SIMPLE PROGRAM THAT ADD 2 NUMBER BUT I AM HAVING PROBLEM IN ENTER THE NUMBER FROM THE USER

    Code:
    import java.io.*;
    public class cal{
    
    
    int a=0;
    int b=0;
    int c=0;
    
    
    a =reader.readLine();
    b =reader.readLine();
    c =a+b;
    system.out.println(c);
    }
    >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>
    the error in the reader.readline ();

    in c++ you just have to write cin>>a>>

    i am new in java and i really dont know what to do
    Welcome to java forum.... :)

    You can start to read Scanner class.

    Look for Scanner( inputstream s) Constructor.... .

    Read also about "System.in" , " Integer.parseIn t( String s ) ".....
    try to browse again about object and class in java on how they will be used...

    Try to have some experiment on this

    Code:
    Scanner scan = new Scanner(System.in);
    String num1 = scan.nextLine();
    int integervalue = Integer.parseInt( num1 );
    Don't forget to import the built-in Scanner class....

    Update us...
    sukatoa

    Comment

    • LOVE4EVER
      New Member
      • Apr 2008
      • 5

      #3
      thx for the information a lot sukatoa its really helped me

      now the code is like this
      Code:
      import java.io.*;
      import java.util.Scanner;
      public class cal{
      
      
      
      Scanner read = new Scanner(system.in);
      
      int a =read.nextInt();
      int b =read.nextInt();
      int c =a+b;
      system.out.println(c);
      }
      there are 2 errors
      >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>
      cal.java:12: <identifier> expected
      system.out.prin tln(c);
      ^
      cal.java:12: <identifier> expected
      system.out.prin tln(c);
      ^
      2 errors
      >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>
      how (c) can be identifier!!!

      Comment

      • sukatoa
        Contributor
        • Nov 2007
        • 539

        #4
        Originally posted by LOVE4EVER
        thx for the information a lot sukatoa its really helped me

        now the code is like this
        Code:
        import java.io.*;
        import java.util.Scanner;
        public class cal{
        
        
        
        Scanner read = new Scanner(system.in);
        
        int a =read.nextInt();
        int b =read.nextInt();
        int c =a+b;
        system.out.println(c);
        }
        there are 2 errors
        >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>
        cal.java:12: <identifier> expected
        system.out.prin tln(c);
        ^
        cal.java:12: <identifier> expected
        system.out.prin tln(c);
        ^
        2 errors
        >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>
        how (c) can be identifier!!!
        You forgot to change the "s". It should be System.

        Just a reminder bro: Java is case sensitive.... :)

        regards,
        sukatoa....

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          Also, you don't need to import java.io.*, since the Scanner is handling input and System.out.prin tln handles output (the System class is included by default). Imports are like C(++)'s #includes, if you're not using anything from them, don't import/#include them. It's not a huge deal now, but it's just good practice not to import more than you need, as it slows down your code (especially if you import java.*...or javax.*...).

          Comment

          • LOVE4EVER
            New Member
            • Apr 2008
            • 5

            #6
            i did all the things you told me guys but i am still having the same errors

            this is the the new code
            Code:
            import java.util.Scanner;
            public class cal{
            
            Scanner read = new Scanner(System.in);
            
            int a =read.nextInt();
            int b =read.nextInt();
            int c =a+b;
            System.out.println (c);
            }
            2 errors
            >>>>>>>>>>>>>>> >>>>>>>>>
            Exception in thread "main" java.lang.NoCla ssDefFoundError : cal
            Caused by: java.lang.Class NotFoundExcepti on: cal
            at java.net.URLCla ssLoader$1.run( URLClassLoader. java:200)
            at java.security.A ccessController .doPrivileged(N ative Method)
            at java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:188)
            at java.lang.Class Loader.loadClas s(ClassLoader.j ava:306)
            at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:276 )
            at java.lang.Class Loader.loadClas s(ClassLoader.j ava:251)
            at java.lang.Class Loader.loadClas sInternal(Class Loader.java:319 )

            C:\DOCUME~1\mes \java>javac cal.java
            cal.java:9: <identifier> expected
            System.out.prin tln (c);
            ^
            cal.java:9: <identifier> expected
            System.out.prin tln (c);
            ^
            2 errors

            >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Take a Java tutorial before you start banging your keyboard: Java classes have
              methods and methods have statements. You can't put statements just at the
              class level, i.e. it doesn't make sense to the compiler.

              kind regards,

              Jos

              Comment

              • sukatoa
                Contributor
                • Nov 2007
                • 539

                #8
                Originally posted by LOVE4EVER
                i did all the things you told me guys but i am still having the same errors

                this is the the new code
                Code:
                import java.util.Scanner;
                public class cal{
                
                Scanner read = new Scanner(System.in);
                
                int a =read.nextInt();
                int b =read.nextInt();
                int c =a+b;
                System.out.println (c);
                }
                2 errors
                >>>>>>>>>>>>>>> >>>>>>>>>
                Exception in thread "main" java.lang.NoCla ssDefFoundError : cal
                Caused by: java.lang.Class NotFoundExcepti on: cal
                at java.net.URLCla ssLoader$1.run( URLClassLoader. java:200)
                at java.security.A ccessController .doPrivileged(N ative Method)
                at java.net.URLCla ssLoader.findCl ass(URLClassLoa der.java:188)
                at java.lang.Class Loader.loadClas s(ClassLoader.j ava:306)
                at sun.misc.Launch er$AppClassLoad er.loadClass(La uncher.java:276 )
                at java.lang.Class Loader.loadClas s(ClassLoader.j ava:251)
                at java.lang.Class Loader.loadClas sInternal(Class Loader.java:319 )

                C:\DOCUME~1\mes \java>javac cal.java
                cal.java:9: <identifier> expected
                System.out.prin tln (c);
                ^
                cal.java:9: <identifier> expected
                System.out.prin tln (c);
                ^
                2 errors

                >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>
                Those error may be avoided if you put those codes inside a method....
                You forgot also to have main method....

                Jos' recommendation may lead you to the right way....

                After reading, applying what you've learn,

                Update us about your new code.....
                sukatoa...

                Comment

                • LOVE4EVER
                  New Member
                  • Apr 2008
                  • 5

                  #9
                  i talled you guys i am new in java i used to programming in c++ and when i did
                  this very simple program i was using my information about c++ but never mind i didnt copy this problem without reading java tutorial but maybe you are see it simple problem for you but for me its not and i did a program that compute the result without the user enter the number but any way thx for help and the information its really helpful and of cource in the final i will have a code without errors

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by LOVE4EVER
                    i talled you guys i am new in java i used to programming in c++ and when i did
                    this very simple program i was using my information about c++ [ ... ]
                    C++ (and C and BCPL and CPL and C#) also have statements in functions/methods only.
                    Java is no exception. Please don't imagine how a language could/should work,
                    read the authorative standards on it and only then try to implement something.
                    Even more: before you implement anything at all you should design it first, just
                    using scrapbook paper and a pencil. The actual typing in of your program should
                    be a boring task and should be finished asap.

                    Most people don't do that: they have a vague idea and they start banging their
                    keyboards with the only result that they're tied to that damned thing during long
                    complicated debug sessions until 'something' more or less working is the result.
                    Don't follow that path because all you'll create are ugly little monstrosities.

                    kind regards,

                    Jos

                    Comment

                    • LOVE4EVER
                      New Member
                      • Apr 2008
                      • 5

                      #11
                      i understant now why the errors because there is no method so i put the method and its really nice to see that no errors in the code but when i open the .class file it give me this statment
                      >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>

                      Exception in thread "main" java.lang.NoSuc hMethodError: main

                      >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>

                      and this is the code
                      Code:
                      import java.util.Scanner;
                      public class cal{
                      
                      public static void main(int  [] args) 
                      {
                      Scanner read = new Scanner(System.in);
                      
                      int a =read.nextInt();
                      int b =read.nextInt();
                      int c =a+b;
                      System.out.println(c);
                      }
                      }
                      why its not working ?

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by LOVE4EVER
                        i understant now why the errors because there is no method so i put the method and its really nice to see that no errors in the code but when i open the .class file it give me this statment
                        >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>

                        Exception in thread "main" java.lang.NoSuc hMethodError: main

                        >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>

                        and this is the code
                        Code:
                        import java.util.Scanner;
                        public class cal{
                        
                        public static void main(int  [] args) 
                        {
                        Scanner read = new Scanner(System.in);
                        
                        int a =read.nextInt();
                        int b =read.nextInt();
                        int c =a+b;
                        System.out.println(c);
                        }
                        }
                        why its not working ?
                        The single argument to the main() method has type String[] not int[]. The JVM
                        checks that.

                        kind regards,

                        Jos

                        Comment

                        • hsn
                          New Member
                          • Sep 2007
                          • 237

                          #13
                          you should also look at other types to read any input like InputStreamRead er and so.

                          hsn

                          Comment

                          • BigDaddyLH
                            Recognized Expert Top Contributor
                            • Dec 2007
                            • 1216

                            #14
                            I still think going through a tutorial could only help you.

                            Comment

                            • Kid Programmer
                              New Member
                              • Mar 2008
                              • 176

                              #15
                              Originally posted by LOVE4EVER
                              HI PPL

                              I DID A SIMPLE PROGRAM THAT ADD 2 NUMBER BUT I AM HAVING PROBLEM IN ENTER THE NUMBER FROM THE USER

                              Code:
                              import java.io.*;
                              public class cal{
                              
                              
                              int a=0;
                              int b=0;
                              int c=0;
                              
                              
                              a =reader.readLine();
                              b =reader.readLine();
                              c =a+b;
                              system.out.println(c);
                              }
                              >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>>>>>>>>>>>> >>>>
                              the error in the reader.readline ();

                              in c++ you just have to write cin>>a>>

                              i am new in java and i really dont know what to do
                              You are becoming a help vampire. But to get this over with just copy and paste this code:
                              Code:
                              import java.util.Scanner;
                              
                              public class cal {
                              
                              	public static void main(String[] args) {
                              		Scanner scan = new Scanner(System.in);
                              		double a, b, c;
                              	
                              		a = scan.nextDouble();
                              		b = scan.nextDouble();
                              		c = a + b;
                              
                              		System.out.println(c);
                              	}
                              }
                              It should work perfectly :-) Good luck programming.

                              Comment

                              Working...