Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

Adding 2 Number In Java

Question posted by: LOVE4EVER (Newbie) on April 5th, 2008 03:23 PM
HI PPL

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

Code: ( text )
  1. import java.io.*;
  2. public class cal{
  3.  
  4.  
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8.  
  9.  
  10. a =reader.readLine();
  11. b =reader.readLine();
  12. c =a+b;
  13. system.out.println(c);
  14. }


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
sukatoa's Avatar
sukatoa
Needs Regular Fix
456 Posts
April 5th, 2008
03:42 PM
#2

Re: Adding 2 Number In Java
Quote:
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: ( text )
  1. import java.io.*;
  2. public class cal{
  3.  
  4.  
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8.  
  9.  
  10. a =reader.readLine();
  11. b =reader.readLine();
  12. c =a+b;
  13. system.out.println(c);
  14. }


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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.parseInt( 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: ( text )
  1. Scanner scan = new Scanner(System.in);
  2. String num1 = scan.nextLine();
  3. int integervalue = Integer.parseInt( num1 );


Don't forget to import the built-in Scanner class....

Update us...
sukatoa

Reply
LOVE4EVER's Avatar
LOVE4EVER
Newbie
5 Posts
April 5th, 2008
04:43 PM
#3

Re: Adding 2 Number In Java
thx for the information a lot sukatoa its really helped me

now the code is like this
Code: ( text )
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class cal{
  4.  
  5.  
  6.  
  7. Scanner read = new Scanner(system.in);
  8.  
  9. int a =read.nextInt();
  10. int b =read.nextInt();
  11. int c =a+b;
  12. system.out.println(c);
  13. }

there are 2 errors
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cal.java:12: <identifier> expected
system.out.println(c);
^
cal.java:12: <identifier> expected
system.out.println(c);
^
2 errors
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
how (c) can be identifier!!!

Reply
sukatoa's Avatar
sukatoa
Needs Regular Fix
456 Posts
April 6th, 2008
03:48 AM
#4

Re: Adding 2 Number In Java
Quote:
Originally Posted by LOVE4EVER
thx for the information a lot sukatoa its really helped me

now the code is like this
Code: ( text )
  1. import java.io.*;
  2. import java.util.Scanner;
  3. public class cal{
  4.  
  5.  
  6.  
  7. Scanner read = new Scanner(system.in);
  8.  
  9. int a =read.nextInt();
  10. int b =read.nextInt();
  11. int c =a+b;
  12. system.out.println(c);
  13. }

there are 2 errors
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
cal.java:12: <identifier> expected
system.out.println(c);
^
cal.java:12: <identifier> expected
system.out.println(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....

Reply
Laharl's Avatar
Laharl
Expert
733 Posts
April 6th, 2008
04:11 AM
#5

Re: Adding 2 Number In Java
Also, you don't need to import java.io.*, since the Scanner is handling input and System.out.println 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.*...).

Reply
LOVE4EVER's Avatar
LOVE4EVER
Newbie
5 Posts
April 6th, 2008
10:52 AM
#6

Re: Adding 2 Number In Java
i did all the things you told me guys but i am still having the same errors

this is the the new code
Code: ( text )
  1. import java.util.Scanner;
  2. public class cal{
  3.  
  4. Scanner read = new Scanner(System.in);
  5.  
  6. int a =read.nextInt();
  7. int b =read.nextInt();
  8. int c =a+b;
  9. System.out.println (c);
  10. }


2 errors
>>>>>>>>>>>>>>>>>>>>>>>>
Exception in thread "main" java.lang.NoClassDefFoundError: cal
Caused by: java.lang.ClassNotFoundException: cal
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)

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

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

Reply
JosAH's Avatar
JosAH
Chief Editor
7,525 Posts
April 6th, 2008
11:18 AM
#7

Re: Adding 2 Number In Java
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

Reply
sukatoa's Avatar
sukatoa
Needs Regular Fix
456 Posts
April 6th, 2008
11:30 AM
#8

Re: Adding 2 Number In Java
Quote:
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: ( text )
  1. import java.util.Scanner;
  2. public class cal{
  3.  
  4. Scanner read = new Scanner(System.in);
  5.  
  6. int a =read.nextInt();
  7. int b =read.nextInt();
  8. int c =a+b;
  9. System.out.println (c);
  10. }


2 errors
>>>>>>>>>>>>>>>>>>>>>>>>
Exception in thread "main" java.lang.NoClassDefFoundError: cal
Caused by: java.lang.ClassNotFoundException: cal
at java.net.URLClassLoader$1.run(URLClassLoader.java: 200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 06)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:2 51)
at java.lang.ClassLoader.loadClassInternal(ClassLoade r.java:319)

C:\DOCUME~1\mes\java>javac cal.java
cal.java:9: <identifier> expected
System.out.println (c);
^
cal.java:9: <identifier> expected
System.out.println (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...

Reply
LOVE4EVER's Avatar
LOVE4EVER
Newbie
5 Posts
April 6th, 2008
12:14 PM
#9

Re: Adding 2 Number In Java
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

Reply
JosAH's Avatar
JosAH
Chief Editor
7,525 Posts
April 6th, 2008
12:25 PM
#10

Re: Adding 2 Number In Java
Quote:
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

Reply
LOVE4EVER's Avatar
LOVE4EVER
Newbie
5 Posts
April 7th, 2008
09:29 AM
#11

Re: Adding 2 Number In Java
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.NoSuchMethodError: main

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

and this is the code
Code: ( text )
  1. import java.util.Scanner;
  2. public class cal{
  3.  
  4. public static void main(int  [] args)
  5. {
  6. Scanner read = new Scanner(System.in);
  7.  
  8. int a =read.nextInt();
  9. int b =read.nextInt();
  10. int c =a+b;
  11. System.out.println(c);
  12. }
  13. }

why its not working ?

Reply
JosAH's Avatar
JosAH
Chief Editor
7,525 Posts
April 7th, 2008
09:35 AM
#12

Re: Adding 2 Number In Java
Quote:
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.NoSuchMethodError: main

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

and this is the code
Code: ( text )
  1. import java.util.Scanner;
  2. public class cal{
  3.  
  4. public static void main(int  [] args)
  5. {
  6. Scanner read = new Scanner(System.in);
  7.  
  8. int a =read.nextInt();
  9. int b =read.nextInt();
  10. int c =a+b;
  11. System.out.println(c);
  12. }
  13. }

why its not working ?


The single argument to the main() method has type String[] not int[]. The JVM
checks that.

kind regards,

Jos

Reply
hsn's Avatar
hsn
Familiar Sight
188 Posts
April 7th, 2008
10:04 AM
#13

Re: Adding 2 Number In Java
you should also look at other types to read any input like InputStreamReader and so.

hsn

Reply
BigDaddyLH's Avatar
BigDaddyLH
Moderator
1,211 Posts
April 7th, 2008
03:14 PM
#14

Re: Adding 2 Number In Java
I still think going through a tutorial could only help you.

Reply
Kid Programmer's Avatar
Kid Programmer
Familiar Sight
174 Posts
May 14th, 2008
08:06 PM
#15

Re: Adding 2 Number In Java
Quote:
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: ( text )
  1. import java.io.*;
  2. public class cal{
  3.  
  4.  
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8.  
  9.  
  10. a =reader.readLine();
  11. b =reader.readLine();
  12. c =a+b;
  13. system.out.println(c);
  14. }


>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
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: ( text )
  1. import java.util.Scanner;
  2.  
  3. public class cal {
  4.  
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         double a, b, c;
  8.    
  9.         a = scan.nextDouble();
  10.         b = scan.nextDouble();
  11.         c = a + b;
  12.  
  13.         System.out.println(c);
  14.     }
  15. }

It should work perfectly :-) Good luck programming.

Reply
Laharl's Avatar
Laharl
Expert
733 Posts
May 15th, 2008
12:02 AM
#16

Re: Adding 2 Number In Java
Kid, after five weeks, either he's gotten the answer himself or he's given up and gone elsewhere. Regardless, we don't just give boilerplate code here and tell the OP to copy/paste.

Reply
Geethu03's Avatar
Geethu03
Member
40 Posts
May 15th, 2008
06:44 AM
#17

Re: Adding 2 Number In Java
Use this i think it will help you. Pls follow this code
Code: ( text )
  1. import java.io.*;
  2. public class cal{
  3.   public static void main(String arg[]) throws IOException {
  4.  
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8.  
  9. DataInputStream ds = new DataInputStream(System.in());
  10. a =Integer.parseInt(ds.readLine());
  11. b =Integer.parseInt(ds.readLine());
  12. c =a+b;
  13. system.out.println(c);
  14. }

Quote:
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: ( text )
  1. import java.io.*;
  2. public class cal{
  3.  
  4.  
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8.  
  9.  
  10. a =reader.readLine();
  11. b =reader.readLine();
  12. c =a+b;
  13. system.out.println(c);
  14. }


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

Reply
JosAH's Avatar
JosAH
Chief Editor
7,525 Posts
May 15th, 2008
07:09 AM
#18

Re: Adding 2 Number In Java
Quote:
Originally Posted by Geethu03
Use this i think it will help you. Pls follow this code


I bet you didn't read reply #16 by Lahari and no, you don't need DataInputStreams
when you want to read text from user's input.

kind regards,

Jos

Reply
Kid Programmer's Avatar
Kid Programmer
Familiar Sight
174 Posts
May 16th, 2008
02:55 AM
#19

Re: Adding 2 Number In Java
Quote:
Originally Posted by Laharl
Kid, after five weeks, either he's gotten the answer himself or he's given up and gone elsewhere. Regardless, we don't just give boilerplate code here and tell the OP to copy/paste.

Well he never said he found the answer and it had gone of for long enough. I figured I might as well just end it. If he has questions about my code I can answer them.

Reply
Reply
Not the answer you were looking for? Post your question . . .
183,943 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Java Forum Contributors