-2

I'm currently taking my first java class and have become completely stuck on an exercise. I'm supposed to read data from a text file containing student IDs and their corresponding test scores, have the program grade them then print the results.

I kind of understand the problem, but the book we're working from is kinda hard to read. It all blurs together and I feel like they want me to read two separate things and make a logical leap on how to put them together and I just don't get it.

TTFTFTTTFTFTFFTTFTTF ABC54102 T FTFTFTTTFTTFTTF TF DEF56278 TTFTFTTTFTFTFFTTFTTF ABC42366 TTFTFTTTFTFTFFTTF ABC42586 TTTTFTTT TFTFFFTF

My main issue is that I don't see how I tie the array to the data I have.

  • 1
    the text file must have some sort of separator, like it may be a csv file or something. can't tell without you posting. but read the file in, and for each line, split on whatever is the delimiter, get the score, the id, etc by its column index. – mistahenry Nov 24 '14 at 19:25
  • 1
    The beginning of learning how to programming is knowing the correct thing to google. Clearly you do not so I would recommend that you make the following searches: "how to read a file line by line java", "how to split a string on commas java"(or however things are separated in the file you are given). You should probably make object representations of what you are reading from the file, and store them in array lists. honestly edit your post to show what the file looks like I can help you further – mistahenry Nov 24 '14 at 19:57

2 Answers2

3

I am not gonna post the whole solution but give some steps to start.

Follow this example

BufferedReader reader = new BufferedReader(new FileReader("/path/to/file.txt"));
String line = null;
ArrayList<String> array = new ArrayList<>();
while ((line = reader.readLine()) != null) {
    array.add(line);
}

and to split the string like this

str.split(" "); // considering that ids and name are separated by spaces
Junaid
  • 2,572
  • 6
  • 41
  • 77
  • See, but that's what I'm confused on. Everything I look up on the subject starts throwing around stuff about splitting the string or doing x or y and the book doesn't mention any of it. – Action Frank Nov 24 '14 at 19:31
  • Find my answer helpful? If yes then mark it correct. Thanks – Junaid Nov 24 '14 at 19:36
  • What exactly is Buffered Reader? Can I have some comments detailing what each step does exactly? – Action Frank Nov 24 '14 at 19:39
  • In simple words BufferedReader is for I/O i.e. reading and writing to file. Details are here http://stackoverflow.com/questions/15984789/what-exactly-does-stream-and-buffer-mean-in-java-i-o – Junaid Nov 24 '14 at 19:40
  • Again, I don't have any idea where to start. It's a god damn rabbit hole. I just end up having more questions than I started with and the book doesn't help when it skims over four or five definitions for various terms then uses them like I have any idea what they mean. – Action Frank Nov 24 '14 at 19:43
  • Better go read good book. Get some introduction to JAVA. These things will be simpler for you. – Junaid Nov 24 '14 at 19:46
0

So, since blanks are allowed in your list of T's and F's, which I assume means the student left the answer to the question blank, you do not have the luxury of using a convenience method like split to easily separate the answers. Instead we use our knowledge that the number of questions must be the same, and id's must have a common length. You can use the substring method to parse out the what you need.

Here's some pseudocode:

final int NUM_QUESTIONS = 25; //I didn't actually count, that's your job
final int ID_LENGTH = 8;
int currentIndex = 0;

//assuming you can fit the whole string in memory, which you should in an intro java class
//do the operations that googling "read a file into a string java" tells you to do in readFileToString
String fileContents = readFileToString("saidFile.txt");


while(fileContents.charAt(currentIndex) != fileContents.length()){
    String userAnswers = fileContents.substring(currentIndex, currentIndex+NUM_QUESTIONS);

    //move index past userAnswers and the space that separates the answers and the id
    currentIndex = currentIndex + NUM_QUESTIONS + 1;

    String userId = fileContents.substring(currentIndex, currentIndex+ID_LENGTH)

    //move currentIndex past userId and the space that separates the userId from the next set of answers
    currentIndex = currentIndex + ID_LENGTH + 1;

    //either create an object to store the score with the userId, or print it right away
    int score = gradeAnswers(userAnswers)
    System.out.println(userId + " scored " + score);
}
mistahenry
  • 8,554
  • 3
  • 27
  • 38