This is the block of data I have to work with:

    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

In a previous exercise I was given the same set of data except everything is separated by a comma rather than with spaces then comma. My lecturer hinted that I would only need to make a slight modification to my existing code in order to complete this exercise but I still can't think of anything. Any help would be appreciated! :)

This is my existing code:

public void readMarksData(String fileName, int numberOfStudents, int responsesPerStudent) throws FileNotFoundException
    {
        studentResponses = new int[numberOfStudents][responsesPerStudent];
        File dataFile = new File(fileName);
        Scanner scanner = new Scanner (dataFile);
        cohort = scanner.nextLine();
        int numberOfResponses = Integer.parseInt(scanner.nextLine());
        System.out.println(cohort);
        System.out.println(numberOfResponses);


    for (int row = 0; row < studentResponses.length; row++) //Take in one line from the data file at a time and loop from 1 to 10 to read in the integer values and 10 to 14 to read in the feedback values
    {

        Scanner scanner2 = new Scanner(scanner.nextLine());
        scanner2.useDelimiter("[ ]*(,)[ ]*");

        for (int column = 0; column < studentResponses[0].length-4; column++)
        {

            studentResponses[row][column] = scanner2.nextInt();
            System.out.print(studentResponses[row][column]+" ");

        }
        System.out.print("\t");
        for (int column = studentResponses[0].length-4; column < studentResponses[0].length; column++)
        {
             switch( scanner2.next().toLowerCase())
             {
                 case "excellent" : studentResponses[row][column] = 5;
                 break;
                 case "very good" : studentResponses[row][column] = 4;
                 break;
                 case "good" : studentResponses[row][column] = 3;
                 break;
                 case "average" : studentResponses[row][column] = 2;
                 break;
                 case "bad" : studentResponses[row][column] = 1;
                 break;

             }
             System.out.print(" "+studentResponses[row][column]);
        }
       scanner2.close(); 
    }    
    scanner.close(); 
}

take a close look at this line in your code

scanner2.useDelimiter("[ ]*(,)[ ]*");

or, you could read the line completely (what you're doing with nextLine) and use the split method of the String class.

This article has been dead for over six months. Start a new discussion instead.