How to Reject Invalid input

java

#1

// program runs fine just need help to figure out how to reject Invalid input
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class SurveyData {

public static void main(String[] args) {

	int rating;
	int sentinel = 0;
	
	// Create Scanner object for user input
	Scanner input = new Scanner(System.in);

	try{
		// Create output file
		PrintWriter outputFile = new PrintWriter("data.txt");
		
		while (sentinel != -1)
		{
			System.out.println("On a scale of 1-5, " +
					"rate your school cafeteria " +
					"(5 being the best) - press -1 to quit");
			rating = input.nextInt();
			sentinel = rating;
			if (sentinel != -1)		
				outputFile.println(rating);
			
		} // End While
		
		
		System.out.println("Data written to file.");
		
		
		
		
		outputFile.close();
		input.close();
	} // End try
	
	catch (IOException e) {
		
		System.out.println("There was an error creating the " +
					"file. Program closing...");
		System.exit(1);
	}

} // End Main

} // End Class


#2

You could make a checking function for the input in order to verify if it is the desired variable type like integer, string, etc. You can find a lot of examples on google like this one.