1

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();
    }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Hervie
  • 23
  • 6
  • it is very unclear what you are asking. can you please be more specific? – Kick Buttowski Dec 10 '14 at 02:40
  • Hi Kick, I'm asking to create an exemption in my main program then it will continue after it displays the InputMismatchException. – Hervie Dec 10 '14 at 02:43
  • what will happen after that? – Kick Buttowski Dec 10 '14 at 02:44
  • What is wrong with your code now? – Scott Hunter Dec 10 '14 at 02:47
  • Okay. My program read in 10 ints from the keyboard, and store them in an array. It finds the position (or index) of the maximum and minimum values in the array, and swap them (move the biggest element to the position of the smallest, and move the smallest element to the position of the biggest). What I'm trying to figure now is how to create a InputMismatchException if ever I entered a string. – Hervie Dec 10 '14 at 02:49
  • Is your problem that when you enter a string once, you code ignore all input integer after? – Fumu 7 Dec 10 '14 at 03:04

1 Answers1

1

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();

            }
aparna
  • 333
  • 1
  • 8
  • Thank you so much for answering Aparna Sridhar. The problem is when I run the program the output is below. – Hervie Dec 10 '14 at 03:10
  • Please enter 10 numbers: 1 2 3 4 5 e Re-enter input number as an exception: java.util.InputMismatchException Re-enter input number as an exception: java.util.InputMismatchException Re-enter input number as an exception: java.util.InputMismatchException Re-enter input number as an exception: java.util.InputMismatchException Re-enter input number as an exception: java.util.InputMismatchException List of elements: 1 2 3 4 5 0 0 0 0 0 Largest element in list is: 5 Smallest element in list is: 0 Revised list of elements: 1 2 3 4 0 5 0 0 0 0 – Hervie Dec 10 '14 at 03:11
  • Yes so that is occurring because the error that you print out in your catch block is being taken as input for your Scanner in the next iteration of your for loop. Since that's a string you keep getting the same issue again. Using a data.next() will consume this error and then you can go back to doing integer inputs in your next iteration. If you want to correct your existing input, ask for the input again in your catch block using list[h]=data.nextInt(); – aparna Dec 10 '14 at 03:15
  • You're awesome! That worked, however, it only work once, how can it work consistently? – Hervie Dec 10 '14 at 03:25
  • Could you elaborate by what you mean by it only worked once? Could you give me the inputs you tried? – aparna Dec 10 '14 at 03:31
  • I tried to put "w" then it worked, after that, I put "w" again and it terminated the program. – Hervie Dec 10 '14 at 03:33
  • Are you using the code that I provide in the answer above ? When I run the program I can keep entering integers till all 10 integers have been entered. Could you provide the updated code you are using? – aparna Dec 10 '14 at 03:37
  • Yes I did. It's just the same code I just added yours. Try putting consistent strings such as "w" and it will show the error. – Hervie Dec 10 '14 at 03:40
  • that's right... if you use list[h]=data.nextInt(); within the catch block, you don't error check on it again so if you re enter bad values it will throw an exception. If you use just data.next() and then continue with your input, then your badly entered number will be taken as 0. – aparna Dec 10 '14 at 03:45
  • Please enter 10 numbers: w Re-enter input as exception: java.util.InputMismatchException Enter 10 intergers: w Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Unknown Source) at java.util.Scanner.next(Unknown Source) at java.util.Scanner.nextInt(Unknown Source) Here's my out put when I put consistent strings. – Hervie Dec 10 '14 at 03:47
  • Sorry I think it's just me being a newbie in Java. So if ever you put the wrong answer twice such as strings it will automatically terminate the program???? – Hervie Dec 10 '14 at 03:48
  • Nope... it doesn't need to, refer edits I made to the code above. It won't terminate for multiple wrong inputs. – aparna Dec 10 '14 at 03:51