tags:

views:

94

answers:

4

Hi, at the moment I have this:

import java.util.*;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class StudentID {

  public static void main(String[] args)throws Exception{

   System.out.println ("Please enter StudentID: ");
   BufferedReader reader = new BufferedReader (new FileReader("CW2_data.csv"));
      File file = new File("CW2_data.csv");
 try {
    BufferedReader r = new BufferedReader(
                        new InputStreamReader(
                        new FileInputStream("CW2_data.csv")));
    String line;
    while((line = r.readLine()) != null)
        System.out.println(line);
    r.close();
} catch(IOException e) {
    System.out.println(" There was an Error Reading the file. " + e.getMessage());
}


  }
}

I am trying to get the program to prompt the user for its StudentID, then search through the data file for that StudentId and return their marks for all the modules. Unfortunatly all my codes does is list the whole file back to me.

I tried to add StudetnID=Userinput.readstring; right after so that user was given an opertunity to type in the username but then I realise that wouldn't work unless I tell it scan the whole document for it. I was reading up on maps (as recomendeded by user on here) but I still haven't got to grips with it and I don't know if it would it even work here. The data file is in form of 3 columns and about 282 rows of data, ie

UserID: Module: Mark

ab006, GYH095, 56

Any help would appreciated.

+1  A: 

As that this appears to be homework (and how it relates to your previous question) I will not give a full solution.

The buffered reader reads an entire line of text. So you'll get strings similar to this: "ab006, GYH095, 56"

With that string you can use something like StringTokenizer or String.split to seperate the individual elements, which now become "ab06", "GYH095", "56" ...

The reason this program just writes the entire file back to you is that the program just scans through the file, without doing anything to what it reads.

monksy
I see so after I have buffered reader the entrie line of text, String.split would sperate the elements, then I could maniuplate them afterwards. are their any examples/reading I could look at that have both of them working together?
Izzy
When you use BufferedReader::readLine, it will give you the current line in the form of a string. You can choose to work with the string after it is read and move on or you could choose to store all of it and then work with the entire contents later. I would highly suggest working with it after it is read.
monksy
A: 

Well, it is good progress that you are getting the file to be read properly. What you first want to do is get the user's input before reading the file. This can be done with the Java Scanner class. Take a look there for examples on how to use it (you will want to read a String from the user. Hint: the nextLine() method). The rest can be done via stevens explanation.

Roman Stolper
Thanks, that has helped, it so obvious that I should of asked the user for the input so I could proceeed, I will hopefully post up what I have done soon.
Izzy
im little a bit stuck on the searching of the file for the studentID, I posted up what did, not including the buffereread thing since all I manage to do is return the list
Izzy
Not quite sure what you're asking.
Roman Stolper
A: 

To load the data, how about you make a simple class that represents a line from your datafile, and then store it in a List. Yes, you'll have to scan through the entire list each time but it doesn't seem that large anyway (if there is a natural key you can use, which appears to be the case, you could indeed use a Map). Check out the java tutorial on collections.

wds
A: 
import java.util.*;

import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.util.Scanner;

public class StudentID {

    public static void main(String[] args)throws Exception{
    String studentid;               // Declare a variable to hold the name.
    Scanner in = new Scanner(System.in);
    //... Prompt and read input.
    System.out.print("Please enter studentId please: ");
    studentid = in.nextLine();      // Read one line from the console.
    in.close();


    }

}

I have got it to read input from the user, that was simple enough but I cant get my head around using string split() to search for the ID. I understand it splits the file into readable chuncks but how do you get it to search for that specific ID in the txt and return the results for their mark?

Izzy