CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    removing new line/line break

    How do I remove empty new lines or line breaks? For example:

    Code:
    System.out.println("Hello World!!!");
    
    System.out.println("Hello World!!!");
    
    System.out.println("Hello World!!!");
    
    System.out.println("Hello World!!!");
    
    System.out.println("Hello World!!!");
    and I want to turn it into like this:

    Code:
    System.out.println("Hello World!!!");
    System.out.println("Hello World!!!");
    System.out.println("Hello World!!!");
    System.out.println("Hello World!!!");
    System.out.println("Hello World!!!");
    As you can see, there is no longer an empty line in between two lines. Anyone knows how do I do that?

    Any help will be much appreciated. Thank you!

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    The two postings look like text. How where they entered?
    What program is reading them?
    If you edit the file in a hexEditor, you can remove the extra lineend chars by replacing 0xd0xa0xd0xa with 0xd0xa for Windows.
    Norm

  3. #3
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    They are actually Java source code.
    I am going to read a Java file in a Java program.
    And in that program I want to remove the empty lines.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    As you read the lines, test the line length and don't copy or save those that are not the length you want.
    Norm

  5. #5
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    hmmm... means I gotta use .length()?

    so if it's less than one char so string.length() < 1? something like that? haha...

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    Try it and see what happens
    Norm

  7. #7
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    I was unable to get it to work. I don't know how to solve this. Hmmm... Here are my codes:

    Code:
    try{
                    jTextArea1.setText("");
                    
                    File newFile = new File(filePath.get(jList1.getSelectedIndex()));
                    // Open the file that is the first 
                    // command line parameter
                    FileInputStream fstream = new FileInputStream(newFile);
                    // Get the object of DataInputStream
                    DataInputStream in = new DataInputStream(fstream);
                    BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    //Read File Line By Line
                    String strLine;
    
                    //while ((strLine = br.readLine()) != null)   {
                    while (true)   {
                    // Print the content on the console
                        if (! br.ready()) break;
                        
                        if(br.readLine().isEmpty() == false) {
                            strLine = br.readLine();
                            jTextArea1.append (strLine + newline);
                        }
                    }
                    //Close the input stream
                    in.close();
                    }catch (Exception e){//Catch exception if any
                        System.err.println("Error: " + e.getMessage());
                        e.printStackTrace();
                    }
    but as you can see in the codes, I did br.readLine() twice, so it ended up skipping some lines. How do I go around this? What can or should I do?

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    I was unable to get it to work
    Please explain. Could you show the input and the output and explain why the output is not what you want?

    Code:
    br.readLine().isEmpty()
    This reads a line but does not save it anywhere.
    Do the read in one step and the test in another step
    Norm

  9. #9
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    Okay. This is the file that I used as an [bold]input:[/bold]

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class p1 extends JFrame
    {
    	private int size=6;
    	private ImageIcon icon;
    	private JLabel lblImg;
    	private Timer timer;
    
    	public p1()
    	{
    		JPanel p1 = new JPanel();
    		lblImg = new JLabel();
    
    		icon = new ImageIcon(getClass().getResource("Sunset0.jpg"));
    		lblImg.setIcon(icon);
    
    		setLayout(new FlowLayout());
    		add(lblImg);
    	}
    
    	public static void main(String args[])
    	{
    		p1 frame = new p1();
    
    		frame.setSize(600,600);
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }
    [bold]and the output that I get was:[/bold]
    Code:
    import java.awt.*;
    
    {
    	private ImageIcon icon;
    	private Timer timer;
    	{
    		lblImg = new JLabel();
    		lblImg.setIcon(icon);
    		add(lblImg);
    
    	{
    
    		frame.setVisible(true);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    However, the output that I [bold]want[/bold] is:
    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class p1 extends JFrame
    {
    	private int size=6;
    	private ImageIcon icon;
    	private JLabel lblImg;
    	private Timer timer;
    	public p1()
    	{
    		JPanel p1 = new JPanel();
    		lblImg = new JLabel();
    		icon = new ImageIcon(getClass().getResource("Sunset0.jpg"));
    		lblImg.setIcon(icon);
    		setLayout(new FlowLayout());
    		add(lblImg);
    	}
    	public static void main(String args[])
    	{
    		p1 frame = new p1();
    		frame.setSize(600,600);
    		frame.setVisible(true);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    }

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    Your code must still be reading and not ouputting good lines as I suggested in post#8.
    The line of code I posted will read a line and not output it if the line is not empty.
    Norm

  11. #11
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    Hmmm... can you show me some examples or give me some samples or something? I still can't figure it out.

  12. #12
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    Some suggestions for testing:
    Create a simple file for copying that has numbers on each line to copy:
    1
    2

    3
    4


    5
    Here are 8 lines, 5 with data, 3 that are empty.

    Write a simple loop that reads the lines from the file one by one.
    Display each line after is read.
    Test if the line is empty and print a message that says the line is empty.

    If you can get this simple program to work, you will be able to fix the logic in your program.

    Go back and read the bottom half of my post#8.
    Norm

  13. #13
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    Oh yes! It's working great now. This is all I needed:

    Code:
    while ((strLine = br.readLine()) != null)   {
                        
                        if (strLine.trim().length() != 0){
                            strLine2 = strLine;
                            jTextArea1.append(strLine2 + newline);
                        }
                    }
    hahaha... just need another string and parse into it. Thanks for the guidance again!

  14. #14
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: removing new line/line break

    You don't need this statement:
    strLine2 = strLine;

    You can use strLine.
    Norm

  15. #15
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: removing new line/line break

    Cool. Yea, that works too. Hahah... I thought I would have to like parse it or something, I guess not. Thanks!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured