问题描述:

I'm try to do an exception on my codes if ever the user puts a string instead of an integer. My codes will swap the position of the largest index to the smallest index. Can you try to rectify this with me?

import java.util.Scanner;

import java.util.InputMismatchException;

public class ArraySwap

{

static int h;

static Scanner data = new Scanner(System.in);

static int[] list = new int[10];

public static void main(String[] args)throws InputMismatchException

{

System.out.println("Please enter 10 numbers: ");

for(h = 0; h < list.length; h++)

{

try

{

list[h] = data.nextInt();

}

catch(InputMismatchException h)

{

System.out.println("Please re-enter 10 numbers as an exception "

+ h.toString());

continue;

}

}

swap();

}

public static void printArray(int[] list)

{

int counter;

for(counter = 0; counter < list.length; counter++)

System.out.print(list[counter] + " ");

}

public static int smallestIndex(int[] list)

{

int length1 = list.length;

int counter;

int minIndex = 0;

for (counter = 1; counter < length1; counter++)

if (list[minIndex] > list[counter])

minIndex = counter;

return minIndex;

}

public static int largestIndex(int[] list)

{

int length2 = list.length;

int counter;

int maxIndex = 0;

for (counter = 1; counter < length2; counter++)

if (list[maxIndex] < list[counter])

maxIndex = counter;

return maxIndex;

}

public static void swap()

{

System.out.print("List of elements: ");

printArray(list);

System.out.println();

int min_index = smallestIndex(list);

int max_index = largestIndex(list);

int min_num = list[min_index];

System.out.println("Largest element in list is: "

+ list[max_index]);

System.out.println("Smallest element in list is: "

+ list[min_index]);

min_num = list[min_index];

list[min_index] = list[max_index];

list[max_index] = min_num;

System.out.print("Revised list of elements: ");

printArray(list);

System.out.println();

}

}

网友答案:

You are already doing exception handling on the integer inputs:

   try
        {
            list[h] = data.nextInt();
        }
    catch(InputMismatchException h)
        {
            System.out.println("Please re-enter 10 numbers as an exception " 
                + h.toString());
            continue;
        }
    }

Your issue is that in your catch block, you are naming your InputMismatchException object as h. This is also your loop count variable. Change that.

catch(InputMismatchException ex)
    {
        System.out.println("Please re-enter 10 numbers as an exception " 
            + ex.toString());
        continue;
    }

Also your second issue is that the print statement in your catch block is being automatically taken as Scanner input for your next loop. So the program isn't allowing input of any more numbers once an error string has been entered. What you need to do is first use a data.next() to consume your error message.

catch (InputMismatchException ex) {
                 System.out.print("Please re-enter 10 numbers as an exception "
                 + ex.toString());
                 data.next();

            }
相关阅读:
Top