java - Vocab learning program not working properly -
so code, , it's meant give random word list, have user input meaning that's in external text file, , if it's correct, removes word list otherwise keeps word , adds missedword list. problem words aren't getting removed list, , can't seem print arraylist. can me problem? also, there improvements can make code? thank of help.
import java.io.*; import java.util.*; public class wordlist1 { public static void main(string[] args) throws ioexception{ boolean fart = true; filereader fr = new filereader("wl1.txt"); bufferedreader br = new bufferedreader(fr); scanner console = new scanner(system.in); arraylist wordarray = new arraylist(); arraylist missedwords = new arraylist(); string input, stringarray[] = new string[2], answer; while((input=br.readline())!=null){ stringarray = input.split(" "); wordarray.add(stringarray); } while(fart){ stringarray = (string[]) wordarray.get((int)(math.random()*(wordarray.size()))); system.out.println(stringarray[0]); answer = console.nextline(); if(answer.equalsignorecase(stringarray[1])){ system.out.println("correct"); wordarray.remove(stringarray[1]); } if(!answer.equalsignorecase(stringarray[1])){ system.out.println("incorrect, " + stringarray[1]); missedwords.add(stringarray[0]); } if(answer.equalsignorecase("escape")){ fart = false; system.out.println(missedwords); } if(answer.equalsignorecase("print")) system.out.println(wordarray); } }
}
system.out.println(wordarray)
going call .tostring()
on arraylist
, gives information reference, not data inside list. need write out each element in list rather list object itself:
system.out.println("words in array:"); (string eachword : wordarray) { system.out.println(eachword); }
now, works if you've defined list list of strings. definition creates list of java object:
arraylist wordarray = new arraylist();
it's better practice specify type of elements go in list. done using generics:
arraylist<string> wordarray = new arraylist<>();
that assumes wordarray
list of words, code adds entire array wordarray
list. give you? think want this:
while((input=br.readline())!=null){ stringarray = input.split(" "); // add each word, not array wordarray.addall(arrays.aslist(stringarray)); }
your problem words not getting removed similar problem. arraylist holds array of strings, you're trying remove single string. call .remove()
not find string in arraylist.
Comments
Post a Comment