Solved

Sum of two Numbers in Javascript

Posted on 2012-04-03
12
245 Views
Last Modified: 2012-04-03
Hi
I want to have sum of two numbers,where i can assign my own values every time
My Program is

public class Sum
 {
   public static void main(String args[])
    {
      System.out.println("The sum of two numbers are : ");
      int x = Integer.parseInt(args[0]);
      int y = Integer.parseInt(args[1]);
      int sum = x + y;
      System.out.println("Sum = "+ sum);
    }
 }

It shows error,can anybody give me an idea of solving the error.
Sum.java
0
Comment
Question by:Govinda2020
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37800392
Are you passing two number on the command line?
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 37800411
change this line
System.out.println("The sum of two numbers are : ");
and print
System.out.println("The sum of two numbers are : " + args[0] + "+" + args[1]);

and let me know what it prints
0
 

Author Comment

by:Govinda2020
ID: 37800414
When I compile the program,It creates a class file.But when I try to run it the error shows like this  " The sum of two numbers are :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at Sum.main(Sum.java:6)"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37800439
...so again I ask:  are you passing two numbers on the command line? If you don't pass any command-line parameters to this program, then your code will fail when you try to index the args object.
0
 

Author Comment

by:Govinda2020
ID: 37800442
gurvinder372,
I tried doing that still it says
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at Sum.main(Sum.java:5)
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37800453
It would provided more stability to your code if you check the length of the args before trying to index it:

public class Sum 
 {
   public static void main(String args[]) 
    { 
      if (args.length() > 1)
      {
          System.out.println("The sum of two numbers are : ");
          int x = Integer.parseInt(args[0]);
          int y = Integer.parseInt(args[1]);
          int sum = x + y;
          System.out.println("Sum = "+ sum);
      }
    }
 }

Open in new window

0
 
LVL 12

Expert Comment

by:Sharon Seth
ID: 37800593
@Govinda2020 , You are not running the class file correctly . Just paste the command line here
0
 

Author Comment

by:Govinda2020
ID: 37800701
@sharonseth
I compile the program in bash- javac Sum.java
and then run - java Sum
0
 
LVL 12

Accepted Solution

by:
Sharon Seth earned 500 total points
ID: 37800841
java Sum your input args here

eg : java Sum 2 3

That's how you shld run it . 2 and 3 will be passed as arguments into args[]
0
 

Author Comment

by:Govinda2020
ID: 37800878
I got the output,
I compile the program in bash- javac Sum.java
and then when I run it  - ( java <class name> arguements )
THat is I wrote - java Sum 3 5
I got the answer Sum = 8
So everytime I change the values I get the correct answer
0
 

Author Closing Comment

by:Govinda2020
ID: 37800897
Thank you for the support
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 37804061
This is just a test of my "Submit" button, because apparently the fact that I posted twice asking whether or not command line parameters were being used wasn't being seen correctly (which is funny, because they show fine on my screen. Hmmm...).
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How  can  i  resolve  HTTP Status 404 -? 8 58
Glassfish admin console not working 1 56
Problem to Alipay 10 67
How to fix  socket closed error 11 26
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question