1

I'm working on a project for school and am stumped at where I am at the moment. When I run my project, the VM seems to be stuck in a loop and will not load (A console should pop up allowing me to input characters for the CombinationLock class setDigit() method). I believe it has something to do with my for loop in my Interface.java class. If anyone could take a look and lead me in the right direction, that'd be much appreciated. Thanks a bunch!

Interface.java

import java.util.*;
public class Interface
{
    public static void main() {
        Scanner in = new Scanner(System.in);
        CombinationLock combo = new CombinationLock();    

        for(int i = 0; i < 3; i++) {
            String ltr = in.nextLine();
            combo.setDigit(ltr.charAt(0), i);
            System.out.println("Digit " + i + " has been set to " + ltr);
        }
    }
}

CombinationLock.java

public class CombinationLock
{
    String[] combo = new String[3];

    public CombinationLock() { }

    public boolean setDigit(char letter, int index) {
        if (Character.isDigit(letter)) {
            return false;
        }
        combo[index] = String.valueOf(letter);
        return true;
    }

    public boolean unlock(String combo) {
        if (combo.length() > 3) {
            return false; //Longer then it can be, not valid
        }

        char[] comboArray = combo.toCharArray();
        for (char c : comboArray) {
            if (Character.isDigit(c)) {
                return false; //Contains numbers, not valid
            }
        }

        boolean valid = true;
        for (int i = 0; i < 3; i++) {
            if (combo.charAt(i) != comboArray[i] && valid == true) {
                valid = false;
                break;
            }
        }

        return valid;
    }
}
Josh Ferrara
  • 757
  • 2
  • 9
  • 25

3 Answers3

2

You have initialized combo array in CombinationLock class with length 0 as String[] combo = {};. This is cause ArrayIndexOutOfBoundsException when you are calling combo.setDigit(ltr.charAt(0), i);. Please correct the initialization. I beleive you want to capture 3 inputs, in that case, please initialize combo in CombinationLock with length 3 as below:

     String[] combo = new String[3];
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
1

Your problem is (the signature of the main method is wrong)

 public static void main() {

it should be

 public static void main(String[] args) {
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • This does not seem to be the error, rather I've found the solution myself (I'll be posting what I did wrong). Thanks for your help :) – Josh Ferrara Oct 29 '12 at 02:11
0

I've found where my error was, using the BlueJ IDE one must output something to the console before it shows up and allows you to input data, therefore it never popped up as I never used System.out.println or System.out.print. After doing so, the console popped up and allowed me to input my data. Thanks you for all your suggestions and help!

Josh Ferrara
  • 757
  • 2
  • 9
  • 25