I want the compiler to randomly
choose a word from text instead of printing everything from a text file. Right now the code below is printing everything from the text file. I think there is something wrong with my getWord
method because when I call the getWord
method from the main function I get an error
.
public class TextFile {
protected static Scanner file;
protected static List<String> words;
public TextFile(){
words = openFile();
}
private List<String> openFile() {
//List<String> wordList = new ArrayList<String>();
try {
file = new Scanner(new File("words.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (Exception e) {
System.out.println("IOEXCEPTION");
}
return words;
}
public void readFile() throws FileNotFoundException {
//ArrayList<String> wordList = new ArrayList<String>();
while(file.hasNext()){
String a = file.nextLine();
//Collections.shuffle(words);
//String pickWord = words.get(1);
//String[] a =
System.out.println(a);
}
}
public void closeFile() {
file.close();
}
public String getWord() {
Random r = new Random(words.size());
String randomWord = words.get(r.nextInt());
//System.out.println(randomWord);
return randomWord;
}
public static void main(String[] args) throws FileNotFoundException {
try {
TextFile file = new TextFile();
file.openFile();
file.readFile();
file.closeFile();
} catch (Exception e) {
System.out.println("IOEXCEPTION");
}
}
}
in the openfile method you are retuning a "word" variable which is null assign value to the variable
the error is coming from the {getword();} because you are accessing properties of a null variable its a error
public List<String> readFile() throws FileNotFoundException {
while(file.hasNext()){
String a = file.nextLine();
words.add(a);
System.out.println(a);
}
return words;
}
inside the open file method at the return statement line call "return readfile();" and try ur code\\
no need to call readfile method inside the main method
You are getting exception while calling getWord method as it throws IndexOutOfBoundsException
at line String randomWord = words.get(r.nextInt());
.
PFB correction to getWord
method:
public String getWord() {
//You can use any approach..Random or Collections
//Random r = new Random();
//String randomWord = words.get(r.nextInt(words.size()));
Collections.shuffle(words);
String randomWord = words.get(1);
return randomWord;
}
Again you should populate words
field correctly:
public void readFile() throws FileNotFoundException {
words = new ArrayList<String>();
while (file.hasNext())
words.add(file.nextLine());
}
Try this. You don't need to use the getWord() method in the main. Also, create a constructor for the class:
public TextFile() {
}
Your openFile() method doesn't need to return string.
private void openFile() {
try {
file = new Scanner(new File("words.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Not Found");
} catch (Exception e) {
System.out.println("IOEXCEPTION");
}
}
Here is your readFile() method: 1)Read file 2)Split a line of words into each word and put it in array 3)Then, random word
public void readFile() throws FileNotFoundException {
// List<String> wordList = new ArrayList<String>();
while(file.hasNext()){
String line = file.nextLine(); //read file one line at a time
String[] parseWords = line.split(" "); //Parse what you read
int index = new Random().nextInt(parseWords.length);
String randW = parseWords[index];
System.out.println(randW);
}
}
In your main method:
TextFile file1 = new TextFile ();
file1.openFile();
file1.readFile();
file1.closeFile();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.