I'm trying to make this as easy as possible but I'm stuck. Inside the for loop, if the input entered was not a number or wasn't valid, it moves on, but I don't want it to. I want it to repeat that interation. Is there a possible way to do this?
package com.geodox.combolock;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class ComboLock
{
public static void main(String[] args)
{
new ComboLock();
}
private ComboLock()
{
Combination combo = new Combination();
while(!(getInputCombo() == combo.toString()))
{
System.out.println("Wrong! Try again!");
}
System.out.println("Combo Found: " + combo.toString());
}
private String getInputCombo()
{
InputStreamReader reader = new InputStreamReader(System.in);
BufferedReader input = new BufferedReader(reader);
int[] inputCombo = new int[3];
for(int i = 0; i < 3; i++)
{
int temp = -1;
System.out.print("Enter Digit for Combo " + (i + 1) + ": ");
try
{
temp = Integer.parseInt(input.readLine());
}
catch (IOException e)
{
System.err.println("ERROR: " + e);
}
catch (NumberFormatException e)
{
System.err.println("ERROR: Please input a number");
}
if(temp < 0 | temp > 9)
{
System.err.println("ERROR: Enter a value between 0 - 9");
}
else
inputCombo[i] = temp;
}
return Arrays.toString(inputCombo);
}
}
package com.geodox.combolock;
import java.util.Arrays;
import java.util.Random;
public class Combination
{
int[] combo;
public Combination()
{
combo = new int[3];
newCombo();
}
private int[] newCombo()
{
Random rand = new Random();
for(int i = 0; i < 3; i++)
{
combo[i] = rand.nextInt(10);
}
return combo;
}
public String toString()
{
return Arrays.toString(combo);
}
}
Example Input/Output:
Enter Digit for Combo 1: 2
Enter Digit for Combo 2: -7
ERROR: Enter a value between 0 - 9
Enter Digit for Combo 3: 12
ERROR: Enter a value between 0 - 9
Wrong! Try again!
Enter Digit for Combo 1:
Expected Wrong Case:
Enter Digit for Combo 1: 2
Enter Digit for Combo 2: -7
ERROR: Enter a value between 0 - 9
Enter Digit for Combo 2: 3
Enter Digit for Combo 3: -7
ERROR: Enter a value between 0 - 9
Enter Digit for Combo 3: 4
Wrong! Try again!
Enter Digit for Combo 1: