Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Exception Write an application that asks the user to \"Enter two integers s

ID: 3691748 • Letter: J

Question

Java Exception

Write an application that asks the user to "Enter two integers separated by a comma". You will need to use a String data type to get this input. The String class has a method name split( ) that takes a String and converts it to an array of type String based on a separator that you provide as the argument to the split( ) method.

For example:

String xStr = "6-3-2";

String vals[];

vals = xStr.split("-");

for( int i = 0; i < vals.length; i++)

System.out.print( vals[i] + " ");

The output would be: 6 3 2

Do the following error checking. Some of the error checking may use try/catch and some of it may be done using if statements.

1. Display an error if the user entered fewer than 2 values.

2. Display an error if the user entered more than 2 values.

3. Display an error if either value cannot be converted to an integer.

You should not let the user continue until they have entered two valid values that can be converted. Save the file as SplitInput.java

Explanation / Answer

public class JavaStringSplitExample{

public static void main(String args[]){

   int count=0;
   String str = "2,3,4";
   String[] temp;
   String delimiter = ",";
   temp = str.split(delimiter);
        for(int i =0; i < temp.length ; i++)
           System.out.println(temp[i]);
       for(int i =0; i <= temp[i] ; i++)
           try{
               count=count+1;
               if(count <2 || count >2)
               System.out.println("Inputed values are more than or less than 2");  
           }catch(ArithmeticException e){System.out.println(e);}
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote