|
||||||
| Java Help Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||||
|
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();
}
}
__________________
|
|
|||||
|
Just a thought: you aren't making sure you have printed anything before calling fout.println();
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
|||
|
I can't see why there would be a blank line at the beginning of the file, but if you wish to use print instead of println then instead of the last 'fout.println()' call just add a '\n' to the end of the last word on each line. Code:
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.print('\n');
}
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Algorithms and Data Structures
Programming Language Popularity
Code Collaboration
Podnet IRC Network
AmpHosted
Goal #1: 1,000 Blogs
Goal #2: 1,000 Wiki Pages
Goal #3: 300,000 Posts
Goal #4: 20,000 Threads
Done: 30%, 23%, 55%, 75%