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

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

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 3
1 Solution
 
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
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now