0

My problem is that I am unable to assign a value from a .txt with a list of words, to an array. I believe the issue is that I am asking for something that is not yet available, like asking for something from the future without knowing it. This is my code, any help will be appreciated along with any tips.

File words = new File("wordList.txt"); //document with words

String wordToArray = new String();
String[] arrWord = new String[3863]; // number of lines
Scanner sc = new Scanner(words);
Random rWord = new Random();
int i = 0;


do
{
    wordToArray = sc.next(); //next word
    arrWord[i] = wordToArray; //set word to position
    i++;  //move to next cell of the array  
    sc.nextLine();  //Error occurs here
}while(sc.hasNext());
Rick
  • 3,240
  • 2
  • 29
  • 53
NoviceCoder
  • 9
  • 1
  • 1
  • 4
  • Add a specific error that you are getting. What about this code is not working? – markbernard Feb 24 '15 at 20:09
  • NoSuchElementException: No line found – NoviceCoder Feb 24 '15 at 20:10
  • The stack trace should have a line number that points to your code. Which line is it in the code above? Do you have to use an array? An ArrayList would provide you virtually unlimited capacity. – markbernard Feb 24 '15 at 20:12
  • Sorry on the line of "sc.nextLine();" 2nd to last. – NoviceCoder Feb 24 '15 at 20:12
  • @markbernard I don't quite get what your getting at regards to what line. I do not have to use an Array, I am fairly new to Java and programming itself and thus lack knowledge i.e. not knowing about ArrayLists. – NoviceCoder Feb 24 '15 at 20:15

2 Answers2

0
while(sc.hasNext()) {
    sc.nextLine();  //This line should be first.
    wordToArray = sc.next(); //next word
    arrWord[i] = wordToArray; //set word to position
    i++;  //move to next cell of the array  
}

You just have your order of operations wrong. sc.hasNext() should occur before getting the next line.

I thought you might be getting an ArrayOutOfBoundsException. If you use an ArrayList that won't happen. This is how you could use an array list.

String wordToArray = new String();
List<String> arrWord = new ArrayList<String>(); 
Scanner sc = new Scanner(words);
Random rWord = new Random();
while(sc.hasNext()) {
    sc.nextLine();  //This line should be first.
    wordToArray = sc.next(); //next word
    arrWord.add(wordToArray); //set word to position
}
int i = arrWord.size();
markbernard
  • 1,412
  • 9
  • 18
0

You're asking for sc.nextLine() before you have the conditional sc.hasNext().

First, you should switch the do...while loop for a while loop:

while(sc.hasNext()) {
  wordToArray = sc.next(); // Reads the first word on the line.
  ...
  sc.nextLine(); // Reads up to the next line.
}

to make sure that more data is available to be read before trying to read it. Then, you should also change sc.hasNext() to sc.hasNextLine() to make sure that there is another line in the file, not just another token:

while(sc.hasNextLine()) {
  ...
}

The issue is that, when you loop through the last line of the .txt file, you ask for the next line (.nextLine()) before knowing if the file has another line to give you (.hasNextLine()).

In general, it's better to use a while loop instead of a do...while loop to avoid things like this. In fact, there's almost never a situation where a do...while loop is actually necessary.

Jon Egeland
  • 12,470
  • 8
  • 47
  • 62