java.util.scanner - Java: Scanner stopping at new line -
i trying scan through text files , add them map, map , working. however, scanner seems stopping when comes 'enter' in text file, or blank line. problem
here block of code scanner/mapper
class onebuttonlistener implements actionlistener { @override public void actionperformed(actionevent evt) { final jfilechooser onefc = new jfilechooser(); onefc.showopendialog(analysisframe.this); string newline = null; onefc.getname(null); int returnval = 0; file fileone = onefc.getselectedfile(); scanner input = null; try { input = new scanner(fileone); } catch (filenotfoundexception ex) { logger.getlogger(analysisframe.class.getname()).log(level.severe, null, ex); } inputtext = input.nextline(); string[] words = inputtext.split("[ \n\t\r,.;:!?(){}]"); for(int = 0; < words.length; i++){ key = words[i].tolowercase(); if (words[i].length() > 1){ if (mapone.get(key) == null){ mapone.put(key, 1); } else { value1 = mapone.get(key).intvalue(); value1++; apone.put(key, value1); } } } } } thanks help!
you should scan inside loop until reaches end of file, example:
stringbuilder builder = new stringbuilder(); while(input.hasnextline()){ builder.append(input.nextline()); builder.append(" "); // might not necessary } string inputtext = builder.tostring(); an alternative using split use delimiter scanner , use hasnext() , next() instead of hasnextline() , nextline(). try out, see if works.
for example:
scanner.usedelimiter("[ \n\t\r,.;:!?(){}]"); arraylist<string> tokens = new arraylist<string>(); while(scanner.hasnext()){ tokens.add(scanner.next()); } string[] words = tokens.toarray(new string[0]); // optional also on side note, it's not necessary create jfilechooser everytime:
class onebuttonlistener implements actionlistener { private final jfilechooser onefc = new jfilechooser(); @override public void actionperformed(actionevent evt) {
Comments
Post a Comment