-
Using for loop to read in data from text file using Scanner class
Hi guys,
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. Maybe i'm just not thinking clear but I've already spent about 5 hours trying to figure this out to no avail so any guidance would be appreciated. Thanks!
-
Re: Using for loop to read in data from text file using Scanner class
write the code in simple steps.
First make a loop and in the loop read a line and print it
continue until all the lines have been read and printed.
When that works, then worry about how to parse the contents of a line and use the data it contains.
-
Re: Using for loop to read in data from text file using Scanner class
Thanks for your responses. This is my attempt:
Code java:
public void readMarksData(String fileName, int numberOfStudents, int responsesPerStudent) throws FileNotFoundException
{
int feedbackScore;
studentResponses = new int[numberOfStudents][responsesPerStudent]; //Creates 2D array
File dataFile = new File(fileName);
Scanner scanner = new Scanner (dataFile);
cohort = scanner.nextLine(); //Assigns the first line of the text file to the field "cohort"
System.out.println (cohort);
int numberOfResponses = scanner.nextInt(); //Assigns the second line of the text file to the field "numberOfResponses"
System.out.println (numberOfResponses);
scanner.useDelimiter("[ ]*(,)[ ]*");
for (int row = 0; row < studentResponses.length; row++)
{
for (int column = 0; column < studentResponses[0].length; column++){
studentResponses[row][column] = scanner.nextInt();
System.out.print(studentResponses[row][column]);
}
}
scanner.close();
}
Once it gets to the studentResponses[row][column] = scanner.nextInt(); part I get this error: "java.util.InputMismatch exception: null(in java.util.Scanner)".
-
Re: Using for loop to read in data from text file using Scanner class
Please edit your post and wrap your code with
[code=java]
<YOUR CODE HERE>
[/code]
to get highlighting and preserve formatting.
Where is the loop that reads the lines and prints them out?
Does that part of the code work?
When that part works, then move to the parsing of the line to get the data on the line.
How will the code get the data from the line? Make a list of the steps it should use to get the various pieces of data contained on the line.
-
Re: Using for loop to read in data from text file using Scanner class
The loop that prints out everything being read in is
Code java:
for (int column = 0; column < studentResponses[0].length; column++){
studentResponses[row][column] = scanner.nextInt();
System.out.print(studentResponses[row][column]);
}
Everything else above the loop works
-
Re: Using for loop to read in data from text file using Scanner class
Quote:
The loop that prints out everything being read in is
That loop doesn't read a line, it reads tokens.
If you read a line first and then parse that line, you will have better control over what happens.
You described the lines in the file as having different parts: ints and Strings.
It will be easier to work on a line by line basis than with token by token
-
Re: Using for loop to read in data from text file using Scanner class
I see, so what modifications would you suggest I make to my code?
-
Re: Using for loop to read in data from text file using Scanner class
Start by making a loop that reads the lines and prints them.
When that works, work on how to parse the data in the line.
Describe what data is on a line and say where that data should be saved.
When you can describe where each part of the data goes, then work on writing the code to do it.
-
Re: Using for loop to read in data from text file using Scanner class
I think this should work, basically it's going to print out everything again separated by a space rather than comma and put a tab space between the numbers and the strings. However I'm still getting the same error as before and can't figure out why I'm getting it :mad:
Code java:
public void readMarksData(String fileName, int numberOfStudents, int responsesPerStudent) throws FileNotFoundException
{
int feedbackScore;
studentResponses = new int[numberOfStudents][responsesPerStudent];
File dataFile = new File(fileName);
Scanner scanner = new Scanner (dataFile);
cohort = scanner.nextLine();
System.out.println (cohort);
int numberOfResponses = scanner.nextInt();
System.out.println (numberOfResponses);
scanner.useDelimiter(",");
for (int row = 0; row < 8; row++)
{
Scanner scanner2 = new Scanner(scanner.nextLine());
for (int column = 0; column < studentResponses[0].length-4; column++)
{
scanner2.useDelimiter(",");
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++)
{
scanner2.useDelimiter("[ ]*(,)[ ]*");
String test = scanner.next();
System.out.print(" "+test);
}
scanner2.close();
}
scanner.close();
}
-
Re: Using for loop to read in data from text file using Scanner class
Have you done this yet?
Describe what data is on a line and say where that data should be saved.
When you can describe where each part of the data goes, then work on writing the code to do it.
Have you given up on my suggestions on how to do this project?
There are some poor techniques in the posted code:
for (int row = 0; row < 8; row++)
Use the number from the file, not an 8 here. What if the data file were changed?
for (int column = studentResponses[0].length-4; column < studentResponses[0].length; column++)
What is the -4 for?
-
Re: Using for loop to read in data from text file using Scanner class
Well I've asked someone in my class to give me some pointers so I'm just trying out his method. Yeah I usually use studentResponses.length rather than a number, the 8 was just there for testing purposes, I've changed it back now.
Basically, scanner will pass a whole line of data to scanner2, the first inner loop will loop from element 1 to 10 which are the integer elements which is why I have studentResponses[0].length-4 because there are 14 elements in total and the last 4 are Strings. The second inner loop will loop from 10 to 14 and read in the Strings.
I'm currently just getting the program to tokenize the data properly and print it to the terminal, the rest is quite easy
-
Re: Using for loop to read in data from text file using Scanner class
Have you fixed the error?
-
Re: Using for loop to read in data from text file using Scanner class
No, I get the error everytime it gets to the line studentResponses[row][column] = scanner2.nextInt();
-
Re: Using for loop to read in data from text file using Scanner class
Please post the full text of the error message.
Copy the full contents of the console and paste it including the debug print out and the error message.
-
Re: Using for loop to read in data from text file using Scanner class
I mentioned this error earlier, the full content of the console is: java.util.InputMismatch exception: null(in java.util.Scanner)
-
Re: Using for loop to read in data from text file using Scanner class
I look at a lot of posts and don't remember. It makes it easier if the message is printed or the post# to look at is posted.
Is Only that one line is the full text of the console???? Normal error message texts have lots more lines to them.
That means the error is happening somewhere else, not in this method because the posted method has this println in it:
System.out.println (cohort);
And that was not printed, which means that the above method was not executed.
-
Re: Using for loop to read in data from text file using Scanner class
Oh so sorry, yeah the Cohort and numberOfResponses get printed out fine. The program prints those two lines, then goes to print the actual data, starting with the integers, and that's when the error appears.
Code java:
public void readMarksData(String fileName, int numberOfStudents, int responsesPerStudent) throws FileNotFoundException
{
int feedbackScore;
studentResponses = new int[numberOfStudents][responsesPerStudent];
File dataFile = new File(fileName);
Scanner scanner = new Scanner (dataFile);
cohort = scanner.nextLine();
int numberOfResponses = scanner.nextInt();
System.out.println (cohort);
System.out.println (numberOfResponses);
scanner.useDelimiter(",");
for (int row = 0; row < studentResponses.length; row++)
{
Scanner scanner2 = new Scanner(scanner.nextLine());
for (int column = 0; column < studentResponses[0].length-4; column++)
{
scanner2.useDelimiter(",");
studentResponses[row][column] = scanner2.nextInt();
//System.out.print(" "+studentResponses[row][column]);
//System.out.print(scanner2.nextInt());
}
System.out.print("\t");
for (int column = studentResponses[0].length-4; column < studentResponses[0].length; column++)
{
scanner2.useDelimiter("[ ]*(,)[ ]*");
String feedback = scanner2.next();
switch (feedback.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;
default: studentResponses[row][column] = 1;
}
}
scanner2.close();
}
scanner.close();
}
-
Re: Using for loop to read in data from text file using Scanner class
Quote:
goes to print the actual data, starting with the integers, and that's when the error appears.
That is what is needed to see where the error is. The last thing printed tells you what was being read when the error happens.
-
Re: Using for loop to read in data from text file using Scanner class
The first line is 5,5,5,6,5,8,9,5,6,8, good, very good, excellent, good and I've set the delimiter to a comma so I would've thought it'd print the first integer before crashing since I can't see anything wrong with studentResponses[row][column]=scanner2.nextInt()
-
Re: Using for loop to read in data from text file using Scanner class
What was the last thing printed before the error?
How many numbers were printed before the error?
What was being read when the error happened?
Was it a numeric value or a String?
-
Re: Using for loop to read in data from text file using Scanner class
Quote:
Originally Posted by
Norm
What was the last thing printed before the error?
How many numbers were printed before the error?
What was being read when the error happened?
Was it a numeric value or a String?
CS1 2012 Group 1 - printed first using scanner.hasNextLine()
8 - printed second as int on a new line scanner.hasNextInt()
One number was printed before the error which is the 8
studentResponses[row][column]=scanner2.nextInt() was being read when the error happened
-
Re: Using for loop to read in data from text file using Scanner class
Quote:
One number was printed
It's important to know what was being read when the error happened so you can change the code to handle the problem. Not copying and pasting here all that was printed just makes the debugging process take more time.
What line was it reading the numbers from?
While still debugging the program You need to print out the lines as they are read so you know where the problem is.
-
1 Attachment(s)
Re: Using for loop to read in data from text file using Scanner class
Here's a screenshot of everything, the attached file is horribly pixelated so use this link : http://i56.tinypic.com/1xxfnn.png
-
Re: Using for loop to read in data from text file using Scanner class
Sorry, images are hard to work with. You can't copy text from an image.
Copy and paste here the full text of the console from when the program is executed.
I'm done for tonight. Back tomorrow.