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: 

Alright, so It turns out it was much simpler then I thought.
Just add i--; into each of the error cases.

I have also turned against the use of System.err.println(""); as it seems to be really buggy and half of the time doesn't end the line.
Changed toSystem.out.println(""): does the trick without bugs.

Highly highly unlikely that you have found a new bug in System.err.println after all this time. System.err is intended for logging system error messages for the developer/administrator, not for messages to the user. If you mix it with System,out and System.in you can't guarantee the exact order in which it will all come out.

Your solution of i-- after error is a perfectly valid one, but inside an for (i... loop it can be confusing and potentially error-prone. Far better to use a while (validDigitCount < 3) loop and increment validDigitCount each time you get a valid digit. Written that way the code is really clear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.