Siddharth Choudhary
Siddharth Choudhary

Reputation: 1129

Scanner shows exception InputMisMatchException

static long arrayMaxMin[];
    public static void main(String[] args) {


    try{    Scanner in = new Scanner(System.in);
            long[] arr = new long[5];

            for(int arr_i=0; arr_i < 5; arr_i++){

                arr[arr_i] =Long.valueOf(Math.abs(in.nextLong()));
              }
           arrayMaxMin = calculate(arr);
    }catch(InputMismatchException e)
    {
        e.getMessage();
    }
      try{  long[] array = findMaxMin(arrayMaxMin);
        for(int ij=0;ij<array.length;ij++){
            System.out.print(array[ij]+" ");
        }
      }catch(NullPointerException e){
          e.getMessage();
      }
} 

     public static long[] calculate(long[] arrayMaxMin ){
    int max=0,min=0;
    long[] outputarray={0,0,0,0,0};
    for(int i=0;i<arrayMaxMin.length;i++){
        for(int j=0;j<arrayMaxMin.length;j++){
            if(i==j){
                //do nothing
            }else {
                outputarray[i] = outputarray[i]+arrayMaxMin[j];
            }
        }
    }
return outputarray;
}

     public static long[] findMaxMin(long[] arr){
       long max=0,min=0;
       try{  for(int i=0;i<arr.length;i++){
           if(max<arr[i]){
               max= arr[i];
           }else{
               min= arr[i];
           }
       }
      long output[]={min,max};
       return output;
   }catch(NullPointerException e){
       e.getMessage();
       return null;
   }
     }

well i am trying hackerrank question here, and i don't get the possible reason for getting some testcases failing, in MIN-MAX problem.

i don't get it, what is lacking here or which case is i am lacking in ,please help me anyone ,well i presume that what i am lacking is if input is such of 7777777777777777777777 than it fails, can anyone help the problem goes like

Given five positive integers, find the minimum and maximum values that can be calculated by summing exactly four of the five integers. Then print the respective minimum and maximum values as a single line of two space-separated long integers.

Input Format

A single line of five space-separated integers.

Constraints

Each integer is in the inclusive range . {1,10^9}

Print two space-separated long integers denoting the respective minimum and maximum values that can be calculated by summing exactly four of the five integers. (The output can be greater than 32 bit integer.)

As you can see, the minimal sum is and the maximal sum is . Thus, we print these minimal and maximal sums as two space-separated integers on a new line.

Hints: Beware of integer overflow! Use 64-bit Integer.

Upvotes: 0

Views: 640

Answers (2)

Chinmay jain
Chinmay jain

Reputation: 999

7777777777777777777777 is not a valid long.

Try using Double instead of long. Try this:

static double arrayMaxMin[];

public static void main(String[] args) {

    try {
        Scanner in = new Scanner(System.in);
        double[] arr = new double[5];

        for (int arr_i = 0; arr_i < 5; arr_i++) {

            arr[arr_i] = in.nextDouble();
        }
        arrayMaxMin = calculate(arr);
        System.out.println(arrayMaxMin);
    } catch (InputMismatchException e) {
        e.printStackTrace();
    }
    try {
        double[] array = findMaxMin(arrayMaxMin);
        for (double element : array) {
            System.out.print(element + " ");
        }
    } catch (NullPointerException e) {
        e.getMessage();
    }
}

public static double[] calculate(double[] arrayMaxMin) {
    int max = 0, min = 0;
    double[] outputarray = { 0, 0, 0, 0, 0 };
    for (int i = 0; i < arrayMaxMin.length; i++) {
        for (int j = 0; j < arrayMaxMin.length; j++) {
            if (i == j) {
                // do nothing
            } else {
                outputarray[i] = outputarray[i] + arrayMaxMin[j];
            }
        }
    }
    return outputarray;
}

public static double[] findMaxMin(double[] arr) {
    double max = 0, min = 0;
    try {
        for (double element : arr) {
            if (max < element) {
                max = element;
            } else {
                min = element;
            }
        }
        double output[] = { min, max };
        return output;
    } catch (NullPointerException e) {
        e.getMessage();
        return null;
    }
}

Upvotes: 0

the fisrt thing that it wrong here is the fact that you are swallowing the exeptions...

here:

catch(InputMismatchException e) {
    e.getMessage();
}

should be

 catch (InputMismatchException e) {
     e.printStackTrace();
 }

THEN you will be able to see why the app is crashing:

java.util.InputMismatchException: For input string: "7777777777777777777777"

this input: 7777777777777777777777 is not a valid long...

that is why you got from the professor the hint: Use 64-bit Integer. but 7777777777777777777777 is bigger than that... so is an invalid input making the scanner to crash trying to read that as a long..

Upvotes: 1

Related Questions