+ Reply to Thread
Results 1 to 5 of 5

Thread: Newlines?

  1. #1
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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();
            
        }
        
        
        
    }
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  2. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,662
    Blog Entries
    57

    Re: Newlines?

    Just a thought: you aren't making sure you have printed anything before calling fout.println();
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  3. #3
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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.
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

  4. #4
    Newbie LordIzuriel is an unknown quantity at this point
    Join Date
    Aug 2008
    Posts
    6

    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');
    }

  5. #5
    Code Slinger chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5 has a reputation beyond repute chili5's Avatar
    Join Date
    Mar 2008
    Posts
    7,023
    Blog Entries
    1

    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. :\
    "Whenever you remember, I'll be there/
    Remember how we reached that dream together" - Carrie Underwood

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts