Connect with Facebook Lost Password?


Go Back   CodeCall Programming Forum > Software Development > Java Help

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.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-07-2008, 08:16 AM
chili5's Avatar   
Code Warrior
 
Join Date: Mar 2008
Posts: 5,462
Blog Entries: 3
Rep Power: 49
chili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to behold
Default Newlines?

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();
        
    }
    
    
    
}
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 11-07-2008, 10:06 AM
WingedPanther's Avatar   
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 8,079
Blog Entries: 48
Rep Power: 20
WingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to beholdWingedPanther is a splendid one to behold
Default Re: Newlines?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-07-2008, 10:14 AM
chili5's Avatar   
Code Warrior
 
Join Date: Mar 2008
Posts: 5,462
Blog Entries: 3
Rep Power: 49
chili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to behold
Default Re: Newlines?

Yeah I can tell that the Strings are being printed to the properly just the output file looks weird.

My output file is attached. I had to rename it to censor.txt to get it to upload though. Any chance that the StringTokenizer adds a new line that I don't know about?

Edit: If I change the println() where I'm printing the sentences to print() it prints it out on one line.

As far, as I can tell, those two new lines shouldn't be there.
__________________

Last edited by chili5; 05-09-2009 at 06:33 AM..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 11-08-2008, 10:06 PM
Newbie
 
Join Date: Aug 2008
Posts: 6
Rep Power: 0
LordIzuriel is an unknown quantity at this point
Default Re: Newlines?

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');
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 11-09-2008, 06:01 AM
chili5's Avatar   
Code Warrior
 
Join Date: Mar 2008
Posts: 5,462
Blog Entries: 3
Rep Power: 49
chili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to beholdchili5 is a splendid one to behold
Default Re: Newlines?

Well this is just weird, that didn't work. Any honestly I don't think there should even be a new line at all. :\
__________________
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 08:23 PM.

Freelance Jobs

XML/XSL: Need code for Book with Chapers using XML
Create an XML file for a book of your creation, and a basic CSS file that will format it to display ...
Earn: $40.00


C++/C: Simple firework cue sequencer
What I require is a rework of a simple cue sequencer. I have a piece of hardware (an Arduino boar...
Earn: $50.00


HTML/XHTML: Menu Rework - ASCIIBin
I'm placing this in the HTML/XHTML section of the Freelance site but you are not limited to HTML. Wh...
Earn: $20.00



CodeCall Goal

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%

Ads