-3

I'm currently stuck on a programming assignment which requires me to read in data from a text file then process it. The file looks like this:

CS1 2012 Group 1
8
5,5,5,6,5,8,9,5,6,8, good, very good, excellent, good
7,7,8,7,6,7,8,8,9,7,very good, Good, excellent, very good
8,7,6,7,8,7,5,6,8,7 ,GOOD, VERY GOOD, GOOD, AVERAGE
9,9,9,8,9,7,9,8,9,9 ,Excellent, very good, very good, excellent
7,8,8,7,8,7,8,9,6,8 ,very good, good, excellent, excellent
6,5,6,4,5,6,5,6,6,6 ,good, average, good, good
7,8,7,7,6,8,7,8,6,6 ,good, very good, good,  very good
5,7,6,7,6,7,6,7,7,7  ,excellent, very good, very good, very good

The first 2 lines are read in then assigned to fields, the remaining lines are the ones I have to process. I've been told to use .useDelimiter("[ ]*(,)[ ]*") but so far haven't really been able to put it to good use.

Once the data has been read in I have to convert the Strings into integers using a switch statement and work out an average feedback score e.g:
excellent = 5
very good = 4
good = 3

SCREENSHOT :

enter image description here

nIcE cOw
  • 24,468
  • 7
  • 50
  • 143
andy94
  • 13
  • 1
  • 4
  • 2
    [What have you tried?](http://whathaveyoutried.com) Show us some code. –  Feb 26 '13 at 17:08
  • 1
    Oh, and stop screwing up the formatting, please. –  Feb 26 '13 at 17:09
  • 1
    They asked you to convert the strings to integers with a switch statement? Wow. – crush Feb 26 '13 at 17:10
  • You can use multiple delimiters using [String.split()](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String)), as discussed [here](http://stackoverflow.com/q/5993779/1057230) – nIcE cOw Feb 26 '13 at 17:12
  • Do you have a specific question? [How to use Scanner with a delimiter](http://stackoverflow.com/a/14243167/1711796). – Bernhard Barker Feb 26 '13 at 17:21
  • Trying to edit the post to add my code but getting formatting errors, bear with me – andy94 Feb 26 '13 at 17:26
  • Just had a look at your link Dukeling, good stuff. Thanks a lot! – andy94 Feb 26 '13 at 17:31
  • Can't seem to get rid of the errors so i've taken a screenshot of my code: http://i51.tinypic.com/34fogmd.png The studentResponses[row][column] = scanner.nextInt(); line is probably wrong but I think the main problem lies somewhere else since I'm getting the error "java.util.InputMismatch exception: null(in java.util.Scanner)" everytime it gets to that line – andy94 Feb 26 '13 at 17:48
  • Don't take a screenshot. **Edit your question to include your code**. –  Feb 26 '13 at 19:23

1 Answers1

1

You can use the useDelimiter(str) method in Scanner with ',' as its argument to separate your input with commas. Afterwards you can just remove spaces around the word and lowercase the string with trim() and toLowerCase(). After that you have your string in a standard way so that you can convert it into an integer however you see fit. Below is some code that does just that

import java.util.*;

public class Parser {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        sc.useDelimiter(",");
        while (sc.hasNext()) {
            String s = sc.next().trim().toLowerCase();
            // Now check s accordingly to do what you want it to do
        }
    }
}

Note: you should use the useDelimiter() method after parsing the first two lines in your file since at the beginning you do want to use whitespace as delimiter.

Gustavo Torres
  • 464
  • 6
  • 22
  • Thanks for your response, going to try it when I get back from work and yes I do have the useDelimiter() line after parsing the first two lines. – andy94 Feb 26 '13 at 17:37