Exception...

In the attached code user is to enter two integers and get their sum.

It is supposed to throw exception:

-if the entry was more than two:
    2 4   <-- is correct
    2  3  7  <--not correct has three numbers

-if both not integer (float string, etc)

After giving the error, it is supposed to ask user to enter it again.

Thank you.
import java.util.Scanner;

public class TestCalculator {

   static int number1;
   static int number2;
           
   public static boolean isNumeric(String s) throws NumberFormatException {

    boolean b = false;
    String[] inputs = s.split(" ");

    if(inputs.length==2)
    {
        try {
            number1=Integer.parseInt(inputs[0]);
            number2=Integer.parseInt(inputs[1]);
            b=true;
        }
        catch(NumberFormatException ex){
            throw ex;
        }
    }
        return b;
    }
   
    public static void main(String[] args) {

        boolean terminate=false;
        while(terminate==false) {

            Scanner input=new Scanner(System.in);
            String str = input.nextLine();
            terminate=(isNumeric(str));
            System.out.println(number1+number2+"\n");
        }

    }
       
}

Open in new window

LVL 34
Mike EghtebasDatabase and Application DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
So waht is your problem with that?
It wil be something like below, you also need to wrute a short class
with that another exception

import java.util.Scanner;

public class TestCalculator {

   static int number1;
   static int number2;
           
   public static boolean isNumeric(String s) throws NumberFormatException, WrongNumberOfInputsException {

    boolean b = false;
    String[] inputs = s.split(" ");

    if(inputs.length==2)
    {
        try {
            number1=Integer.parseInt(inputs[0]);
            number2=Integer.parseInt(inputs[1]);
            b=true;
        }
        catch(NumberFormatException ex){
            throw ex;
        }
    } else
throw new WrongNumberOfInputsException();



        return b;
    }
   
    public static void main(String[] args) {

        boolean terminate=false;
        while(terminate==false) {

            Scanner input=new Scanner(System.in);
            String str = input.nextLine();
          try{
            terminate=(isNumeric(str));
               } catch(Exception ex){
     System.out.println(ex);

}

            System.out.println(number1+number2+"\n");
        }

    }
       
}

Open in new window

0
for_yanCommented:

this is how it works:
import java.util.Scanner;

public class TestCalc1 {

   static int number1;
   static int number2;

   public static boolean isNumeric(String s) throws NumberFormatException, WrongNumberOfInputsException {

    boolean b = false;
    String[] inputs = s.split(" ");

    if(inputs.length==2)
    {
        try {
            number1=Integer.parseInt(inputs[0]);
            number2=Integer.parseInt(inputs[1]);
            b=true;
        }
        catch(NumberFormatException ex){
            throw ex;
        }
    } else
throw new WrongNumberOfInputsException("need two args");



        return b;
    }

    public static void main(String[] args) {

        boolean terminate=false;
        while(terminate==false) {

            Scanner input=new Scanner(System.in);
            String str = input.nextLine();
          try{
            terminate=(isNumeric(str));
               } catch(Exception ex){
     System.out.println(ex);

}

            System.out.println(number1+number2+"\n");
        }

    }

}
class  WrongNumberOfInputsException extends Exception {
    String s;
    public   WrongNumberOfInputsException(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

Open in new window



Output:
2
need two args
0

2
need two args
0

3 4 5
need two args
0

2 3
5

Open in new window

0
for_yanCommented:
I would suggest to add the prompt at teh beggining, otherwise user will not know what to do

import java.util.Scanner;

public class TestCalc1 {

   static int number1;
   static int number2;

   public static boolean isNumeric(String s) throws NumberFormatException, WrongNumberOfInputsException {

    boolean b = false;
    String[] inputs = s.split(" ");

    if(inputs.length==2)
    {
        try {
            number1=Integer.parseInt(inputs[0]);
            number2=Integer.parseInt(inputs[1]);
            b=true;
        }
        catch(NumberFormatException ex){
            throw ex;
        }
    } else
throw new WrongNumberOfInputsException("need two args");



        return b;
    }

    public static void main(String[] args) {

        boolean terminate=false;


        while(terminate==false) {
                 System.out.println("Enter two numbers: ");
            Scanner input=new Scanner(System.in);
            String str = input.nextLine();
          try{
            terminate=(isNumeric(str));
               } catch(Exception ex){
     System.out.println(ex);

}

            System.out.println(number1+number2+"\n");
        }

    }

}
class  WrongNumberOfInputsException extends Exception {
    String s;
    public   WrongNumberOfInputsException(String s){
        this.s = s;
    }

    public String toString() {return s;}

}

Open in new window

Output:
Enter two numbers: 
2
need two args
0

Enter two numbers: 
2 3 4 5 6 
need two args
0

Enter two numbers: 
2 3 8
need two args
0

Enter two numbers: 
24 2
26

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

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.