954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

User Input and File Reading

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());
  }
  }
}
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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(:)

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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)  
		 {
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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.

mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 10
Solved Threads: 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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

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

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: