I'm having a bit of trouble with this project:
We're given a text file of 5 students, with 4 number grades following each name in a separate line. We have to use our main method to read the file, then perform calculations in the gradebook class. However, from what others in class have been saying the method we're using is archaic at best, involving Parallel arrays. We don't really seem to have any other option though, so I'm making due with what I can. But near what I'm hoping is the end of this code, I've encountered a problem, where it says the index that contains the grades for students is out of bounds.
I've rewritten the readFile method in my main in quite a few different ways, but no matter what I still get these errors. Could anyone help me understand what's going on? I'll post everything I can.
I also apologize if I'm a bit slow/incapable with this, this is my first time coding java in months unfortunately.
Also, in the error I post, line 64 of the main class is: grades[test] = inputFile.nextDouble();
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at Package1.GradeBookDemo.readFile(GradeBookDemo.java:64)
at Package1.GradeBookDemo.main(GradeBookDemo.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
The following is my Main method:
import java.util.Scanner;
import java.io.*;
public class GradeBookDemo {
public static void main(String[] args) throws IOException {
// Create a new gradebook object
Gradebook gradeBook = new Gradebook();
//Read StudentInfo file for names and test scores using readFile method
readFile(gradeBook);
//Output Student data
for (int index = 1; index <= 5; index++)
{
System.out.println("Name: " + gradeBook.getName(index) +
"\tAverage Score: " + gradeBook.getAverage(index) +
"\tGrade: " + gradeBook.getLetterGrade(index));
}
}
// This method reads from the StudentInfo file
public static void readFile(Gradebook gradeBook) throws IOException
{
int nI = 0; // Name index
int gI = 4; // Grade index
// Create a string array to hold student names and another for grades
double[] grades = new double[gI];
// Access the StudentInfo text file
File sFile = new File("StudentInfo.txt");
// Create a scanner object to read the file
Scanner inputFile = new Scanner(sFile);
// Read StudentInfo
for(int student = 1; student <= 5; student++)
{
String name = inputFile.nextLine();
gradeBook.setName(student, name);
for (int test = 0; test < 4; test++)
{
grades[test] = inputFile.nextDouble();
}
}
// Close the file
inputFile.close();
}
}
And then the Gradebook class
public class Gradebook {
// Declare fields
private final int NUM_STUDENTS = 5;
private final int NUM_TESTS = 4;
// ArrayList for names of students - 5 in total
private String[] names = new String[NUM_STUDENTS];
// Array to store letter grades
private char[] grades;
// array to store each student's scores
private double[] scores1 = new double[NUM_TESTS];
private double[] scores2 = new double[NUM_TESTS];
private double[] scores3 = new double[NUM_TESTS];
private double[] scores4 = new double[NUM_TESTS];
private double[] scores5 = new double[NUM_TESTS];
// Method to set student's name
public void setName(int studentNumber, String name)
{
names[studentNumber-1] = name;
}
// Method sets student scores
public void setScores(int studentNumber, double[] scores)
{
switch(studentNumber)
{
case 1:copyArray(scores1,scores); break;
case 2:copyArray(scores2,scores); break;
case 3:copyArray(scores3,scores); break;
case 4:copyArray(scores4,scores); break;
case 5:copyArray(scores5,scores); break;
default:break;
}
}
// Returns the student's name
public String getName(int studentNumber)
{
return names[studentNumber-1];
}
// Returns student's average score
public double getAverage(int studentNumber)
{
double avg=0.0;
switch(studentNumber)
{
case 1:avg = calcAverage(scores1); break;
case 2:avg = calcAverage(scores2); break;
case 3:avg = calcAverage(scores3); break;
case 4:avg = calcAverage(scores4); break;
case 5:avg = calcAverage(scores5); break;
default:break;
}
return avg;
}
// Returns the student's letter grade
public char getLetterGrade(int studentNumber)
{
char lettergrade;
if(getAverage(studentNumber)>=90 && getAverage(studentNumber)<=100)
lettergrade = 'A';
else if(getAverage(studentNumber)>=80 && getAverage(studentNumber)<=89)
lettergrade = 'B';
else if(getAverage(studentNumber)>=70 && getAverage(studentNumber)<=79)
lettergrade = 'C';
else if(getAverage(studentNumber)>=60 && getAverage(studentNumber)<=69)
lettergrade = 'D';
else
lettergrade = 'F';
return lettergrade;
}
// Calculates the student's average
private double calcAverage(double[] scores)
{
double sum=0;
for(int i=0; i<scores.length; i++)
sum+=scores[i];
return sum/scores.length;
}
// Determines student's letter grade based on average score
public char LetterGrade(double average)
{
char lettergrade;
if(average>=90 && average<=100)
lettergrade = 'A';
else if(average>=80 && average<=89)
lettergrade = 'B';
else if(average>=70 && average<=79)
lettergrade = 'C';
else if(average>=60 && average<=69)
lettergrade = 'D';
else
lettergrade = 'F';
return lettergrade;
}
// Array copy method
private void copyArray(double[] to, double[] from)
{
System.arraycopy(from, 0, to, 0, from.length);
}
}
Here is the file the program is reading - StudentInfo.txt:
Joanne Smith
98
89
100
76
Will Jones
67
89
91
88
Kerry McDonald
78
79
88
91
Sam Young
88
98
76
56
Jill Barnes
94
93
91
98
You will get InputMismatchException
Reason
String name = inputFile.nextLine(); //Reads the current line, Scanner moves to next line
grades[test] = inputFile.nextDouble(); //Reads the next double value
You should be able to read first student name and grades without any issues. After reading 76, the scanner position is at the end of line 5. It didn't skip line 5.
So, when you try to read next student name by calling nextLine(). you will see "". and scanner moves to line 6.
So the next call to nextDouble() fails.
Quick fix
use Double.parseDouble(inputFile.nextLine()); for reading the double values here
This is a rather interesting "This can not happen" scenario.
From the comments I would guess that the code actually compiled has an error in the inner for-loop. You need to find which file you are actually compiling.
Try adding random
System.out.println("Got HERE!");
to your code and see if any of them are actually printed.
Because the code you show should not be able to give that exception.
Related
The file contains the data in the following format:
Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
I am trying to find out how to get the sum of each column (Ex. Test 1 = 82 + 92 + 68 + ...) to ultimately calculate the average score for each test.
This is how I parsed the file and did the other calculations:
public class TestAverages
{
private static int[] grades;
private static int[] testTotal;
private static int N;
private static double classTotal;
public static void main(String[] args) throws FileNotFoundException
{
File input = new File("TestData.txt");
Scanner in = new Scanner(input);
parseFile(in);
}
public static void parseFile(Scanner in) throws FileNotFoundException
{
TestAverages t = new TestAverages();
in.nextLine(); //skips the first line of text in file (labels)
double classAvg =0.0;
while(in.hasNextLine())
{
String line = in.nextLine();
String[] data = line.split("\\|");
String name = data[0];
grades = new int[data.length - 1];
N = grades.length;
for(int i = 0; i < N; i++)
{
grades[i] = Integer.parseInt(data[i + 1]);
}
System.out.println(name);
System.out.println("Student Average: " + t.getStudentAvg(grades) + "%\n");
classAvg = t.getClassAvg(grades);
System.out.println("Test Average: " + t.getTestAvg(grades) + "%\n");
}
System.out.printf("\nClass Average: %.2f%%\n", classAvg );
}
public double getStudentAvg(int[] grades)
{
double total = 0.0;
double avg = 0.0;
int N = grades.length;
for(int i = 0; i < N; i++){
total += grades[i];}
avg = total / N;
return avg;
}
public double getClassAvg(int[] grades)
{
double classTotal = getStudentAvg(grades) / N;
double classAvg =0.0;
classTotal += classTotal;
classAvg = classTotal;
return classTotal;
}
}
Please excuse my formatting if it's not up to par to the standard.
My main issue at the moment is how to extract the score for each test for each student and add everything up.
The test scores for each student are being calculated. What you need are the class average and the test average.
The test averages are the average scores on each test.
The student averages are the average test scores for each student
The class averages is how the overall class did on all the tests.
Consider three tests
test1 test2 test3 student average
student1 90 80 70 240/3
student2 100 90 90 280/3
student3 80 80 90 250/3
testAvg 270/3 250/3 250/3
class avg = (270 + 250 + 250)/9 or (240+280+250)/9
So you need to read in the values in such a way so as to facilitate making the other calculations. I would recommend either a Map<String,List<Integer>> for the data where the string is the students name and the list is the test scores for each student. Or you could use a 2D int array. But you need to save the scores so you can do the column average for the test scores. Most of the work is done.
Also, two problems I noticed. You call the method getTestAvg but don't declare it and you declare the method getClassAvg but never call it.
if you already know that there are 10 test total in the file , it should be easy .
you can let int[] testTotal = new int[10]; for testTotal[0] = sum-of-first-column , testTotal[1] = sum-of-second-column and so on . . .
you can read each line in and split them with "\\|" as regex
as you are cycling through lines , let testTotal[0] += data[1] , testTotal[1] += data[2] . . . testTotal[9] += data[10] since data[0] is the name of the student .
this is my first question in this community as you can see I'm a beginner and I have very little knowledge about java and coding in general. however, in my beginner practices, I came up with a little project challenge for myself. as you can see in the figure, the loop starts and it prints out the number that is given to it through the scanner. the problem with my attempt to this code is that it gives me the output value as soon as I press enter. what I want to do is an alternative of this code but I want the output values to be given after the whole loop is done all together.
figure
So, basically what I want is to make the program give me the input values together after the loop ends, instead of giving them separately after each number is put.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
calc(); }
public static int calc (){
Scanner scan = new Scanner(System.in);
int count = 1;
int pass = 0;
int notpass = 0;
System.out.println("how many subjects do you have? ");
boolean check = scan.hasNextInt();
int maxless = scan.nextInt();
if (check){
while(count <= maxless ){
System.out.println("Enter grade number " + count);
int number = scan.nextInt();
System.out.println("grade number" + count + " is " + number);
if (number >= 50){
pass++;
}else{
notpass++;
}
count++;
}
System.out.println("number of passed subjects = " + pass);
System.out.println("number of failed subjects = " + notpass);
}else{
System.out.println("invalid value!");
} return pass;
}
}
I think what you want to do is create an array of int numbers.
It would be something like this:
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int maxless = 5;
int[] numbers = new int[maxless];
int count = 0, pass = 0, notPass = 0;
while(count < maxless){
System.out.println("Enter grade number " + (count + 1) + ":");
numbers[count] = scan.nextInt();
if(numbers[count] >= 50){
pass++;
}
else{
notPass++;
}
count++;
}
for(int i=0; i<maxless; i++){
System.out.println("Grade number " + (i + 1) + " is " + numbers[i]);
}
}
}
The output is the following:
Enter grade number 1:
90
Enter grade number 2:
76
Enter grade number 3:
54
Enter grade number 4:
67
Enter grade number 5:
43
Grade number 1 is 90
Grade number 2 is 76
Grade number 3 is 54
Grade number 4 is 67
Grade number 5 is 43
When dealing with arrays, just remember that the indexation begins at 0. You can read more about arrays here: http://www.dmc.fmph.uniba.sk/public_html/doc/Java/ch5.htm#:~:text=An%20array%20is%20a%20collection,types%20in%20a%20single%20array.
A tip: it's gonna be easier to help if you post the code on your question as a text, not an image, so we can copy it and try it on.
Approach 1 :
You can use ArrayList from Collection Classes and store the result there and after the loop is completed, just print the array in a loop.
Example :
//Import class
import java.util.ArrayList;
//Instantiate object
ArrayList<String> output = new ArayList();
while(condition){
output.add("Your data");
}
for(i = 0; i < condition; i++){
System.out.println(output.get(i));
}
Approach 2 :
Use StringBuilder class and append the output to the string, after the loop is completed, print the string from stringbuilder object.
Example :
//import classes
import java.util.*;
//instantiate object
StringBuilder string = new StringBuilder();
while(condition){
string.append("Your string/n");
}
System.out.print(string.toString());
Approach 3 : (As mentioned by Sarah)
Use arrays to store the result percentage or whatever and format it later in a loop. (Not a feasible approach if you want to store multiple values for the same student)
Example :
int studentMarks[] = new int[array_size];
int i = 0;
while(condition){
studentMarks[i++] = marks;
}
for(int j = 0; j < i; j++)
System.out.println("Marks : " + studentMarks[j]);
I would appreciate knowing how to tackle this type of problems. Thank you in advance.
Here is the question.
The first line of the files contains two integer numbers ;
number-of-records exam-grade
number-of-records : indicates number of the records in the file.
exam-grade : indicates the grade of the exam.
The file follows by students name and their grades.
Sample File: test1.txt
Contains four records, and the exam is out of 80. The file follows by the name and grade of the students:
4 80
Mary 65.5
Jack 43.25
Harry 79.0
Mike 32.5
You have to develop the body of following method:
public static void readWrite(String srcfileName, String dstFileName)
That reads grades of each student from srcFileName, calculates their grade percent, indicates that if student passed or failed, and finally reports the class average, number of the students passed, and number of the students failed the exam and saves the result in dstFileName.
The output file for the previous test file should be:
Mary 81.88 passed
Jack 54.06 passed
Harry 98.75 passed
Mike 40.63 failed
class average:68.83
passed: 3
failed: 1
here is the code I wrote for it,
import java.util.*;
import java.io.*;
public class Lab10Quiz {
public static void main(String[] args) throws FileNotFoundException
{
// Test cases
readWrite("test1.txt", "out1.txt");
readWrite("test2.txt", "out2.txt");
}
/** copies the content of the srcFileName into dstFileName, and add the average of the number to the end of the dstFileName
#param srcFileName : souce file name contains double numbers
#param dstFileName : destination file name
*/
public static void readWrite(String srcFileName, String
dstFileName) throws FileNotFoundException {
// Your code goes here
File output = new File(dstFileName);
PrintWriter outPut = new PrintWriter(output);
double avg = 0;
int count = 0;
double tmp = 0;
Scanner in = new Scanner(new File(srcFileName));
while (in.hasNextDouble()) {
tmp = in.nextDouble();
avg += tmp;
outPut.println(tmp);
count ++;
}
avg = avg / count;
outPut.println("Average = " + avg);
outPut.close();
}
}
This code achieves what you want
double avg = 0;
int failCounter = 0;
String[] keywords = in.nextLine().split(" ");
int studentNumber = Integer.parseInt(keywords[0]);
double examValue = Double.parseDouble(keywords[1]);
for (int i = 0; i < studentNumber; i++) {
keywords = in.nextLine().split(" ");
String studentName = keywords[0];
double studentMark = Double.parseDouble(keywords[1]);
double grade = calculateTotalGrade(studentMark, examValue);
failCounter += (hasFailed(grade) ? 1 : 0);
avg += grade;
outPut.println(String.format("%s \t\t %.2f \t\t %s", studentName, grade, hasFailed(grade) ? "failed" : "passed"));
}
avg = avg / studentNumber;
outPut.println("class average: " + avg);
outPut.println("passed: " + (studentNumber - failCounter));
outPut.println("failed: " + failCounter);
And I extracted some of the logic to below methods.
private static double calculateTotalGrade(double grade, double examValue) {
return grade * 100 / examValue;
}
private static boolean hasFailed(double grade) {
return grade < 50;
}
To answer how to tackle this type of questions:
Look for the simplest way. In this case looping for a finite iterations was easier. So I went with the for loop.
The counter is already given, No need to re-calculate it.
If you are working on a computer, write a little code and test it.
Do more questions like these. (if you go through the first chapters of this book these questions will be easy)
The code was working fine before but getting runtime exception even when no changes were made. The programs purpose is, to be a database for baseball players, which houses a couple of their stats - can be seen in code below. When I run the program now, I get this error
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Assig3.getBatters(Assig3.java:47)
at Assig3.<init>(Assig3.java:62)
at Assig3.main(Assig3.java:248)
This error is not really helping me to solve anything, I am completely lost about, what is the problem which occured overnight . Anyways here is the code, it is split up between three files which receive the data from a fourth plain text file
import java.util.*;
import java.io.*;
// CS 0401 Assignment 3 main program class. Note how this class is set up
// with instance variables and methods. The idea is that the main program is itself an
// object and the methods being called are parts of the object that implement the various
// requirements. I have implemented the initial reading of the data from the file to
// get you started. You must add the menu and all of its required functionality.
// Note that this program WILL NOT COMPILE until you have completed the Batter and BatterDB
// classes to some extent. They do not have to be totally working but all of the methods
// used here must be implemented in order for this code to compile.
public class Assig3
{
private BatterDB theDB;
private Batter currBatter;
private Scanner inScan;
private String fName;
// Note how this method is working. It first reads the number of Batters from the
// file, then for each Batter it gets the names, creates the object, and mutates it
// with the instance methods shown. Finally, it adds the new object to the BatterDB
// object.
public void getBatters(String fName) throws IOException
{
Batter currB;
File inFile = new File(fName);
Scanner inScan = new Scanner(inFile);
int numBatters = inScan.nextInt();
inScan.nextLine();
for (int i = 0; i < numBatters; i++)
{
String first = inScan.nextLine();
String last = inScan.nextLine();
currB = new Batter(first, last);
int ab, h, d, t, hr;
ab = inScan.nextInt(); inScan.nextLine();
currB.setBats(ab);
h = inScan.nextInt(); inScan.nextLine();
currB.setHits(h);
d = inScan.nextInt(); inScan.nextLine();
currB.setDoubles(d);
t = inScan.nextInt(); inScan.nextLine();
currB.setTriples(t);
hr = inScan.nextInt(); inScan.nextLine();
currB.setHR(hr);
theDB.addBatter(currB);
}
}
// Constructor is really where the execution begins. Initialize the
// database (done for you) and then go into the main interactive loop (you
// must add this code).
public Assig3(String fstring) throws IOException
{
Scanner reader = new Scanner(System.in);
fName = fstring;
Batter currB;
theDB = new BatterDB(); // <-- there used to be a 2 in there
getBatters(fName);
System.out.println("The database has been loaded");
System.out.println(theDB.toString());
commandPrompter();
String command = reader.next();
while(!command.equals("8")){
if(command.equals("1")){
System.out.println(theDB.toString());
} else if(command.equals("2")){
System.out.println("First name: ");
String first = reader.next();
System.out.println("Last name: ");
String last = reader.next();
currB = new Batter(first, last);
int ab, h, d, t, hr;
System.out.print("How many times did he bat: ");
ab = reader.nextInt();
currB.setBats(ab);
System.out.println("How many hits: ");
h = reader.nextInt();
if(h>ab || h<0){
while(h>ab || h<0){
System.out.println("Invalid try again: ");
h = reader.nextInt();
}
}
currB.setHits(h);
System.out.println("How many doubles: ");
d = reader.nextInt();
if(d>ab || d<0 || d>h){
while(d>ab || d<0){
System.out.println("Invalid try again: ");
d = reader.nextInt();
}
}
currB.setDoubles(d);
System.out.println("How many triples: ");
t = reader.nextInt();
if(t>ab || t<0 || t>h || (t+d)>h){
while(t>ab || t<0 || t>h || (t+d)>h){
System.out.println("Invalid try again: ");
t = reader.nextInt();
}
}
currB.setTriples(t);
System.out.println("How many Homeruns: ");
hr = reader.nextInt();
if(hr>ab || hr<0 || hr>h || (hr+d+t)>h){
while(hr>ab || hr<0 || hr>h || (hr+d+t)>h){
System.out.println("Invalid try again: ");
hr = reader.nextInt();
}
}
currB.setHR(hr);
theDB.addBatter(currB);
} else if(command.equals("3")){
System.out.println("Player first name: ");
String firstNameSearch = reader.next();
System.out.println("Player last name: ");
String lastNameSearch = reader.next();
currB = theDB.findBatter(firstNameSearch, lastNameSearch);
if(currB == null){
System.out.println("The player you wish to see in not in this database");
} else {
System.out.println(currB.toString());
}
} else if(command.equals("4")){
System.out.println("Player first name: ");
String firstNameSearch = reader.next();
System.out.println("Player last name: ");
String lastNameSearch = reader.next();
currB = theDB.findBatter(firstNameSearch, lastNameSearch);
if(currB == null){
System.out.println("The player you wish to remove in not in this database");
} else {
System.out.println("The player has been removed from the database");
theDB.removeBatter(currB);
}
} else if(command.equals("5")){
System.out.println("Player first name: ");
String firstNameSearch = reader.next();
System.out.println("Player last name: ");
String lastNameSearch = reader.next();
currB = theDB.findBatter(firstNameSearch, lastNameSearch);
if(currB == null){
System.out.println("The player you wish to edit in not in this database");
} else {
int ab, h, d, t, hr;
System.out.print("How many times did he bat: ");
ab = reader.nextInt();
currB.setBats(ab);
System.out.println("How many hits: ");
h = reader.nextInt();
if(h>ab || h<0){
while(h>ab || h<0){
System.out.println("Invalid try again: ");
h = reader.nextInt();
}
}
currB.setHits(h);
System.out.println("How many doubles: ");
d = reader.nextInt();
if(d>ab || d<0 || d>h){
while(d>ab || d<0){
System.out.println("Invalid try again: ");
d = reader.nextInt();
}
}
currB.setDoubles(d);
System.out.println("How many triples: ");
t = reader.nextInt();
if(t>ab || t<0 || t>h || (t+d)>h){
while(t>ab || t<0 || t>h || (t+d)>h){
System.out.println("Invalid try again: ");
t = reader.nextInt();
}
}
currB.setTriples(t);
System.out.println("How many Homeruns: ");
hr = reader.nextInt();
if(hr>ab || hr<0 || hr>h || (hr+d+t)>h){
while(hr>ab || hr<0 || hr>h || (hr+d+t)>h){
System.out.println("Invalid try again: ");
hr = reader.nextInt();
}
}
currB.setHR(hr);
}
} else if(command.equals("6")){
theDB.sortName();
} else if(command.equals("7")){
theDB.sortAve();
} else {
System.out.println("What the heck that was not an option, bye.");
}
commandPrompter();
command = reader.next();
}
theDB.toStringFile();
System.out.println("Thanks for using the DB! Bye.");
}
private void commandPrompter(){
System.out.println("Please choose one of the options below: ");
System.out.println("1) Show the list of players");
System.out.println("2) Add a new player");
System.out.println("3) Search for a player");
System.out.println("4) Remove a player");
System.out.println("5) Update a player");
System.out.println("6) Sort the list alphabetically");
System.out.println("7) Sort the list by batting average");
System.out.println("8) Quit the program [list will be saved]");
}
// Note that the main method here is simply creating an Assig3 object. The
// rest of the execution is done via the constructor and other instance methods
// in the Assig3 class. Note also that this is using a command line argument for
// the name of the file. All of our programs so far have had the "String [] args"
// list in the header -- we are finally using it here to read the file name from the
// command line. That name is then passed into the Assig3 constructor.
public static void main(String [] args) throws IOException
{
Assig3 A3 = new Assig3(args[0]);
}
}
BatterDB:
import java.util.ArrayList;
import java.util.Collections;
import java.util.stream.Collectors;
import java.io.PrintWriter;
// CS 0401 BatterDB class
// This class is a simple database of Batter objects. Note the
// instance variables and methods and read the comments carefully. You minimally
// must implement the methods shown. You may also need to add some private methods.
// To get you started I have implemented the constructor and the addBatter method for you.
public class BatterDB
{
private ArrayList<Batter> theBatters = new ArrayList<Batter>(); // ArrayList of Batters
private int num; // int to store logical size of DB
// Initialize this BatterDB
public BatterDB()
{
num = 0;
}
// Take already created Batter and add it to the DB. This is simply putting
// the new Batter at the end of the array, and incrementing the int to
// indicate that a new movie has been added. If no room is left in the
// array, resize to double the previous size, then add at the end. Note that
// the user never knows that resizing has even occurred, and the resize()
// method is private so it cannot be called by the user.
public void addBatter(Batter b)
{
theBatters.add(b);
num++;
}
// Remove and return the Batter that equals() the argument Batter, or
// return null if the Batter is not found. You should not leave a gap in
// the array, so elements after the removed Batter should be shifted over.
public Batter removeBatter(Batter b)
{
theBatters.remove(b);
return b;
}
// Return logical size of the DB
public int numBatters()
{
return theBatters.size();
}
// Resize the Batter array to that specified by the argument
private void resize(int newsize)
{
//Don't need this now that it's an array list!
}
// Find and return the Batter in the DB matching the first and last
// names provided. Return null if not found.
public Batter findBatter(String fName, String lName)
{
Batter currentB = null;
String fullNameSearch = lName+","+fName;
for(int i=0; i<theBatters.size(); i++){
if(fullNameSearch.toUpperCase().equals(theBatters.get(i).getName().toUpperCase())){
currentB = theBatters.get(i);
}
}
return currentB;
//change
}
// Sort the DB alphabetically using the getName() method of Batters for
// comparison
public void sortName()
{
int j;
for ( j = 0; j < theBatters.size()-1; j++)
{
if ( theBatters.get(j).getName().compareToIgnoreCase(theBatters.get(j+1).getName()) > 0 )
{ // ascending sort
Collections.swap(theBatters, j, j+1);
j=0;
}
if ( theBatters.get(j).getName().compareToIgnoreCase(theBatters.get(j+1).getName()) > 0 )
{ // ascending sort
Collections.swap(theBatters, j, j+1);
j=0;
}
}
}
// Sort the DB from high to low using the getAve() method of Batters for
// comparison
public void sortAve()
{
int j;
for ( j = 0; j < theBatters.size()-1; j++)
{
if ((theBatters.get(j+1).getAve() - theBatters.get(j).getAve()) > 0 )
{ // ascending sort
Collections.swap(theBatters, j, j+1);
j=0;
}
if ((theBatters.get(j+1).getAve() - theBatters.get(j).getAve()) > 0 )
{ // ascending sort
Collections.swap(theBatters, j, j+1);
j=0;
}
}
}
// Return a formatted string containing all of the Batters' info. Note
// that to do this you should call the toString() method for each Batter in
// the DB.
public String toString()
{
return theBatters.stream().map(b -> b.toString()).collect(Collectors.joining("\n"));
}
// Similar to the method above, but now we are not formatting the
// string, so we can write the data to the file.
public void toStringFile()
{
try{
PrintWriter writer = new PrintWriter("batters.txt", "UTF-8");
writer.println(theBatters.size());
for(int i=0; i<theBatters.size(); i++){
String[] parts = theBatters.get(i).getName().split(",");
String fName= parts[1];
String lName = parts[0];
writer.println(fName);
writer.println(lName);
writer.println(theBatters.get(i).getAtBats());
writer.println(theBatters.get(i).getHits());
writer.println(theBatters.get(i).getDoubles());
writer.println(theBatters.get(i).getTriples());
writer.println(theBatters.get(i).getHomeRuns());
}
writer.close();
} catch (Exception e) {
System.out.println("Did not work, sorry :(");
}
}
}
Batter:
public class Batter {
private String firstName;
private String lastName;
private int atBats;
private int hits;
private int doubles;
private int triples;
private int homeRuns;
public Batter(String fName, String lName){
firstName = fName;
lastName = lName;
}
public void setBats(int batCount){
atBats = batCount;
}
public void setHits(int hitCount){
hits = hitCount;
}
public void setDoubles(int doubleCount){
doubles = doubleCount;
}
public void setTriples(int tripleCount){
triples = tripleCount;
}
public void setHR(int homeRunCount){
homeRuns = homeRunCount;
}
public double getAve(){
double one = this.hits;
double two = this.atBats;
double average = (one / two);
return average;
}
public int getAtBats(){
return this.atBats;
}
public int getHits(){
return this.hits;
}
public int getDoubles(){
return this.doubles;
}
public int getTriples(){
return this.triples;
}
public int getHomeRuns(){
return this.homeRuns;
}
public String getName(){
String fullName = this.lastName + "," + this.firstName;
return fullName;
}
public boolean equals(){
return true;
}
public String toString(){
return "Player: "+getName()+"\nAt Bats: "+this.atBats+"\nHits: "+this.hits+"\nDoubles: "+this.doubles+"\nTriples: "+this.triples+"\nHome Runs: "+this.homeRuns;
}
}
batters.txt (text file):
5
Cannot
Hit
635
155
12
7
6
Major
Hitter
610
290
50
25
65
The
Hulk
650
300
0
0
300
Iron
Man
700
600
300
0
300
Herb
Weaselman
600
200
20
15
30
Error :
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at Assig3.getBatters(Assig3.java:47)
at Assig3.<init>(Assig3.java:62)
at Assig3.main(Assig3.java:248)
Description :
NoSuchElementException mean whatever your code is trying to read is not there
mean there is no element which you are trying to read.
At line 47 i.e hr = inScan.nextInt(); inScan.nextLine();
your code is trying to read an int and nextline
but there is no next line in your source file
which basically will happen in the last iteration of your loop
Reason is when your code reach your last file entry i.e 30 then
hr = inScan.nextInt(); inScan.nextLine();
//^^^ this will read 30
// but ^^^ there is no next line and hence the exception
Solution : either use hasNextLine or add nextline by using an enter at the end of the file
hr = inScan.nextInt();
if(inScan.hasNextLine())
inScan.nextLine();
or you can manipulate file then simply add an empty line in your file at the end using enter (which i don't recommend)
I'll copy/paste the assignment, and then the progress I've made thus far. I'm truly not looking for someone to do the assignment, only some assistance to point me in the right direction as to where I'm going wrong? The program is running without error, however it's returning multiple results for a student when it should only be returning one. I can modify the grades and have it produce different results, so I know the math is being done, however I don't know why it's giving multiple (and different) results for a single student. I have been going over the code and cannot determine where I'm going wrong. My best guess is that it's somewhere in the OutputGrade method, I just can't find it.
Instructions:
Create a Java class called student with the following instance variables:
• private String studentName;
• private int [] grades; //an array of grades
Include setter and getter methods to set and get these 2 properties. The setter method for the grades instance array variable — setGrades() — should take a single argument which is an array of int with the grades already filled in. setGrades() should be used if statement(s) to make sure that each grade value in the array parameter is valid (between 0 and 100) — you will need to use a loop in conjunction with the if-statement(s) to do this efficiently. If a grade is out of bounds, setGrades() should set that grade value to 0. The array that you pass to setGrades() must hold between 3 and 5 grades.
Include a method called outputGrade() that averages up all the grades in the student grades array and then uses a switch statement to output grade conversions based on the following criteria:
• For values from 0–59, your program should output: “Student has failed this class”
• For values from 60–69, your program should output: “Student gets a D”
• For values from 70–79, your program should output: “Student gets a C”
• For values from 80–89, your program should output: “Student gets a B”
• For values from 90–100, your program should output: “Student gets an A”
Where “Student” is the actual name of the student.
Create a test class called TestStudent that instantiates 3 students and sets their names and grades and then calls outputGrade() for each student. For your 3 students, 1 must have 3 grades, 1 must have 4 grades, and 1 must have 5 grades. This is so you will have 3 different array sizes to pass to setGrades(). Make sure that for 1 student, setGrades() is called with 1 grade value that is out of bounds (less than 0 or greater than 100).
Code:
public class TestStudent {
public static void main(String[] args) {
Student student1 = new Student();
int[] grades1 = {15, 50, 5};
student1.setStudentName("Ernest Craft");
student1.setGrades(grades1);
student1.outputGrades();
Student student2 = new Student();
int[] grades2 = {95, 95, 95, 95};
student2.setStudentName("Steve Jones");
student2.setGrades(grades2);
student2.outputGrades();
Student student3 = new Student();
int[] grades3 = {105, -1, 72, 90, 88};
student3.setStudentName("Mary Smith");
student3.setGrades(grades3);
student3.outputGrades();
} // end method main
} // end class TestStudent
Student class:
public class Student {
private String studentName;
private int[] grades;
//constructor
public Student() {
}
public void setStudentName(String name) {
studentName = name;
} // end method setStudentName
public String getStudentName() {
return studentName;
} // end method getStudentName
public void setGrades(int gradeArray[]) {
grades = gradeArray;
for (int i = 0; i < gradeArray.length; i++) {
if (gradeArray[i] < 0 || gradeArray[i] > 100) {
gradeArray[i] = 0;
} // end if
} // end loop
} // end method setGrades
public int[] getGrades() {
return grades;
} // end method getGrades
public void outputGrades() {
int gradesTotal = 0;
int gradesAverage;
char letterGrade;
for (int i : grades) {
gradesTotal += i;
} // end loop
gradesAverage = gradesTotal / (grades.length);
if (gradesAverage >= 0 && gradesAverage <= 59) {
letterGrade = 'F';
} else if (gradesAverage >= 60 && gradesAverage <= 69) {
letterGrade = 'D';
} else if (gradesAverage >= 70 && gradesAverage <= 79) {
letterGrade = 'C';
} else if (gradesAverage >= 80 && gradesAverage <= 89) {
letterGrade = 'B';
} else {
letterGrade = 'A';
} // end if statement
switch (letterGrade) {
case 'A':
System.out.println(studentName + " gets an A.");
case 'B':
System.out.println(studentName + " gets an B.");
case 'C':
System.out.println(studentName + " gets an C.");
case 'D':
System.out.println(studentName + " gets an D.");
case 'F':
System.out.println(studentName + " gets an F.");
} // end switch
} // end method outputGrades
} // end class Student
Thanks for taking a look!
Ben
You forgot the break statements from your switch. If you don't break, it'll just keep executing everything until the end of the block!
This somewhat confusing "feature" is a leftover from C, and in most cases you'll want to end all your cases with a break, return or throw.