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!
Code Java:
//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() );
}
}
}
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?
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.
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!
Re: Reading a particular string and printing the line that contains the string from a text file
Quote:
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.
Quote:
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).
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.
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.
Re: Reading a particular string and printing the line that contains the string from a text file
Quote:
Originally Posted by
jps
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 )
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!
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.
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.
Code Java:
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() );
}
}
}