Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java programming help My program wont run. could someone tell me why. everything seems correct to me... giving me the error: Exception in thread main

Java programming help

My program wont run. could someone tell me why. everything seems correct to me...

giving me the error:

Exception in thread "main" java.lang.NumberFormatException: For input string: "Stud"

at java.base/java.lang.NumberFormatException.forInputString(Unknown Source)

at java.base/java.lang.Integer.parseInt(Unknown Source)

at java.base/java.lang.Integer.parseInt(Unknown Source)

at Util.readFile(Util.java:35)

at Driver.main(Driver.java:8)

_________________________________________________________________________________________________________________________

Program Prompt:

Write a program to perform statistical analysis of scores for a class of students.The class may have up to 40 students.There are five quizzes during the term. Each student is identified by a four-digit student ID number.

The program is to print the student scores and calculate and print the statistics for each quiz. The output is in the same order as the input; no sorting is needed. The input is to be read from a text file. The output from the program should be similar to the following:

Here is some sample data (not to be used) for calculations:

Stud Q1 Q2 Q3 Q4 Q5

1234 78 83 87 91 86

2134 67 77 84 82 79

1852 77 89 93 87 71

High Score 78 89 93 91 86

Low Score 67 77 84 82 71

Average 73.4 83.0 88.2 86.6 78.6

The program should print the lowest and highest scores for each quiz.

_________________________________________________________________________________________________________

PROGRAM and txt file below

public class Driver

{

public static void main(String[] args)

{

Student arrStudent[] = new Student[40];

int studentCount=0;

arrStudent = Util.readFile("C:\\Users\\JamesJr\\eclipse-workspace\\test\\src\\Data.txt", arrStudent);

// find number of lines from file. which will show number of students in array.

studentCount= Util.studentCount;

Statistics s = new Statistics();

// print student data fetched from file

for(int i = 0; i

{

arrStudent[i].printData();

}

// print statistics of students

s.printStatistics(arrStudent,studentCount);

}

}

____________________________________________________________________________________________

import java.io.*;

import java.util.StringTokenizer;

public class Util

{

static int studentCount;

static Student [] readFile(String filename, Student [] stu)

{

//Reads the file and builds student array.

int i=0;

try

{

//Open the file using FileReader Object.

FileReader file = new FileReader(filename);

BufferedReader buff = new BufferedReader(file);

boolean eof = false;

while (!eof)

{

//In a loop read a line using readLine method.

String line = buff.readLine();

if (line == null)

eof = true;

else

{

// System.out.println(line);

stu[i]= new Student();

//Tokenize each line using StringTokenizer Object

StringTokenizer st = new StringTokenizer(line);

while(st.hasMoreTokens())

{

//Value is then saved in the right property of Student Object.

stu[i].setSID(Integer.parseInt(st.nextToken()));

int[] arr= new int[5];

for(int j=0;j<5;j++)

{

//Each token is converted from String to Integer using parseInt method

arr[j]= Integer.parseInt(st.nextToken());

}

//Value is then saved in the right property of Student Object.

stu[i].setScores(arr);

}

}

i++;

}

buff.close();

} catch (IOException e)

{

System.out.println("Error -- " + e.toString());

}

studentCount = i-1;

return stu;

}

}

___________________________________________________________________________________________________

public class Statistics

{

private int[] lowscores= new int[5];

private int[] highscores= new int[5];

private float[] avgscores= new float[5];

void findlow(Student[] a, int c)

{

// finds low score.

int i=0;

for(i=0;i<5; i++)

{

int[] temp= a[0].getScores();

int min = temp[i];

for(int j=0; j< c; j++)

{

int[] s= a[j].getScores();

if(min > s[i])

{

min = s[i];

}

}

lowscores[i]= min;

}

}

//find high score

void findhigh(Student[] a, int c)

{

int i=0;

for(i=0;i<5; i++)

{

int[] temp= a[0].getScores();

int max = temp[i];

for(int j=0; j< c; j++)

{

int[] s= a[j].getScores();

if(max < s[i])

{

max = s[i];

}

}

highscores[i]= max;

}

}

//find average score.

void findavg(Student[] a, int c)

{

int i=0;

for(i=0;i<5; i++)

{

int sum =0;

for(int j=0; j

{

int[] s= a[j].getScores();

sum= sum + s[i];

}

avgscores[i]= sum/c;

}

}

//print statistics for all students and all quizes

public void printStatistics(Student[] a, int c)

{

findlow(a,c);

findhigh(a,c);

findavg(a,c);

System.out.println("Lowest score :\t\t"+ lowscores[0] +"\t"+ lowscores[1] +"\t"+ lowscores[2] +"\t"+ lowscores[3] +"\t"+ lowscores[4] );

System.out.println("High score :\t\t"+ highscores[0] +"\t"+ highscores[1] +"\t"+ highscores[2] +"\t"+ highscores[3] +"\t"+ highscores[4] );

System.out.println("Average score :\t\t"+ avgscores[0] +"\t"+ avgscores[1] +"\t"+ avgscores[2] +"\t"+ avgscores[3] +"\t"+ avgscores[4] );

}

}

___________________________________________________________________________________________________________

public class Student

{

private int SID;

private int scores[] = new int[5];

//get and set functions for scores and ID

public int getSID()

{

return this.SID;

}

public void setSID(int id)

{

this.SID= id;

}

public int[] getScores()

{

return this.scores;

}

public void setScores(int[] s)

{

this.scores= s;

}

//print all data of a student

public void printData()

{

System.out.println("\t\t\t\t"+ SID +"\t"+ scores[0] +"\t"+ scores[1] +"\t"+ scores[2] + "\t"+ scores[3] +"\t"+ scores[4] +" ");

}

}

________________________________________________________________________________________________________

Data.txt

Stud Qu1 Qu2 Qu3 Qu4 Qu5

1234 052 007 100 078 034

2134 090 036 090 077 030

3124 100 045 020 090 070

4532 011 017 081 032 077

5678 020 012 045 078 034

6134 034 080 055 078 045

7874 060 100 056 078 078

8026 070 010 066 078 056

9893 034 009 077 078 020

1947 045 040 088 078 055

2877 055 050 099 078 080

3189 022 070 100 078 077

4602 089 050 091 078 060

5405 011 011 000 078 010

6999 000 098 089 078 020

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Upgrading Oracle Databases Oracle Database New Features

Authors: Charles Kim, Gary Gordhamer, Sean Scott

1st Edition

B0BL12WFP6, 979-8359657501

More Books

Students also viewed these Databases questions