Hello I am currently running into an error in one of my java programs and cannot
ID: 3801253 • Letter: H
Question
Hello I am currently running into an error in one of my java programs and cannot seem to determine why. Hopefully you can help me with this question.
Here is my program
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Student {
// static variables
static String path2StuFile = "Student2017.txt";
static String path2MinFile = "Post Secondary Education.txt";
// instance attribs
int studentId;
String firstName;
String lastName;
ArrayList mins = new ArrayList<>();
public static void studIdAndReadMins(ArrayList stdList) throws IOException {
ArrayList minsReadByStudId = new ArrayList<>();
minsReadByStudId = new ArrayList(Files.readAllLines(Paths.get(path2MinFile)));
Scanner sc = new Scanner(new FileReader(path2MinFile));
for (Student std : stdList) {
for (int i=0;i if(i!= 0){
String[] minArray = minsReadByStudId.get(i).split(" ");
int minutes = Integer.parseInt(minArray[0]);
int id = Integer.parseInt(minArray[1]);
if (std.studentId == id) {
std.mins.add(minutes);
}
}
}
}
}
public static void main(String[] args) throws IOException {
ArrayList stdList = new ArrayList<>();
Scanner sc = new Scanner(new FileReader(path2StuFile));
while(sc.hasNext()){
Student stud = new Student();
String[] data = sc.nextLine().split("\|");
stud.studentId = Integer.parseInt(data[0]);
stud.firstName = data[1];
stud.lastName = data[2];
stdList.add(stud);
}
studIdAndReadMins(stdList);
System.out.println("Sum of Minutes Read: ");
System.out.println("Name MinutesRead");
for (Student std : stdList) {
System.out.print(std.firstName+" "+std.lastName+" ");
int sum = 0;
for(int min:std.mins){
sum+=min;
}
System.out.print(sum+" ");
}
}
}
Here is my errors.
Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.nio.file.Files.readAllLines(Unknown Source)
at java.nio.file.Files.readAllLines(Unknown Source)
at Student.studIdAndReadMins(Student.java:26)
at Student.main(Student.java:63)
Here is the question i am trying to answer.
Program 1
Each year, elementary school kids must keep track of the number of minutes they read each week. This ensures that they continue progressing in their reading and become lifelong readers. In this program, you are to create an application that shows the sum total of the minutes read by each student. There are two files in which you are to read. The first one, Student2017.txt contains all the students. The second one, MinutesRead.txt contains the minutes read and the student id. You are to create a Student class that contains an ArrayList of all the minutes read for each student. The end user should be able to print out the individual minutes read by a student and the sum total of the minutes ready by a student. An example of output might be like this:
-----------------------------------------------------
Sum of Minutes Read
Name Minutes Read
Sammy Hagger 1,203
Nick Jonas 3,435
…
------------------------------------------------------
Individual Minutes Read
Name Minutes Read
Sammy Hagger 234
Sammy Hagger 345
Nick Jonas 948
Nick Jonas 232
…
------------------------------------------------------
------------------------------------------------------
students 2014
1|Claude|Camero
2|Stanley|Stewart
3|Betty|Bonsai
4|Gina|Ginger
5|Paul|Peacock
MinutesRead.txt
MinutesRead Person
395 2
246 1
176 1
394 4
116 3
336 3
316 5
159 1
239 3
154 2
254 3
436 4
213 2
291 4
371 3
201 1
429 5
486 5
389 5
396 3
246 1
495 1
143 2
351 5
313 1
110 4
439 1
428 5
349 2
281 5
277 1
437 5
480 4
468 5
176 5
298 2
238 1
106 1
344 3
204 4
220 2
472 4
230 5
186 5
457 1
424 3
446 2
142 3
383 5
137 4
239 4
122 4
258 5
370 3
147 2
222 1
354 4
296 2
426 3
480 5
345 5
268 4
236 5
186 2
497 1
379 3
478 5
432 3
182 1
358 4
126 4
263 5
333 2
169 5
286 2
269 3
487 4
294 3
184 5
333 1
247 3
148 3
308 2
359 4
473 5
411 1
256 3
120 1
151 1
178 3
375 3
468 3
253 1
247 1
419 4
120 4
104 5
398 5
439 3
489 5
317 2
111 5
196 2
450 4
200 3
487 3
240 3
353 4
477 3
378 1
430 2
204 2
299 4
429 5
292 3
120 1
101 1
459 2
239 4
145 4
252 5
437 1
167 3
115 2
202 4
256 4
421 4
265 4
452 5
473 2
138 5
447 1
189 3
200 3
112 2
233 5
198 5
448 5
103 2
154 5
451 1
465 3
177 5
279 4
429 2
284 5
204 1
397 1
299 4
439 1
455 5
358 5
443 4
387 5
452 5
158 4
397 4
439 3
118 5
102 2
135 2
174 1
131 2
259 4
349 1
462 1
176 4
125 5
272 1
233 5
336 4
250 1
495 5
367 3
320 5
458 3
111 2
370 2
Explanation / Answer
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class Student
{
// static variables
static String path2StuFile = "Student2017.txt";
static String path2MinFile = "PostSecondaryEducation.txt";
// instance attribs
int studentId;
String firstName;
String lastName;
ArrayList<Integer> mins = new ArrayList<>();
public static void studIdAndReadMins(ArrayList<Student> stdList) throws IOException
{
ArrayList<String> minsReadByStudId = new ArrayList<>();
minsReadByStudId = new ArrayList<String>(Files.readAllLines(Paths.get(path2MinFile)));
Scanner sc = new Scanner(new FileReader(path2MinFile));
for (Student std : stdList)
{
for (int i=0;i<minsReadByStudId.size();i++)
{
if(i!= 0)
{
// split string to array had to be modified inorder to
// save the string in array
String[] minArray = minsReadByStudId.get(i).split("");
int minutes = Integer.parseInt(minArray[0]);
int id = Integer.parseInt(minArray[1]);
if (std.studentId == id)
{
std.mins.add(minutes);
}
}
}
}
}
public static void main(String[] args) throws IOException
{
ArrayList<Student> stdList = new ArrayList<>();
Scanner sc = new Scanner(new FileReader(path2StuFile));
while(sc.hasNext())
{
Student stud = new Student();
String[] data = sc.nextLine().split("\|");
stud.studentId = Integer.parseInt(data[0]);
stud.firstName = data[1];
stud.lastName = data[2];
stdList.add(stud);
}
studIdAndReadMins(stdList);
System.out.println("Sum of Minutes Read: ");
System.out.println("Name MinutesRead");
for (Student std : stdList)
{
System.out.print(std.firstName+" "+std.lastName+" ");
int sum = 0;
for(int min:std.mins)
{
sum+=min;
}
System.out.print(sum+" ");
}
}
}
/*
output:
Sum of Minutes Read:
Name MinutesRead
Claude Camero 26
Stanley Stewart 41
Betty Bonsai 68
Gina Ginger 43
Paul Peacock 74
Student2017.txt
1|Claude|Camero
2|Stanley|Stewart
3|Betty|Bonsai
4|Gina|Ginger
5|Paul|Peacock
PostSecondaryEducation.txt
395 2
246 1
176 1
394 4
116 3
336 3
316 5
159 1
239 3
154 2
254 3
436 4
213 2
291 4
371 3
201 1
429 5
486 5
389 5
396 3
246 1
495 1
143 2
351 5
313 1
110 4
439 1
428 5
349 2
281 5
277 1
437 5
480 4
468 5
176 5
298 2
238 1
106 1
344 3
204 4
220 2
472 4
230 5
186 5
457 1
424 3
446 2
142 3
383 5
137 4
239 4
122 4
258 5
370 3
147 2
222 1
354 4
296 2
426 3
480 5
345 5
268 4
236 5
186 2
497 1
379 3
478 5
432 3
182 1
358 4
126 4
263 5
333 2
169 5
286 2
269 3
487 4
294 3
184 5
333 1
247 3
148 3
308 2
359 4
473 5
411 1
256 3
120 1
151 1
178 3
375 3
468 3
253 1
247 1
419 4
120 4
104 5
398 5
439 3
489 5
317 2
111 5
196 2
450 4
200 3
487 3
240 3
353 4
477 3
378 1
430 2
204 2
299 4
429 5
292 3
120 1
101 1
459 2
239 4
145 4
252 5
437 1
167 3
115 2
202 4
256 4
421 4
265 4
452 5
473 2
138 5
447 1
189 3
200 3
112 2
233 5
198 5
448 5
103 2
154 5
451 1
465 3
177 5
279 4
429 2
284 5
204 1
397 1
299 4
439 1
455 5
358 5
443 4
387 5
452 5
158 4
397 4
439 3
118 5
102 2
135 2
174 1
131 2
259 4
349 1
462 1
176 4
125 5
272 1
233 5
336 4
250 1
495 5
367 3
320 5
458 3
111 2
370 2
350 5
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.