java - Reading words and numbers out of a text file -
java - Reading words and numbers out of a text file -
i writing programme reads text file , adds unique words , numbers arraylist. used delimiter nosuchelementexception when run program. delimiter wrong or did create mistake?
here program:
import java.util.*; import java.io.*; public class indexer { public static void main(string[] args) throws filenotfoundexception { scanner filescanner = new scanner(new file("file.txt")).usedelimiter("[.,:;()?!\" \t]+~\\s"); int totalwordcount = 0; arraylist<string> words = new arraylist<string>(); while ((filescanner.hasnext()) && (!words.contains(filescanner.next()))) { words.add(filescanner.next()); totalwordcount++; } system.out.println("there " + totalwordcount + " unique word(s)"); system.out.println("these words are:"); system.out.println(words.tostring()); filescanner.close(); } }
this should work, can utilize tostring or iterator show words:
set<string> words = new hashset<string>(); while ((filescanner.hasnext())) { words.add(filescanner.next()); } system.out.println("there " + words.size() + " unique word(s)"); system.out.println("these words are:"); //system.out.println(words.tostring()); (iterator<string> = words.iterator(); it.hasnext(); ) { string f = it.next(); system.out.println(f); } filescanner.close();
java java.util.scanner java-io
Comments
Post a Comment