java - How to read my words.txt into a array? -
this question has answer here:
import java.util.*; import java.io.*; public class hangman { public static void main (string [] args) throws ioexception{ // importing file wordbank // file wordbank = new file("words.txt"); scanner scan = new scanner( new file ("words.txt")); string [] wordbank = new string [20]; while ( scan.hasnext() ){ for(int x = 0; x < 10; x++){ string line = scan.nextline(); wordbank[x] = line; } } system.out.println( wordbank[1]); } }
my error below. im not sure means.
exception in thread "main" java.io.filenotfoundexception: words.txt (the system cannot find file specified) @ java.io.fileinputstream.open(native method) @ java.io.fileinputstream.<init>(unknown source) @ java.util.scanner.<init>(unknown source) @ hangman.main(hangman.java:8)
you need add file called words.txt (containing json data) in same directory java file running. uncomment line says
file wordbank = new file("words.txt");
Comments
Post a Comment