1

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
    }
}

2 Answers2

1

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");
        }
    }
}
jlars62
  • 7,183
  • 7
  • 39
  • 60
  • Thank you, huge improvement compared to before, but i am still getting a null message `How many tickets do you want? 0 Sorry, there is a 1 lotto ticket minimum! null How many tickets do you want? 1 8 44 24 18 14 MEGA: 16 ` – user3558183 May 12 '14 at 23:11
  • @user3558183 The null is coming from your printTicket method. Which is a different problem. – jlars62 May 12 '14 at 23:12
  • okay, and how do i get the other message to print when the number is entered as say "five" instead of "5"? – user3558183 May 12 '14 at 23:13
  • never mind did not see that part of the code but i am now stuck in an infinite loop when user enters a word – user3558183 May 12 '14 at 23:16
0

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.

b3by
  • 114
  • 4
  • 15
  • `System.out.println("How many tickets do you want?"); try { int number = keyboard.nextInt(); for (int i = 0; i < number; i++) if(number < 1) throw new LottoLimitException(); } catch (LottoLimitException e) { System.out.println(e.getMessage()); } printTicket(generateNumber()); }` i am not getting error message it is just printing the ticket numbers anyways – user3558183 May 12 '14 at 22:50
  • Let me be clearer. Get rid of the try/catch block, just throw an exception if the number inserted is lower than 1. And please, fix your indentation. – b3by May 12 '14 at 22:52
  • okay i handled it and am now getting somewhere but it is printing it and terminating the program. it was also printed like this: Exception in thread "main" LottoLimitException: Sorry, there is a 1 lotto ticket minimum! at SuperLottoPlus.main(SuperLottoPlus.java:12) – user3558183 May 12 '14 at 23:00
  • This is what an exception is supposed to do. Probably you would like something like: ```int number = keyboard.nextInt(); while(number < 1) { System.out.println("1 ticket minimum! Retype!"); number = keyboard.nextInt(); }``` – b3by May 12 '14 at 23:02
  • i got it to print as a regular line of text now, but in my console it is showing this: How many tickets do you want? 0 Sorry, there is a 1 lotto ticket minimum! Exception in thread "main" LottoLimitException at SuperLottoPlus.main(SuperLottoPlus.java:12) – user3558183 May 12 '14 at 23:02
  • this would work but my instructions `In your SuperLottoPlus class (or using my solution to assignment 4), throw a LottoLimitException if the user enters any number less than 1 for the number of ticket's they want. (3 point) Add another catch block to catch the excpetion thrown if the user enters an incorrect format for the number of tickets, such as "five" or "5.0". This should prevent your program from crashing or throwing a run time excpetion. When this exception is caught, the program should not crash and should display "Sorry, please enter the number of tickets as an integer".` – user3558183 May 12 '14 at 23:08