1,105,232 Community Members

User Input and File Reading

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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());
  }
  }
}
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

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

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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(:)

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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?

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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.

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
0
 

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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
1
 

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

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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)  
		 {
Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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?

Member Avatar
mrjeck2008
Newbie Poster
7 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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

Member Avatar
JamesCherrill
... trying to help
10,362 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,749 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

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

Member Avatar
NormR1
Posting Sage
7,723 posts since Jun 2010
Reputation Points: 563 [?]
Q&As Helped to Solve: 793 [?]
Skill Endorsements: 16 [?]
Team Colleague
 
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 three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: