Java using the replaceAll method -
i have code , reads text file array list , prints out array list. how can have read file , store array list , create uppercase lowercase letters , remove punctuations? have been looking on net couldn't find solutions.
for illustration text file can be:
it's sunny day today!
and result should be:
its sunny day today
.
import java.io.*; import java.util.*; public class junk { private static arraylist<string> list = new arraylist<string>(); public static void main (string[] args) { seek { scanner s = new scanner(new file("test.txt")); while (s.hasnext()) { list.add(s.next()); } } grab (exception e) { e.printstacktrace(); } int n = list.size(); for(int = 0; < n ; i++) { system.out.println(list.get(i)); } } }
this line of code here works , remove punctuation , makes letters lowercase works if string s = "blah blah blah;;;"
string s1 = s.replaceall("\\p{punct}|\\d","").tolowercase();
how can create work array list?
sorry, looks question updated during writing illustration code. rephrased question, guess means replaceall take effect not set arraylist. repalceall generate new string object , not impact 1 in array. needs repaceall after read file , before "add" arraylist object.
========================
try code snippet, may needs tuning justice of punctuation.
public void convert(string filename) throws filenotfoundexception, ioexception { list<string> list = new arraylist<string>(); bufferedreader reader = null; seek { reader = new bufferedreader(new filereader(filename)); string s = reader.readline(); while (s != null) { // should work in loop string s1 = s.replaceall("\\p{punct}|\\d","").tolowercase(); list.add(s1.tostring()); } } { if (reader != null) reader.close(); } (string s : list) { // not work in loop //string s1 = s.replaceall("\\p{punct}|\\d","").tolowercase(); // list.add(s1.tostring()); system.out.println(s); } }
java
No comments:
Post a Comment