0

So i have been coding a project in Java and have come across a pause while trying to reference an array list to another class. The first bit of code is the class I am trying to reference the ArrayList too. The second is where I am creating the ArrayLists.

import Game.App;

public class WordPuzzleGenerator {

    public WordPuzzleGenerator() {
        if (Game.SizeOfPuzzle.currentRows == 4){
            threeLetterWordList
        }else if (Game.SizeOfPuzzle.currentRows == 5){
            maximum = 12482;
        }else{
            maximum = 1310;
        }
    }

    Scanner threeLetterScanner = new Scanner(File("3LetterWords.txt"));
    ArrayList<String> threeLetterWordList = new ArrayList<String>();
    while (threeLetterScanner.hasNext()){
        threeLetterWordList.add(threeLetterScanner.next());
    }
    threeLetterScanner.close();

    Scanner fourLetterScanner = new Scanner(File("4LetterWords.txt"));
    ArrayList<String> fourLetterWordList = new ArrayList<String>();
    while (fourLetterScanner.hasNext()){
        fourLetterWordList.add(fourLetterScanner.next());
    }
    fourLetterScanner.close();

    Scanner fiveLetterScanner = new Scanner(File("5LetterWords.txt"));
    ArrayList<String> fiveLetterWordList = new ArrayList<String>();
    while (fiveLetterScanner.hasNext()){
        fiveLetterWordList.add(fiveLetterScanner.next());
    }
    fiveLetterScanner.close();
}
3
  • If I understood well, you have to use getters. Commented May 15, 2015 at 11:25
  • 2
    So what exactly is the problem? Commented May 15, 2015 at 11:27
  • @Mureinik As shown in the wordPuzzleGenerator class the first if statement I am trying to reference to the ArrayList which has been created in App. Commented May 15, 2015 at 11:32

1 Answer 1

1

Use a variable inside the class like:

public class WordPuzzleGenerator {

    ArrayList<String> threeLetterWordList = new ArrayList<String>();
    //...
    public List<String> getThreeLetterWordList() {
       return threeLetterWordList ;
    }
}

THen you can just call myGenerator.getThreeLetterWordList(); to get your list.

Sign up to request clarification or add additional context in comments.

1 Comment

Ah okay that will work thanks you for that @breakline

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.