1.11M Members

User Input and File Reading

 
0
 

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());
  }
  }
}
 
0
 

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

 
0
 

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

 
0
 

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(:)

 
0
 

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

 
0
 
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?

 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 

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.

 
0
 

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

 
0
 

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

 
1
 

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

 
0
 

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)  
		 {
 
0
 

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.

 
0
 

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?

 
0
 

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.

 
0
 

im learning on the fly.

If you want to learn to write a java program you need to look at the tutorials to understand how to use classes and their methods. Look through these:
http://docs.oracle.com/javase/tutorial/java/TOC.html
http://docs.oracle.com/javase/tutorial/reallybigindex.html
http://docs.oracle.com/javase/tutorial/essential/TOC.html

 
0
 

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

 
0
 

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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: