Hey guys, right now I need the user to either input a 1 or a 2
I am using the following Try-Catch
while (true) try { num1 = Integer.parseInt(kb.nextLine()); break; } catch (NumberFormatException nfe) { System.out.print("That is not a number: "); }
This will limit the possibility to a number, however users can still input any number such as 3, 4, 5, etc.
I have If statements for 1 and 2 and an else for 3
However, how can I re-request an input if any other value is entered. Im slightly confused
Here is the entire code for reference:
package salesperson_part1; //Imports import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; import java.util.Scanner; public class DriverCalculator { public static void main(String args[]){ //Initializations double annualSales; SalesPerson sales; Scanner kb = new Scanner(System.in); int num1; System.out.print("Enter 1 for Total Commission and Annual Sales Calculator,\n" + "Enter 2 for Sales Person Compensation Comparison Calculator: "); while (true) try { num1 = Integer.parseInt(kb.nextLine()); break; } catch (NumberFormatException nfe) { System.out.print("That is not a number: "); } if (num1 == 1) { //Request Total Sales Scanner input=new Scanner(System.in); System.out.print("\nThis program will calculate Total Compensation based" + " upon\nthe percentage met of the $120,000 Annual Sales Target." + "\nThis program will also output a table of possible Total" + " Compensations\nin increments of $5,000 up to 1.5 times your inputted Annual Sales\n\n" + "Please enter your Total Annual Sales: "); //Check for Correct Entry Data try { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String line = in.readLine().trim(); //Check for Null Entry if (line == null || line.length() == 0) { System.out.println("You did not enter a value"); return; } //Declare Annual Sales and Replace Commas/Dollar Signs annualSales = Double.parseDouble(line.replaceAll(",", "").replaceAll("\\$", "")); //Check for numerical value } catch (NumberFormatException exception) { System.out.println("Error: Not a valid sales amount."); return; //Check for IOException } catch (IOException exception) { System.out.println("Error: " + exception.getLocalizedMessage()); return; } //Declare Sales sales=new SalesPerson(annualSales); //Output Total Commission System.out.println("\nYour Total Commission for the year is: $" + SalesPerson.commission); //Output Total Compensation System.out.println("Your Total Compensation for the year is: $"+ String.format("%.2f", sales.getTotalAnnualCompensation())+ "+\n"); //Output Table Headers System.out.println("Total Sales | Total Compensation"); //Set UpperLimit at 150% int upperlimit = (int) ((annualSales/2)/5000); //Calculate Total Compensation in relevance with Total Sales for(int i=0;i<=upperlimit;i++){ sales=new SalesPerson(annualSales); System.out.println(" $"+ annualSales+" | " +"$"+String.format("%.2f", sales.getTotalAnnualCompensation())); annualSales+=5000; } } if (num1 == 2) { System.out.print("\nSales Person Comparison Program\n"); } else { System.out.print("\nYou did not enter 1 or 2\n"); } } }