0

in the case that I want to throw an exception when the wrong type of data type is entered, how can I do this with IllegalArgumentException?

if(row < 1) {
        row = 1;
    }
    if(row > 5) {
        row = 5;
    }
    if (row != int) {
        throw new IllegalArgumentException("Please enter appropriate integer.");
    }
    System.out.print("Specify red value (0-255): ");
    int red = console.nextInt();
    if(red < 0) {
        red = 0;
    }
    if(red > 255) {
        red = 255;
    }
    if (red != int){
        throw new IllegalArgumentException("Please enter appropriate integer.");
    }
    System.out.print("Specify green value (0-255): ");
    int green = console.nextInt();
    if(green < 0) {
        green = 0;
    } 
    if(green > 255) {
        green = 255;
    }
    if (green != int){
        throw new IllegalArgumentException("Please enter appropriate integer.");
    }
    System.out.print("Specify blue value (0-255): ");
    int blue = console.nextInt();
    if(blue < 0) {
        blue = 0;
    }
    if(blue > 255) {
        blue = 255;
    } 
    if (green != int){
        throw new IllegalArgumentException("Please enter appropriate integer.");
    }

My main problem is with the "!= int" part. I know for a fact that this is not right, but I am not sure what else to use. Using an else statement will not work either. If someone were to enter a double, string, or boolean input into the scanner instead of an integer, how could I throw an exception for this? I have to do this for an assignment, and they specifically said that I need to use a throw exception for this. Thanks everyone in advance!

1
  • 2
    console.nextInt(), assuming console is of type java.uil.Scanner, reads an int (not a double, not a string, not a boolean), and it already throws an exception if the input is not an int. So, if you got a value in your int variable, you got an int and nothing else. An int variable will never, ever contain anything other than an int. docs.oracle.com/javase/8/docs/api/java/util/…
    – JB Nizet
    Jun 17, 2018 at 8:09

3 Answers 3

1
(red != int)

What are you trying to do

Let me come to the question title "Throwing exception for wrong data types". Well if this is the actual question then you dont need to do anything the line

int red = console.nextInt();

will automatically throw an exception if the data type is anything other than an Integer.

1
  • This is a better answer than mine but you might demonstrate catching the exception from nextInt()
    – Tavi Kohn
    Jun 17, 2018 at 8:49
0
  1. Making them new method for example.

    private void inputColorDetails(){
    if(row < 1) {
            row = 1;
        }
        if(row > 5) {
            row = 5;
        }
        if (row != int) {
            throw new IllegalArgumentException("Please enter appropriate integer.");
        }
        System.out.print("Specify red value (0-255): ");
        int red = console.nextInt();
        if(red < 0) {
            red = 0;
        }
        if(red > 255) {
            red = 255;
        }
        if (red != int){
            throw new IllegalArgumentException("Please enter appropriate integer.");
        }
        System.out.print("Specify green value (0-255): ");
        int green = console.nextInt();
        if(green < 0) {
            green = 0;
        } 
        if(green > 255) {
            green = 255;
        }
        if (green != int){
            throw new IllegalArgumentException("Please enter appropriate integer.");
        }
        System.out.print("Specify blue value (0-255): ");
        int blue = console.nextInt();
        if(blue < 0) {
            blue = 0;
        }
        if(blue > 255) {
            blue = 255;
        } 
        if (green != int){
            throw new IllegalArgumentException("Please enter appropriate integer.");
        }
    }
    
  2. Call them in main as try catch.

    private void Main(){
    try {
        inputColorDetails();
    } catch (NumberFormatException e) {
        System.out.prinln(e.getMessage());
    }  }
    
-1

You could look at using the "instanceof" keyword or Class.isInstance()

var instanceof Integer
Integer.class.isInstance(var)

As JB Nizet noted, the nextInt() method only returns an integer though, so this check shouldn't be necessary.

To get any input and check it afterwards you could do

int color;
String input = console.next();
try {
    color = Integer.parseInt(input)
} catch (NumberFormatException e) {
    throw new IllegalArgumentException("Input \"" + input + "\" was not an integer");
}

(Code off the top of my head, please correct me if I'm wrong)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.