Note that there are some explanatory texts on larger screens.

plurals
  1. POJava scanner: Getting "java.util.NoSuchElementException" error, not sure why
    primarykey
    data
    text
    <p>I'm writing a word search program that scans in from a text file. The text file contains two integers (the dimensions of the puzzle, which in this case is 10x10), followed by the puzzle, followed by another integer (the amount of words to find), and finally the words to be found. I am trying to write the code so that the program will execute a for loop which will scan the next line in, then add each character in the string to the 2D array. However, the scanner continues seems to continue through the file and eventually "out of bounds" on the file, even though, according to the for loop, it should run no more than 10 times (the dimension of the puzzle is 10x10)</p> <p>Here is the problematic code:</p> <pre><code>Scanner scan = new Scanner(new File("puzzle.txt")); int rows = scan.nextInt(); int columns = scan.nextInt(); int [][] wordSearch = new int[rows][columns]; for (int i = 0; i &lt; rows; i++){ for (int j = 0; j &lt; columns; j++){ String temp = scan.nextLine(); for (int k = 0; k &lt; temp.length(); k++){ wordSearch[i][j] = temp.charAt(k); } } } </code></pre> <p>Here is the puzzle.txt:</p> <ul> <li>10 10</li> <li>WVERTICALL</li> <li>ROOAFFLSAB</li> <li>ACRILIATOA</li> <li>NDODKONWDC</li> <li>DRKESOODDK </li> <li>OEEPZEGLIW</li> <li>MSIIHOAERA</li> <li>ALRKRRIRER</li> <li>KODIDEDRCD</li> <li>HELWSLEUTH</li> <li>10</li> <li>WEEK</li> <li>FIND</li> <li>RANDOM</li> <li>SLEUTH</li> <li>BACKWARD</li> <li>VERTICAL</li> <li>DIAGONAL</li> <li>WIKIPEDIA</li> <li>HORIZONTAL</li> <li>WORDSEARCH</li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload