0

I am working through an assignment for my course. I was asked to create a valid index method that checked whether the int entered was a valid index for my ArrayList. This method compiled fine and worked fine.

A further exercise asked me to then use this validIndex method within other methods. I have tried to do that within my removeFile method. The removeFile method is meant to call the validIndex method in order to check whether the index parameter of removeFile is a valid index for the ArrayList. However my file now refuses to compile giving me the error

cannot find symbol - method validIndex()

The code is below:

import java.util.ArrayList;

/**
 * A class to hold details of audio files.
 * 
 * @author David J. Barnes and Michael Kölling
 * @version 2011.07.31
 */
public class MusicOrganizer
{
    // An ArrayList for storing the file names of music files.
    private ArrayList<String> files;     
    /**
     * Create a MusicOrganizer
     */
    public MusicOrganizer()
    {
        files = new ArrayList<String>();
    }

    /**
     * Add a file to the collection.
     * @param filename The file to be added.
     */
    public void addFile(String filename)
    {
        files.add(filename);
    }

    /**
     * Return the number of files in the collection.
     * @return The number of files in the collection.
     */
    public int getNumberOfFiles()
    {
        return files.size();
    }

    /**
     * List a file from the collection.
     * @param index The index of the file to be listed.
     */
    public void listFile(int index)
    {
        if(index >= 0 && index < files.size()) {
            String filename = files.get(index);
            System.out.println(filename);
        }
    }

    /**
     * Remove a file from the collection.
     * @param index The index of the file to be removed.
     */
    public void removeFile(int index)
    {
        if(files.validIndex() = true){
            files.remove(index);
        }
    }

    // Problem with this method. If ArrayList is empty then the indexes variable returns minus 1
    public void checkIndex(int index)
    {
        int size = files.size();
        int indexes = size - 1;
        if (index >= 0 && index <= indexes){
            System.out.println("");
        }
        else {
            System.out.println("That is not a valid index number.");
            System.out.println("The index should be between 0 and " + indexes);
        }
    }

    public boolean validIndex(int index)
    {

        if (index >= 0 && index <= files.size()-1){
            return true;
        }
        else {
            return false;
        }
    }



}

If someone could point out why my code won't compile that would be appreciated.

Alfabravo
  • 7,493
  • 6
  • 46
  • 82
ed2101
  • 13
  • 4
  • 1
    I think you meant to use `validIndex(index)` instead of `files.validIndex() = true`. `files` is a `List` which does not contain this method. – Zircon Oct 06 '16 at 14:50
  • You implemented the method `validIndex()' with a parameter from type int. You try to call this method without any parameter. That's a big difference, in order you can override methods. – Reporter Oct 06 '16 at 14:51
  • 1
    It's out of scope, but validIndex can has one line - just `index >= 0 && index <= files.size()-1`. You're learning, so maybe it will help to improve your code readability :) – T. Gawęda Oct 06 '16 at 14:53

2 Answers2

1

If you are trying to call your own method validIndex, you are calling it wrong.

To call your validIndex method, you should do the following:

public void removeFile(int index)
    {
        if(this.validIndex(index)){
            files.remove(index);
        }
    }

Note that files is an object of the ArrayList class. So the statement

files.validIndex()

points to method called validIndex() in the ArrayList class, which doesn't exist. The method exists in your class so the only way of accessing it is with the current object. Change the if statement to

if(this.validIndex(...) == true){ ... }

or simply

if(validIndex(...) == true){ ... }
progyammer
  • 1,498
  • 3
  • 17
  • 29
Daniel Squires
  • 338
  • 3
  • 13
  • Thank you, that works! Can I ask you for a small explanation of why it works though? Do method calls to methods within the same class always require the this prefix in java? – ed2101 Oct 06 '16 at 14:53
  • @ed2101 No, you don't need to add `this`. It is very helpful if you i.e. have field named `test` and method parameter is also called `test` and you want to access field, not parameter. Here `this` may be omitted – T. Gawęda Oct 06 '16 at 14:55
0

this line.

if(files.validIndex() = true){

1) What index is a valid index? You need to use a parameter, as defined by the validIndex method

2) files does not have a validIndex method, since it is a List object. Your method is defined in your current class, so do not prefix the method call with files. You can optionally replace it with this

3) You are using assignment operation, not boolean condition. Either remove it because checking for true is redundant, or use two equals

So.

if (validIndex(index)) {
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245