Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Reading a particular string and printing the line that contains the string from a text file

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Reading a particular string and printing the line that contains the string from a text file

    Hello,
    Please point me in the right correction. I am trying to read the test.txt file and print all the lines that contains the string "Yellow".

    Thanks!



    //package textfiles;
    import java.io.*;
     
     
     class ReadFromAnsiFile
     {
     	public static void main ( String[] args) 
     	{
     		String fileName = "test.txt"; //string variable to hold the name of a text file:
    	 	String line;
     
     
    	try	
     	{
    		 BufferedReader in = new BufferedReader( new FileReader( fileName  ) );
    		 line = in.readLine();  // Reads a line of text, returns a null if nothing is there.
        while (line != null )
        {
    		if (line.nextLine().equals("Yellow"))
    		{  
    	   System.out.println (line); 
     
    	   line = in.readLine();
       		}	    
        }
         in.close();  
        }
     
     
        catch ( IOException e )
        {
    	    System.out.println(e.getMessage() ); 
    	}		
     
     	}
     }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    What does this code do? How does it differ from what you expect?

    Hint: You're using the equals() method. What do you expect that to do?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So the only value in the line is "Yellow"? Why wouldn't you use ---------------------------------------------

    MES Enterprises, LLC
    Last edited by jps; November 5th, 2012 at 04:30 PM. Reason: spoonfeeding

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    @MES Enterprises LLC please read The problem with spoonfeeding.

  5. #5
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    I made the following change :
    if (line.equals("Yellow"))
    and it gave me the line that only starts with "Yellow" as the string. I believe that a start, but I want to print all the lines that start with Yellow. For example I want this line to print as well: "Yellow - and Blue makes Green". But I am having a problem coming up with a way to search for the string "Yellow" first and then print any line that contains it. Thanks!

  6. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    I made the following change :
    Any time you make changes you should post the new version of the code so everyone is looking at the same code for the discussion.
    if (line.equals("Yellow"))
    and it gave me the line that only starts with "Yellow" as the string. I believe that a start,
    That checks to see if the entire contents of the line are exactly "Yellow", nothing more and nothing less. What you really want to know is if "Yellow" is a substring of the line.
    I would think finding a substring in a string, if it exists, would be a fairly common thing to do with a string. Maybe something already exists to do something like this.
    Substring being the keyword here (and for a search engine).

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    [nitpick]
    I think substring is a little misleading here. I can think of 2 other methods that would be more appropriate.
    Improving the world one idiot at a time!

  8. The Following User Says Thank You to Junky For This Useful Post:

    matt0605 (November 12th, 2012)

  9. #8
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    You should list them. I try to give a nudge in the right direction, but if there is a better (or just different) way feel free to mention it.

  10. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    Quote Originally Posted by jps View Post
    You should list them. I try to give a nudge in the right direction, but if there is a better (or just different) way feel free to mention it.
    The String API is everyone's best friend here: String (Java Platform SE 7 )
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. The Following User Says Thank You to KevinWorkman For This Useful Post:

    matt0605 (November 12th, 2012)

  12. #10
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    Hey Everyone, thanks for the your input and I appreciate the spoonfeeding info. As I mention I am new to Java and just wanted to get pointed in the right direction. It seems there are different ways to get the result I want. While searching the Java Oracle tutorials website I came across Regular Expression tutorial (regex) and it seems to be what I am looking for. I will post my updated code once I am done. Thanks!

  13. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    There are in fact many ways to do it.

    I apologize for the misleading comment on Substring. What I meant by substring is that, you were checking if the entire string .equals "Yellow", when really you need to check if "Yellow" is a substring of line.nextLine()
    Meaning that you have to check more than the case where the entire string .equals "Yellow"

    regex is another way to do it. Good luck with getting that code working.

    Still I think it should be said that KevinWorkman is right in pointing at the String class, and there are two methods in the string class (probably the two mentioned by Junky), that are suited for this assignment. Read over the methods of the class. If you actually read them all you will see many other wonderful methods. Read them all at least once, then read them again. I'm still not going to list them because I believe reading all of the methods of the class (especially this class) is so useful.

  14. The Following User Says Thank You to jps For This Useful Post:

    matt0605 (November 12th, 2012)

  15. #12
    Junior Member
    Join Date
    Sep 2012
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Reading a particular string and printing the line that contains the string from a text file

    Thanks for your assistance. I did a little reading on the String Class and chose the startsWith(). It did what I wanted it to do.

    import java.io.*;
    import java.nio.*;
    import java.nio.channels.*;
    import java.util.*;
     
    class ReadFromAnsiFile
    {
      public static void main ( String[] args) 
      {
        String fileName = "test.txt"; //string variable to hold the name of a text file.
    	String line;
    	String stringToSearch = "Yellow";  // String to search for.
    	int numberOfStrings = 0; // number of found strings in the text file. 
     
    	try	
     	{
    	  BufferedReader in = new BufferedReader( new FileReader( fileName  ) );
    	  line = in.readLine();
     
    		while (line !=  null)
        	{
    	   	  if (line.startsWith(stringToSearch))
    	      {
    	   		System.out.println (line); 
    	   		numberOfStrings++;	   		  		
       		  }	 	  
              line = in.readLine();
            }
           System.out.println ("The total number of lines containing the string is: " + numberOfStrings);  
           in.close();  
        }
     
     	 catch ( IOException e )
         {
    	   System.out.println(e.getMessage() ); 
    	 }		
      }
     
    }

Similar Threads

  1. How to plot line graph using jfreechart reading from text file
    By priti in forum Java Theory & Questions
    Replies: 9
    Last Post: March 31st, 2012, 01:39 PM
  2. how to plot the line graph using jfreechart reading from text file
    By priti in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2012, 06:53 AM
  3. Reading in entire contents of text file to one string
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 9
    Last Post: December 12th, 2010, 07:03 PM
  4. reading string in from text file
    By basketball8533 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: December 3rd, 2010, 05:31 PM
  5. Reading lines of a text file into a string array
    By fortune2k in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 11th, 2010, 11:56 AM