I'm working on Problem A on this page, and I have the program working correctly sort of. However, when I go to my output file, I see two blank lines one at the beginning of the file, and one at the end of the file.
Yet I have no idea, why these lines are appearing at the end of the file.
Basically you have to split a sentence into words, and check the length of each word. I've tried two methods of doing this, one with the split method of the String class, and one with the StringTokenizer class, and I'm still having this problem. Any ideas as to where the blank lines are coming from?
The code using the StringTokenizer:
Code:package contest_problems; import java.io.*; import java.util.*; /** * * @author James */ public class PA98 { public static void main(String[] args) throws IOException { Scanner fin = new Scanner(new FileReader("censor.in")); PrintWriter fout = new PrintWriter(new FileWriter("censor.out")); String sSentence = ""; StringTokenizer st; String sWord = ""; fin.nextInt(); while (fin.hasNextLine()) { sSentence = fin.nextLine(); st = new StringTokenizer(sSentence); while (st.hasMoreTokens()) { sWord = st.nextToken(); if (sWord.length() == 4) { fout.print("**** "); } else { fout.print(sWord + " "); } } fout.println(); } fout.close(); } }


LinkBack URL
About LinkBacks





Reply With Quote



Bookmarks
Algorithms and Data Structures
Java tutorials
Algorithms Forum