Hi All

I have been stuck on this for a number of hours now, really cant workout how I should go about making this work, I am not looking for the correct code, More looking for what I should be looking up and what I have done wrong.

What the code should be doing is scanning a file, Looking at the first token of a line, and matching it to a what the user input, if the token matches, print the whole line and move onto the next line, and so on...

Here is the code I have mustered up so far

public class ExamTimes {
	
    public void printCourse(){
	
    	Scanner sc = new Scanner(System.in);
    	System.out.print("Enter the course code: ");
    	String userInput = sc.next();
    	
    	try {
    	    BufferedReader input = new BufferedReader(new FileReader("examdata.txt")); 
    	    String strData = (input.readLine());
    	   
    	    
    	    while ((strData = (userInput())) != null) {
    	    	StringTokenizer Tok = new StringTokenizer(strData);
        	    String compare = Tok.nextToken();
    	    	
    	    	if (compare.equals(userInput)){
    	        	
    	        	System.out.println(compare);
    	        	
    	        }	
    	    }
    	    
    	    input.close();
    	} catch (IOException e) {
    	}

And here is a sample of what examdata.txt looks like,

ACCY111	KKLT301	10	50	PEDERSEN	REID	
ACCY111	KKLT303	10	85	REID	SKINNER	
ACCY111	MCLT101	10	82	SKINNER	THAM	
ACCY111	MCLT102	10	39	THEVAKULASINGAM	VANN	
ACCY111	MCLT103	10	84	VEGAR	XU	
ACCY111	RHLT2	10	26	YANG	ZHU	
ANTH102	EALT006	22	72	ABRAHAM	GIANNOTTI	
ANTH102	EALT206	22	25	GIBSON	JENKINS	
ANTH102	KKLT301	22	50	JOBBINS	OAKLEY	
ANTH102	KKLT303	22	84	OATES	WOOTTON	
ARCH172	MCLT101	20	82	ANDERSON	LEE	
ARCH172	MCLT103	20	83	LI	ZHAO	
ARCH251	VS127	2	12	ABRAHAM	COCKROFT	
ARCH251	VS220	2	12	CRABBE	GILLER	
ARCH251	VS308	2	16	GLEN	KIMBER

So expanding on what I was trying to explain above, When the user types in "ACCY111" I would like it to print the following section of text

ACCY111	KKLT301	10	50	PEDERSEN	REID	
ACCY111	KKLT303	10	85	REID	SKINNER	
ACCY111	MCLT101	10	82	SKINNER	THAM	
ACCY111	MCLT102	10	39	THEVAKULASINGAM	VANN	
ACCY111	MCLT103	10	84	VEGAR	XU	
ACCY111	RHLT2	10	26	YANG	ZHU

Thoughts please?

Just use Scanner to read in the file.

Scanner input = new Scanner(System.in);
System.out.println("Course to match");
String course = input.next();
Scanner file = new Scanner(yourFilename);
while (file.hasNext()){
String courseID = file.next();
if ( // check if the course ID matches the course the user is looking for){
System.out.println( // print the rest of the current line; check out nextLine() method );
}
}

Hope that code snippet helps. I'm expecting that you will need to modify it to do anything substantial, but hopefully that will give you a start.

Thank you for your reply, Rather than going back to the more basic approach that you have suggested, I have managed to refined my current code and got it doing more along the lines of what I am after,

Currently though I am only getting it outputting the first token on the line of the data file, How can I go about increasing it from the first token until the end of each line?

Edit: Scrap that!

Got it working just as I want! Here it is below if you are at all interested,

public class ExamTimes {
	
    public void printCourse(){
	
    	Scanner sc = new Scanner(System.in);
    	System.out.print("Enter the course code: ");
    	String userInput = sc.next();
    	
    	try {
    	    BufferedReader brInput = new BufferedReader(new FileReader("examdata.txt")); 
    	    
    	   
    	    String curLine = brInput.readLine();
    	    while (curLine != null) 
    	    {    	    	
    	    	StringTokenizer Tok = new StringTokenizer(curLine);
        	String compare = Tok.nextToken();
        	    
    	    	if (userInput.equals(compare))
    	    	{
    	        	System.out.println(curLine);
    	        }	
    	    	
    	    	curLine = brInput.readLine();
    	    }
    	    
    	    brInput.close();    
    	    
    	} catch (IOException e) {
    	}

Instead of

StringTokenizer Tok = new StringTokenizer(curLine);
        	String compare = Tok.nextToken();
        	    
    	    	if (userInput.equals(compare))

why not just

if (curLine.startsWith(userInput))

or, if there can be whitespace at the front of the line, or the "first token" can be of varying lengths (i.e. the "userInput" might match two different tags due to additional characters) then

if (curLine.matches("^\\s*" + userInput + "\\s+.*$"))

(for performance on this though, you will probably want to do this String concatenation before the while loop and just use it in the while loop).

Instead of


why not just

if (curLine.startsWith(userInput))

or, if there can be whitespace at the front of the line, or the "first token" can be of varying lengths (i.e. the "userInput" might match two different tags due to additional characters) then

if (curLine.matches("^\\s*" + userInput + "\\s+.*$"))

(for performance on this though, you will probably want to do this String concatenation before the while loop and just use it in the while loop).

lol simply because we have not gone over startsWith in class, and from what I read on google, people seem to think its rather confusing

people seem to think its rather confusing

I have no idea why that would (or even might) be.

Thank you for your reply, Rather than going back to the more basic approach that you have suggested, I have managed to refined my current code and got it doing more along the lines of what I am after,

No problem. As a lot of the other posters here probably know, I'm pretty partial towards Scanner. Either way, both approaches are pretty simple, and I'm glad you got it to work.

:)

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.