Results 1 to 8 of 8
  1. #1
    fpena06 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Skipe line if text in array?

    Hi everyone,

    I'm new to java and the forum. I have the following code but can't seem to figure out, how to have the line skipped if the text is in the array. i.e skip line if = "Report"

    Thank you.

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*; 
    
    public class ReadTextFileExample {
        public static void main(String[] args) {
            File file = new File("/titan.txt/");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;
            ArrayList al = new ArrayList(); 
            al.add("Report");
            al.add("Page");
            al.add("Earliest");
            al.add("Planned");
    
            try {
            	
                reader = new BufferedReader(new FileReader(file));
                String text = null;
               
                // repeat until all lines is read
                while ((text = reader.readLine()) != null) {
                    contents.append(text)
                        .append(System.getProperty(
                            "line.separator"));
                
                
                
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            // show file contents here
            System.out.println(contents.toString());
    
        }
    }
    Last edited by fpena06; 04-26-2012 at 04:26 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Skipe line if text in array?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Skipe line if text in array?

    That's Darryl's shorthand for: "Put your java code in the proper tags or we ain't gonna help. "

  4. #4
    fpena06 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Skipe line if text in array?

    Sorry about that.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Skipe line if text in array?

    So you actually want to check if the String is in the ArrayList, not an array, and that's a big difference. You're in luck since ArrayList has a method that will help you here. I suggest that you go through the ArrayList API to find this method, and give a try solving your problem using it. If you're still stuck, then come on back with your new attempt code.

  6. #6
    fpena06 is offline Member
    Join Date
    Apr 2012
    Posts
    3
    Rep Power
    0

    Default Re: Skipe line if text in array?

    Error line 30.

    Java Code:
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.*; 
    
    public class ReadTextFileExample {
        public static void main(String[] args) {
            File file = new File("/titan.txt/");
            StringBuffer contents = new StringBuffer();
            BufferedReader reader = null;
            ArrayList al = new ArrayList(); 
            al.add("Report");
            al.add("Page");
            al.add("Earliest");
            al.add("Planned");
    
    
            try {
            	
                reader = new BufferedReader(new FileReader(file));
                String text = null;
               
    
                
                // repeat until all lines is read
                while ((text = reader.readLine()) != null) {
                	
                	if(al.contains(text)) = "true"{ 
                		
                		text = reader.readLine();
                	}
                	
                	else{
                		
                    contents.append(text)
                        .append(System.getProperty(
                            "line.separator"));
                	}
                }
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            
            // show file contents here
            System.out.println(contents.toString());
    
        }
    }

  7. #7
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Skipe line if text in array?

    Line 30 contains the following code:

    Java Code:
    ...
    if(al.contains(text)) = "true"{
    ...
    A couple of errors there:

    • You can see that the parenthesis in your if statement is not correct.
    • Also the ArrayList.contains() method returning a boolean value and not a string.
    • When comparing boolean value use the == operator. The = operator is assignment operator.
    Last edited by wsaryada; 04-27-2012 at 07:16 AM.

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Skipe line if text in array?

    Additionally, since String.contains() returns a boolean, you do not need to compare it to anything. The if tests for true or false, so this is cleaner:

    Java Code:
    ...
    if (al.contains(text))  {
    ...

Similar Threads

  1. How to read a 2d array file line by line
    By jsoeung in forum New To Java
    Replies: 35
    Last Post: 04-18-2012, 03:23 PM
  2. Replies: 7
    Last Post: 03-05-2011, 09:25 AM
  3. Replies: 1
    Last Post: 02-28-2011, 05:30 PM
  4. Stepping through a text file line by line
    By evanlivingston in forum New To Java
    Replies: 10
    Last Post: 01-29-2011, 04:30 AM
  5. Replies: 8
    Last Post: 12-28-2010, 02:24 PM

Posting Permissions

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