-1
import java.util.*;

class Project11
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        int num = 0;
    do
    {
        System.out.print("Enter int in range 1..100 inclusive: ");
        try
        {
            num = input.nextInt();
            if(num < 1 || num > 100)
            throw new NumberOutOfRangeException(); 
        }
        catch(InputMismatchException e)
        {   
            System.out.println("Input was not an integer");
        }
        catch(NumberOutOfRangeException e)
        {
            //System.out.println(e);
        }
        catch(Exception e)
        {
            System.out.println(e);
            System.exit(0);
        }
    }while(num < 1 || num > 100);

    System.out.println("Thank you. You entered " + num);
}
    class NumberOutOfRangeException extends Exception
    {
        String s;

        public NumberOutOfRangeException()
        {
            s = "Number out of range. Must be in 1..100";
        }
        public String toString()
        {
            return s;
        }

    }

When I put the NumberOutOfRangeException class above main I do not get that error. How do I make it so it can go beneath main without getting that error? Ive tried changing/adding some brackets in different places but I still get that error regardless. Thanks for any help!

Jack Owen
  • 1
  • 4
  • 1
    You are missing a `}` somewhere, either before or after the declaration of the exception. Is `NumberOutOfRangeException` supposed to be an inner class? – luk2302 Nov 13 '19 at 14:53

2 Answers2

0

There are two problems in your code:

  • missing } at the end
  • calling no-static new NumberOutOfRangeException() in static main() method. You have to mark as static class or move outside of Project11 class

Here is a working version:

class Project11 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = 0;
        do {
            System.out.print("Enter int in range 1..100 inclusive: ");
            try {
                num = input.nextInt();
                if (num < 1 || num > 100)
                    throw new NumberOutOfRangeException();
            } catch (InputMismatchException e) {
                System.out.println("Input was not an integer");
            } catch (NumberOutOfRangeException e) {
                //System.out.println(e);
            } catch (Exception e) {
                System.out.println(e);
                System.exit(0);
            }
        } while (num < 1 || num > 100);

        System.out.println("Thank you. You entered " + num);
    }

    static class NumberOutOfRangeException extends Exception {
        String s;

        public NumberOutOfRangeException() {
            s = "Number out of range. Must be in 1..100";
        }

        public String toString() {
            return s;
        }

    }
}
Andriy Budzinskyy
  • 1,971
  • 22
  • 28
  • Awesome, thank you! Also, why is it that if I put the class above the main I do not need to add a "static"? – Jack Owen Nov 13 '19 at 15:20
  • Check [this answer](https://stackoverflow.com/questions/975134/why-cant-we-have-static-method-in-a-non-static-inner-class) – Andriy Budzinskyy Nov 13 '19 at 15:22
  • I actually just came across another problem. When I run the code and enter an invalid number, it doesn't throw the NumberOutOfRangeException it just goes back to main and prints "Enter int in range 1..100 inclusive: " – Jack Owen Nov 13 '19 at 15:27
  • It happens because you catch `NumberOutOfRangeException` and do nothing: ` //System.out.println(e);` – Andriy Budzinskyy Nov 13 '19 at 15:28
0

You are missing a closing } at the end, to close your outer class. Also you'd need to make your NumberOutOfRangeException inner class static, as you're throwing it from your static method main. Full correct code :

class Project11{

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int num = 0;
        do {
            System.out.print("Enter int in range 1..100 inclusive: ");
            try {
                num = input.nextInt();
                if (num < 1 || num > 100) {
                    throw new NumberOutOfRangeException();
                }
            } catch (InputMismatchException e) {
                System.out.println("Input was not an integer");
            } catch (NumberOutOfRangeException e) {
                //System.out.println(e);
            } catch (Exception e) {
                System.out.println(e);
                System.exit(0);
            }
        } while (num < 1 || num > 100);

        System.out.println("Thank you. You entered " + num);
    }

    static class NumberOutOfRangeException extends Exception {

        String s;

        public NumberOutOfRangeException() {
            s = "Number out of range. Must be in 1..100";
        }

        public String toString() {
            return s;
        }
    }
}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
  • Awesome thank you! Also, why is it that if I put the class above the main I do not need to add a "static"? – Jack Owen Nov 13 '19 at 15:20