-2

So I'm taking AP Computer Science A and I need some help I'm on Activity 28 and I have this question:

The removeExtension method is designed to input a name or 3-digit extension number (presented in the form of a String), and remove from the extensions array the first entry it finds that matches either the name or the extension (or both). If such a removal takes place, the size of the extensions array is reduced by 1.

As in Activity 27.1, a match should be registered when comparing names if the only difference is in the use of uppercase or lowercase letters. So "PETER" and "Peter" should match, while "Peter" and "Pete" should not.

Complete the definition of the removeExtension method:

public class MainClass{
    public static PhoneExtension[] extensions = new PhoneExtension[]{
        new PhoneExtension("Christine", "763"),
        new PhoneExtension("Janice", "464"),
        new PhoneExtension("Jon", "564"),
        new PhoneExtension("Peter", "760"),
        new PhoneExtension("Nicholas", "564"),
        new PhoneExtension("Michael", "465"),
        new PhoneExtension("Ryan", "564"),
        new PhoneExtension("Pamela", "467"),
        new PhoneExtension("Janice", "999"),
        new PhoneExtension("Christine", "763")
    };
    public static void printDirectory(){ 
        for (PhoneExtension ext : extensions) 
            System.out.println(ext); 
    } 
    public static void removeExtension(String t){
        //My code starts here.
        PhoneExtension[] temp = new PhoneExtension[extensions.length - 1];
        int index;
        for (int i = 0; i < extensions.length - 1; i++){
            if (t.toLowerCase().equals(extensions[i].getName().toLowerCase()) || t.equals(extensions[i].getExtension())){
                index = i;
                continue;
            }
        }
        for (int i = 0; i < index; i++){
            temp[i] = extensions[i];
            }
        for (int i = index; i < temp.length - 1; i++){
            temp[i + 1] = extensions[i];
        }
        extensions = temp;
        //And ends here.
    }
    public static void main(String[] args){
        removeExtension( "Peter" );
        printDirectory();
    }
}

This throws this error:

MainClass.java:75: error: variable index might not have been initialized
for (int i = 0; i < index; i++){
                    ^

A few things to note are: the PhoneExtension class isn't shown but it has a getName and getExtension method that return it's name or extension. It's constructor looks like: PhoneExtension(String name, String extension). I don't know why we aren't using ArrayLists for this, seems like it could save some trouble but I could be missing something that is keeping us from using them...

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
GusN
  • 63
  • 6
  • if the `if` in your first loop fails `index` will not be initialized ,compiler sees that and complains – Ramanlfc Feb 28 '16 at 15:25
  • The error message is telling you exactly what is wrong -- So initialize the variable. The key to this problem is to critically read all error messages and try to figure out what they're telling you. If you're stuck then search on this site for the error message, as it is quite likely this sort of question has been asked before. – Hovercraft Full Of Eels Feb 28 '16 at 15:28
  • @HovercraftFullOfEels Yes I see the error, the variable should be initialized in the for loop... – GusN Feb 28 '16 at 15:35
  • @Ramanlfc it shouldn't be failing at all... – GusN Feb 28 '16 at 15:36

1 Answers1

0

You need to initialize the local variable index. If i < extensions.length - 1 is never true, then index won't be initialized and that is why the compiler complains.

do this: int index = 0.

Atri
  • 5,511
  • 5
  • 30
  • 40
  • i is 0, extensions.length - 1 is 9, the problem shouldn't be there. If I initialize index as 0 it just removes the first PhoneExtension and not the correct one. – GusN Feb 28 '16 at 15:34
  • Well, even if you think `extensions.length -1` is 9, the compiler would make sure that in the worst case if the condition is not met and `index` is never set then you cannot access it without initializing. Just replace `int index;` with `int index = 0; //or whatever you intend` and see for your self. – Atri Feb 28 '16 at 15:37
  • the if statement is functional, it turns out it was the index not being initialized like you said in your second comment, I remember trying it before with int index = 0; but I guess I changed something else too. – GusN Feb 28 '16 at 15:56