• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Sum of two Numbers in Javascript

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
Govinda2020
Asked:
Govinda2020
  • 5
  • 4
  • 2
  • +1
1 Solution
 
käµfm³d 👽Commented:
Are you passing two number on the command line?
0
 
Gurvinder Pal SinghCommented:
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
 
Govinda2020Author Commented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
käµfm³d 👽Commented:
...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
 
Govinda2020Author Commented:
gurvinder372,
I tried doing that still it says
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
      at Sum.main(Sum.java:5)
0
 
käµfm³d 👽Commented:
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
 
Sharon SethCommented:
@Govinda2020 , You are not running the class file correctly . Just paste the command line here
0
 
Govinda2020Author Commented:
@sharonseth
I compile the program in bash- javac Sum.java
and then run - java Sum
0
 
Sharon SethCommented:
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
 
Govinda2020Author Commented:
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
 
Govinda2020Author Commented:
Thank you for the support
0
 
käµfm³d 👽Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now