Question

I have my class already set. I am looking to catch exceptions on the part where number is set to the next keyboard integer. I need to catch if the number is 1< and if the user enters the number in word form. I created a custom exception class but i can not get my messages to print i am getting a null message

main code:

import java.util.Scanner;


public class SuperLottoPlus {
public static void main(String[] args) throws LottoLimitException {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many tickets do you want?");
    try {
        int number = keyboard.nextInt();
        for (int i = 0; i < number; i++)
            printTicket(generateNumber());
    } catch (Exception LottoLimitException) {
        System.out.println(LottoLimitException.getMessage());
    }

}
public static boolean exists(int [] array, int value)
{
    for (int i=0; i< array.length; i++)
    {
        if (array[i]== value)
            return true;
    }
    return false;
}

public static void printTicket(int [] array)
{
    for (int i=0; i< array.length-1; i++)
    {
        System.out.print(array[i] +" ");
    }
    System.out.println("MEGA: " + array[5]);
}


public static int[] generateNumber()
{
    int[] array = new int[6];

    int temp = (int) (46 * Math.random()) + 1;
    for (int i=0; i< array.length-1; i++)
    {
        while (exists(array, temp))
        {
            temp = (int) (46 * Math.random()) + 1;  
        }
        array[i] = temp;
    }
    array[5] = (int) (27 * Math.random()) + 1; 
    return array;
}
}

custom exception code

    public class LottoLimitException extends Exception {
    public LottoLimitException()
    {
        super("Sorry, there is a 1 lotto ticket minimum!");
        return; //should print when integer entered is less than 1
    }
    public LottoLimitException(String message)
    {
        super("Please enter a valid interger.");
        return; //should print when words are entered
    }
}
Was it helpful?

Solution

This should get what you are going for

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);
    boolean valid = false;
    while(!valid) {
        System.out.println("How many tickets do you want?");

        try {
            int number = keyboard.nextInt();
            if(number < 1) throw new LottoLimitException();
            for (int i = 0; i < number; i++)
                printTicket(generateNumber());

            valid = true;
        } catch (LottoLimitException e) {  //number is less than 1
            valid = false;
            System.out.println(e.getMessage());
        } catch (Exception e) { //will get here for any other exception, such as invalid number
            valid = false;
            System.out.println("Invalid Format");
        }
    }
}

OTHER TIPS

You tried to catch a generic exception, calling it LottoLimitException. Long shot, I guess that what you need is something like:

public static void main(String[] args) throws LottoLimitException {

    Scanner keyboard = new Scanner(System.in);

    System.out.println("How many tickets do you want?");

    int number = keyboard.nextInt();
    if(number < 1) throw new LottoLimitException();
    for (int i = 0; i < number; i++)
        printTicket(generateNumber());
}

Of course, you need to handle it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top