string - write a program that reads a sentance and write it on a file separating each word on a different line of the file (JAVA) -
i need exercise java , don't know begin, told me should utilize stringtokenizer , have no thought how utilize or else on exercise. can of help me? i'm watching tutorials on java don't nil ...
the first step take string , alter list of words. can accomplished next code:
string initialstring = "..."; //whatever string string[] words = initialstring.split(" "); //split spaces word list next, have open file can write words to. create instance of printwriter, constructor 2 parameters (the file name , encoding, utf-8). create printwriter next code:
printwriter write = new printwriter("output.txt", "utf-8"); just remember whatever file name specify printwriter overwritten!
finally, loop through list of words , write each 1 file modified for-loop:
for (string word : words) write.writeln(word); and close writer:
write.close(); thus, final code looks this:
import java.io.printwriter; public class writewords { public static void main(string[] args) { //you can alter :) string sentence = "i using java text processing instead of perl."; string[] words = sentence.split(" "); //change "output.txt" whatever file printwriter write = new printwriter("output.txt", "utf-8"); (string word : words) write.writeln(word); write.close(); } } java string file stringtokenizer
No comments:
Post a Comment