I need my program to take user input look throught the text file that is being read and find the the text and print back the corresponding text. In the text file I have a list of java compile time errors and their sample code separated by a colon.

For example: ; expected:new String[] { "x", "y" }

I need the user to input "; expected" and then have my program look through the text file find "; expected" and then print back the sample code "new String[] { "x", "y" }".
Right now I have the program just reading the text file and printing it out. Can anyone help me?

import java.io.*;

class FileRead 
{
 public static void main(String args[])
  {
  try{
	  
  // Open the file that is the first 
  // command line parameter
  FileInputStream fstream = new FileInputStream("javaerrors.txt");
  
  // Get the object of DataInputStream  
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  String strLine;
  
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
	  
  // Print the content on the console
  System.out.println (strLine);
  
  }
  
  //Close the input stream
  in.close();
  
    }
  
  catch (Exception e){//Catch exception if any
  System.err.println("Error: " + e.getMessage());
  }
  }
}

Recommended Answers

All 19 Replies

You can use regex to find your matching text and return match results.

how would I set up the text file to read the correct error and send back the sample code for the error?

Another way to find your desired lines in the file is to use a String method to see if the line has the String in it you are looking for.
The String class also has methods for getting at the substrings that are delimited by your marker character(:)

ok can I see some example code of using this String method cause I am totally blanking and it would be greatly appreciated

example code of using this String method

What method(s) of the String class have you looked at that would allow you to determine if a String that was read from the file contains the String you are looking for?

compareTo, compareToIgnoreCase, equalsIgnoreCase

im not sure which one will be best for what I want, but my question is how will the program know how to compare the users input to the corresponding words in the text file

Have a look at the method that comes after concat and before contentEquals in the API doc for String.

You only have to do two things:
1) Determine if the line read contains the string the user input.
2) If it does, return the portion of that string after the ":"

As Norm already said, you just need to read over the methods in the String class that do those things. The comparison methods you listed above compare an entire string. That is not what you are wanting to do.

how will the program know how to compare the users input to the corresponding words in the text file

Normally the programmer puts instructions into a program that controls what the program does.

ok yea that on will work, is there anyway you can show me how to implement this in my code above it would be much appreciated

What part are you having problems with?
Post your code and ask your questions.

I'm here to help you learn Java, not do your homework for you. It's not hard, just give it a try.

would I use the contains method inside the while loop or after the input stream is closed?

try
	 {	  
		 // Open the file that is the first 
		 // command line parameter
		 FileInputStream fstream = new FileInputStream("file here");
  
		 // Get the object of DataInputStream  
		 DataInputStream in = new DataInputStream(fstream);
		 BufferedReader br = new BufferedReader(new InputStreamReader(in));
		 String strLine;
  
		 //Read File Line By Line
		 while ((strLine = br.readLine()) != null)   
		 {
			 System.out.println (strLine);
		 }
  
		 //Close the input stream
		 in.close();
		 
		 public boolean Contains (CharSequence s)  
		 {

Why have you copied in a partial method declaration into your code block?

As far as where to check the string, you need to check each line, right? That should suggest where to put the code.

Where is the text that was read from the file? Is it in the String: strLine?
You need to look at what is in strLine to see if it has the contents that you want to process.

Why are you writing the method: Contains?

i need to use contains to compare the user input to a line in the file because my file is set up as so:

; expected: int x=3 (sample code that would throw error)

so when a user enters "; expected" I need the users input to be compared to the file as it is being read and when the input is deteced print the sample code after the ":"


i understand that you guys won't do it for me I just don't know much about java so any help is appreciated im learning on the fly.

You don't need to write contains, that method already exists in the String class

Try writing a very short simple program to experiment with the different String class methods. Something like this:

String aStr = "This is a String to experiment with";
  System.out.println(aStr.<PUT METHOD TO TEST HERE>(<ARGS>)); // print out the results

Change the method used and the args used to see what each method does.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.