I have a text file and i want to read each word into an ArrayList<Dictionary>
but I have to leave commas, dashes, dots etc. This is the code so far for this purpose:
Scanner sc2 = null;
while (sc2.hasNextLine()) {
Scanner s2 = new Scanner(sc2.nextLine());
while (s2.hasNext()) {
String s = s2.next();
String[] tokens = s.split("\\W+");
s = tokens.toString();
Dictionary.add(s);
}
}
The problem is that when I execute the printing code :
for (int i = 0; i < Dictionary.size();i++) {
System.out.println(Dictionary.get(i));
}
I get the following:
[Ljava.lang.String;@ea2f77
[Ljava.lang.String;@ea6137
[Ljava.lang.String;@ea639
Etc. for every word. I belive that the problem is s = tokens.toString();
but i do not know how to fix it. Thank you!
tokens
is an array of Strings
, and as such its toString()
method returns what you see in your output. You need to iterate over each String
in tokens
and add them to the Dictionary
individually, eg
for (int i = 0; i < tokens.length; i++) {
Dictionary.add(tokens[i]);
}
if you wanted to avoid for loops; then just use java.util.Arrays
--
s = Arrays.toString(s.split("\\W+"));
This is a question that has already been asked, but here's the answer:
String punctutations = ".,:;";//add all the ones you want.
if(punctutations.contains(letter[a])) //If the character at letter[a] contains a punctuation mark
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.