0
votes
Scanner scan = new Scanner(System.in);   
        int year = -1;
        int yearDuplicate = -1;
        System.out.println("This calculator tells you whether a year that you "
                + "enter is a leap year.\nPlease enter a year:");
        
        do{
            try{
                year = scan.nextInt();
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }    
            
        }while(year % 1 != 0);
            
        
        do{
            if(yearDuplicate % 4 == 0){ 
                if(yearDuplicate % 100 == 0){
                    if(yearDuplicate % 400 == 0){
                        System.out.println(yearDuplicate + " is a leap year.");
                    }else{
                        System.out.println(yearDuplicate + " is not a leap year.");
                    }
                }else{
                    System.out.println(yearDuplicate + " is a leap year.");
                }
            }else{
                System.out.println(yearDuplicate + " is not a leap year.");
            }
            
            System.out.println("Enter another year or enter '-1' to quit:");
            try{
                yearDuplicate = scan.nextInt();
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }
            
        }while(yearDuplicate != -1);

I am trying to make a leap year calculator that also tells you whether you accidentally typed in a string or double instead of an integer. What I would want this program to do is run the "ERROR. Please enter a numerical digit without decimals:" over and over again until the user enters in an integer. However, currently, for the first try catch block, if I input two wrong input types, it gives me this error message:

test
ERROR. Please enter a numerical digit without decimals:
-1 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
BUILD SUCCESSFUL

The second one is at least slightly better, it keeps repeating the result of the last integer I put in (year "is a/not a leap year") but at least it repeats forever unlike the first one.

test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:
test
ERROR. Please enter a numerical digit without decimals:
10 is not a leap year.
Enter another year or enter '-1' to quit:

All I want in for error message to get replayed until you input in the correct number. How do I get rid of the previous input result and how do I fix the fact that the first try catch block only runs twice?

I looked online but it seems if I change one small part of this program, something else breaks. It does exactly what I want it to if I make the condition of the first while loop to be while(year % 1 == 0); but then that means that if it is an integer, the while loop repeats which is the exact opposite of what I want it to do. If I mess with the scan.next();, it'll give me an infinite error loop.

Btw, there are two try catch blocks since there are two opportunities for the user to enter in a year and two places where I might have to correct them. If I put them both in the same while loop, it'll work the first time but then after that, every time I need to enter in two numbers (one for the first try catch which gets overwritten by the second try catch).

Edit: I changed the starting values of year and yearDuplicate to 0, declared the scanner inside the try catch block and got rid of the scan.next(); in both try catch blocks. Now, I'm getting a loop with the first try catch block but it's still carrying over my previous input.

2
What do you imagine the effect of year % 1 != 0 to be?user14387228

2 Answers

1
votes

Your basic 'read until you get an integer' block should look like this:

    do {
        try {
            year = scan.nextInt();
            if (year <= 0) { // see comment
                System.out.println("ERROR. Please enter positive number");
            }
        } catch(Exception e){
            System.out.println("ERROR. Please enter a number "
                               + "without decimals");
            scan.next();
            year = -1;
        }                
    } while (year <= 0);

So if there's non-numeric input, you actively set a value that will cause you to loop, rather than relying on what might have been there already.

Also, you want to make sure the input was positive.

Given that you should be able to figure out the entire program.

Where I said "see comment", perhaps you might want to check for a reasonable year; maybe throw out anything before about 1752 (when Britain and the US adopted the Gregorian calendar).

Your original problem likely was due to the year % 1 != 0 condition. That means "remainder when dividing year by 1 is not 0". It's always true for non-negative values. I'd have to look up how Java implements % for negative values to answer in that case.

0
votes

Fixed the code.

int year = 0;
        int yearDuplicate = 0;
        System.out.println("This calculator tells you whether a year that you "
                + "enter is a leap year.\nPlease enter a year:");
        
        do{
            Scanner scan = new Scanner(System.in);
            try{
                yearDuplicate = scan.nextInt();
                if(yearDuplicate % 4 == 0){ 
                    if(yearDuplicate % 100 == 0){
                        if(yearDuplicate % 400 == 0){
                            System.out.println(yearDuplicate + " is a leap year.");
                        }else{
                            System.out.println(yearDuplicate + " is not a leap year.");
                        }
                    }else{
                        System.out.println(yearDuplicate + " is a leap year.");
                    }
                }else{
                    System.out.println(yearDuplicate + " is not a leap year.");
                }
                System.out.println("Enter another year or enter '-1' to quit:");  
                
            }catch(Exception e){
                System.out.println("ERROR. Please enter a numerical digit "
                        + "without decimals:");
                scan.next();
            }
            
        }while(yearDuplicate != -1);

Basically removed the first try catch block. Made the second try catch block include the entire do while loop. If anyone know what mistakes I made before and why they were wrong, I'd still like to know.