Hi,

I am writing an application class for a Lunar Calendar I created in a service class and I have to make sure it is able to detect that the year entered is not in the range of values(I have done this in the service class using a switch statement) and be able to detect if the year is a valid integer. I need a little help with the last one.

Here is what I have so far:

package classes;

import java.util.InputMismatchException;
import java.util.Scanner;

public class LunarCalendarApp {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

final int SENTINEL = -1;
boolean validInput = false;

System.out.println("Enter a year:");
int someYear = in.nextInt();

while(someYear != SENTINEL){

LunarCalendar calen = new LunarCalendar (someYear);
calen.Calendar();

do {

try {

System.out.println("Enter a year:");
validInput = in.hasNext();
someYear = in.nextInt();

}

catch (InputMismatchException e)
{ System.out.println("This is not an integer!");
in.next();
}

}while(!validInput);

}
}

}

To detect if the year entered is a valid integer, you can modify your code as follows:

```java
package classes;

import java.util.InputMismatchException;
import java.util.Scanner;

public class LunarCalendarApp {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

final int SENTINEL = -1;
boolean validInput = false;

System.out.println("Enter a year:");
int someYear;

while((someYear = in.nextInt()) != SENTINEL){
do {
try {
validInput = true;
} catch (InputMismatchException e) {
System.out.println("This is not an integer!");
in.next();
validInput = false;
}
} while(!validInput);

LunarCalendar calen = new LunarCalendar (someYear);
calen.Calendar();

System.out.println("Enter a year:");
}
in.close();
}
}
```

In this code, the `do-while` loop will repeatedly prompt the user for input until a valid integer is entered. If an input mismatch exception is caught, the code will display an error message and clear the input stream by calling `in.next()`. This ensures that your program will continue to prompt for valid input until a valid integer is provided.

Note that I added `in.close()` at the end to close the Scanner object, which is a good practice to prevent resource leaks.

You are on the right track! To detect if the year entered is a valid integer, you can use a combination of a try-catch block and a do-while loop. Here's how you can modify your code to achieve this:

```java
package classes;

import java.util.InputMismatchException;
import java.util.Scanner;

public class LunarCalendarApp {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

final int SENTINEL = -1;
boolean validInput = false;
int someYear = 0; // Initialize the year variable

System.out.println("Enter a year:");

do {
try {
validInput = in.hasNextInt(); // Check if input is an integer
someYear = in.nextInt(); // Read the integer input

if (!validInput) {
System.out.println("This is not an integer!");
in.next(); // Clear the invalid input from the scanner
}
} catch (InputMismatchException e) {
System.out.println("This is not an integer!");
in.next(); // Clear the invalid input from the scanner
}
} while (!validInput);

while (someYear != SENTINEL) {

LunarCalendar calen = new LunarCalendar(someYear);
calen.Calendar();

System.out.println("Enter a year:");
someYear = in.nextInt();
}
}
}
```

In this modified code, we have added a do-while loop that will continue until a valid integer input is provided for the year.

Inside the loop, we use `in.hasNextInt()` to check if the input is an integer. If it returns true, we proceed to read the integer with `in.nextInt()`. If it returns false, we output an error message and clear the invalid input with `in.next()`.

After the loop, we continue with the rest of your code to create the `LunarCalendar` object and display the calendar.

I hope this helps! Let me know if you have any further questions.