-1

I have an array of a specified sized created and must ask the user to enter an index and print that indexed value. However if they enter something that's not an int I need to use an InputMismatchException to check whether they've entered 'q' or 'Q' to terminate my program. My question is how do I do this. I have catch (InputMismatchException ex) and I've tried if(ex == 'q'...) but I'm not allowed to do this. In what way can I test the object?

EDIT: This is the code I have so far

import java.util.InputMismatchException;
import java.util.Scanner;

public class ExceptionsWithUserInput {
public static void main(String[] args) {
    final int SIZE_OF_ARRAY = 100;
    final int MAX_OF_ARRAY = 10;
    final int MIN_OF_ARRAY = -10;
    float [] randomFloats = new float [SIZE_OF_ARRAY];
    for (int i = 0; i < SIZE_OF_ARRAY; i++){
        randomFloats [i] = (float) ((Math.random () * 10) - 10);
    }
    Scanner input = new Scanner (System.in);
    System.out.println("Generating 100 random numbers in [-10.0,10.0) ...");
    boolean continueInput;
    do {
        try {
            System.out.print ("Please enter an index: ");
            int index = input.nextInt();
            System.out.println(
                    "The random number for index = " + index + " is "
                    + randomFloats[index]);
            continueInput = true;
        }
        catch (IndexOutOfBoundsException ex){
            System.out.println("ERROR: user-supplied index is out of bounds!");
            continueInput = true;
        }
        catch (InputMismatchException ex){
            if (ex == 'q' )
            }
        }
    }

As I was saying I'm not sure how to use what the user inputs

Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
  • I've added the code I have so far. I still haven't completed the do while loop since I"m stuck on this part. – Shreesham Mukherjee May 09 '15 at 08:59
  • 2
    If nextInt() is not an integer, then call next() to have the next token as a String, and test if this String equals "q". An exception is not a char. Comparing them makes no sense. Note that relying on exception for this is bad design. You should use hasNextInt() to test if the next token is an int. – JB Nizet May 09 '15 at 09:04
  • Oh thank you. I understand what you mean. I just had to say if(input.next() == "q" in the catch block. I'm a student and a beginner at java so I'm sorry if this question seemed dumb. – Shreesham Mukherjee May 09 '15 at 09:11
  • Nizet meant, you should check with hasNextInt() so that this exception doesnt occur. You shouldn't use exceptions to check for something – Dude May 09 '15 at 09:18
  • @ShreeshamMukherjee you don't compare Strings with ==. That won't work. Use equals(). – JB Nizet May 09 '15 at 09:21
  • Oh right thanks just caught that and fixed it. – Shreesham Mukherjee May 09 '15 at 09:33
  • @Dude I understood what Nizet was saying but the purpose of this assignment was to handle errors so we had to do it this way. – Shreesham Mukherjee May 09 '15 at 09:36

1 Answers1

-1
if (ex == 'q' ) is not true, you should check  input and write 
if (input == 'q') 
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
fasadat
  • 1,025
  • 1
  • 12
  • 27