Reader does not stop in my program (java) - java

I am still fairly new to java and in my current class project we are trying to set students to classrooms, the students information is read from a text file which we create but my reader program does not stop after 4 lines (which is the end of the information of the student). It reads through all of them and prints them before analyzing what they have to be able to put them in the correct classRoom. Please remember I am still new to java.
Also not sure about an error I keep getting about Exception in thread "main" java.util.NoSuchElementException: No line found
this is my reader program
import java.io.*;
import java.util.Scanner;
public class Student{
private Scanner file;
public String name;
public String id;
public String year;
public String course;
//open the file to read
public void openFile(){
try{
file = new Scanner(new File("StudentFile.txt"));
}
catch(Exception e)
{
System.out.print("Could not Open File");
}
}
//Read the file and Assign the Student information strings
protected void readFileText(){
if(file.hasNext())
{
for(int n=0; n<4; n++)
{
name = file.nextLine();
id = file.nextLine();
year = file.nextLine();
course = file.nextLine();
System.out.print("\n" + name + "\n" + id + "\n" + year + "\n" + course + "\n");
}
}
else
{
name = null;
}
}
//close the file
public void closeFile(){
file.close();
}
//set information
public Student(){
this.name = name;
this.id = id;
this.year = year;
this.course = course;
}
//Get the informaton to main method
public String getName(){
return name;
}
public String getId(){
return id;
}
public String getYear(){
return year;
}
public String getCourse(){
return course;
}
}
This is my main method
public class Registrar {
public static void main(String[] args) {
Student student = new Student();
student.openFile();
String namenull = " ";
String classRoom[] = {"Washington W1000", "Washington W1100", "Washington W1200", "Washington W1300", "Washington W1400", "Washington W1500", "Kennedy K1000", "Kennedy K1100", "Kennedy K1200", "Kennedy K1300"};
int maxCapacity = 25;
int studentNum = 0;
int num = 0;
int n = 0;
String currentClass = classRoom[0];
while(namenull != null){ // to read the file and set students to a classroom.
student.readFileText();
String course = student.getCourse();
while (n < 10){
if(currentClass == "Washington W1400"){
maxCapacity = 30;
}
else if(currentClass == "Kennedy 1000"){
maxCapacity = 25;
}
else if(currentClass == "Kennedy 1200"){
maxCapacity = 35;
}
int num1 = 1;
int num2 = 2;
int num3 = 3;
if(studentNum < maxCapacity && course == "Comp 182" && num < 10){
System.out.print("\nRegistered in Comp 182, in classroom " + currentClass);
studentNum++;
}
else{
while(num == num1 || num == num2 || num == num3 || num == num){
num = num++;
}
}
if(studentNum < maxCapacity && course == "Comp 182 Lab" && num1 < 10){
currentClass = classRoom[num1];
System.out.print("\nRegistered in Comp 182 Lab, in classroom " + currentClass);
studentNum++;
}
else{
while(num1 == num || num1 == num2 || num1 == num3 || num1 == num1){
num1 = num1++;
}
}
if(studentNum < maxCapacity && course == "Comp 101" && num2 < 10){
currentClass = classRoom[num2];
System.out.print("\nRegistered in Comp 101, in classroom " + currentClass);
studentNum++;
}
else{
while(num2 == num || num2 == num1 || num2 == num3 || num2 == num2){
num2 = num2++;
}
}
if(studentNum < maxCapacity && course == "Comp 101 Lab" && num3 <10){
currentClass = classRoom[num3];
System.out.print("\nRegistered in Comp 101 Lab, in classroom " + currentClass);
studentNum++;
}
else{
while(num3 == num || num3 == num1 || num3 == num2 || num3 == num3){
num3 = num3++;
}
}
currentClass = classRoom[num];
}
namenull = student.getName();
}
student.closeFile();
}
}
As Requested, my StudentFile.txt
Alfredo Dominguez
1205586453
Freshman
Comp 182
Stacy Flores
6584527256
Sophmore
Comp 182 Lab

Scanner throws NoSuchElementException if no line was found. so wrap each file.nextLine(); with
if(file.hasNextLine()){
file.nextLine();
}

Related

Comparing Two Strings One Character at a Time in Java

I am trying to compare two different strings, one character at a time. I need to return the correct number of digits until they do not equal each other anymore. However, I can't include the character of '.' in the return statement. How would I go about doing this?
import java.util.Scanner;
import java.math.BigDecimal;
public class PiEstimate {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String a;
String b;
char y;
char c;
char d;
String userInput;
do {
System.out.print("Enter a number of randomly generated points:");
userInput = in.nextLine();
if (!isValid(userInput)) {
System.out.print("\n" + "You entered an invalid integer. Please enter a valid integer greater than 0: ");
userInput = in.nextLine();
BigDecimal estimate = new BigDecimal((Math.PI / 4) * 4);
estimate.toString();
System.out.println("\n" + "Your estimate is: " + calculation(userInput));
System.out.println("\n" + "Accuracy of digits is :" + comparison(estimate.toString(),userInput));
} else {
BigDecimal estimate = new BigDecimal((Math.PI / 4) * 4);
estimate.toString();
System.out.println("\n" + "Your estimate is: " + calculation(userInput));
System.out.println("\n" + "Accuracy of digits is :" + comparison(estimate.toString(),userInput));
}
System.out.println("\n" + "Would you like to play again? Enter 'Y' for yes or 'N' for no: ");
String optionToPlay = in.nextLine();
c = optionToPlay.charAt(0);
d = Character.toUpperCase(c);
if (d == 'n' || d == 'N') {
BigDecimal estimate2= new BigDecimal( (Math.PI / 4) * 4);
System.out.println("\n" + "The best estimate is: " + estimate2);
}
} while (d == 'Y');
} // end psvm
public static boolean isValid(String a) {
boolean isFlag = true;
char holder;
for (int i = 0; i < a.length(); i++) {
holder = a.charAt(i);
if (!Character.isDigit(a.charAt(i))) {
return false;
} if (i == 0 && holder == '-') {
return false;
}
} // end for
return isFlag;
} // end isValid
public static double calculation(String a) { // String a means 'looking for a string
double calc = Double.parseDouble(a);
int i;
double x;
double y;
double c = 0;
double runningCounter = 0;
double totalCounter;
for (i = 0; i < calc; i++) {
x = Math.random();
y = Math.random();
c = Math.sqrt((x * x) + (y * y));
if (c <= 1) {
runningCounter++;
}
} // end for
totalCounter = ((runningCounter / calc) * 4);
calc = totalCounter;
return calc;
}
public static int comparison (String bear, String userInput) {
int i = 0;
String s = calculation(userInput) + "";
int b;
int counter2 = 0;
for (i=0; i < s.length(); i++) {
if (s.charAt(i) != bear.charAt(i)) {
return i;
}
}
return i;
} // end comparison
} // end class
Code from IDE

Changing my program to use ArrayList

So I have been given a project in where I must validate ISBN-10 and ISBN-13 numbers. My issue is that I want to use an ArrayList based on what the user inputs(the user adds as many numbers as they want to the ArrayList). Here is my code (without an ArrayList). How can I modify this so that the user can put as many ISBN number as they want?
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String isbn;
//Get the ISBN
System.out.print("Enter an ISBN number ");
isbn = input.nextLine();
input.close();
//Strip out the spaces/System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");dashes by replacing with empty character.
isbn = isbn.replaceAll("( |-)", "");
//Check depending on length.
boolean isValid = false;
if(isbn.length()== 10){
isValid = CheckISBN10(isbn);
}else if (isbn.length()== 13){
isValid = CheckISBN13(isbn);
}else{
isValid = false;
}
//Print check Status
if(isValid){
System.out.println(isbn + " IS a valid ISBN");
}else{
System.out.println(isbn + " IS NOT a valid ISBN");
}
}
//Checking ISBN-10 numbers are valid
//
private static boolean CheckISBN10(String isbn){
int sum = 0;
String dStr;
for (int d = 0; d < 10; d++){
dStr = isbn.substring(d, d + 1);
if (d < 9 || dStr != "X"){
sum += Integer.parseInt(dStr) * (10-d);
}else {
sum += 10;
}
}
return (sum %11 == 10);
}
private static boolean CheckISBN13(String isbn){
int sum = 0;
int dVal;
for (int d = 0; d < 13; d++){
dVal = Integer.parseInt(isbn.substring(d, d + 1));
if (d % 2 == 0){
sum += dVal * 1;
}else {
sum += dVal * 3;
}
}
return (sum % 10 == 0);
}
}
public static List<String> scanNumberToListUntilAppears(String end) {
if(end == null || end.isEmpty())
end = "end";
List<String> numbers = new ArrayList<>();
String message = "Enter an ISBN number: ";
try (Scanner input = new Scanner(System.in)) {
System.out.print(message);
while(input.hasNext()) {
String isbn = input.nextLine();
if(isbn.equalsIgnoreCase(end)) {
if(!numbers.isEmpty())
break;
} else {
numbers.add(isbn);
if(numbers.size() == 1)
message = "Enter the next ISBN number or '" + end + "': ";
}
System.out.print(message);
}
}
return numbers;
}
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
String isbn;
String ans;
ArrayList<String> isbns = new ArrayList<String>();
// user will enter at least 1 ISBN
do{
//Get the ISBN
System.out.println("Enter an ISBN number ");
isbns.add(input.nextLine());
//loops till answer is yes or no
while(true){
System.out.println("Would you like to add another ISBN?");
ans = input.nextLine();
if(ans.equalsIgnoreCase("no"))
break;
else if (!(ans.equalsIgnoreCase("yes"))
System.out.println("Please say Yes or No");
}
}while(!(ans.equalsIgnoreCase("yes"));
input.close();
//Strip out the spaces/System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");System.out.println("Press 1 to enter a list of ISBN numbers to verify. ");dashes by replacing with empty character.
for(int i = 0; i<isbns.size(); i++)
isbns.set(i, isbns.get(i).replaceAll("( |-)", ""));
isbn = isbn.replaceAll("( |-)", "");
//Check depending on length.
boolean isValid = false;
for(String isbn : isbns){
if(isbn.length()== 10){
isValid = CheckISBN10(isbn);
print(isbn, isValid);
}else if (isbn.length()== 13){
isValid = CheckISBN13(isbn);
print(isbn, isValid);
}else{
isValid = false;
print(isbn, isValid);
}
}
public static void print(String isbn, boolean isValid){
if(isValid){
System.out.println(isbn + " IS a valid ISBN");
}else{ System.out.println(isbn + " IS NOT a valid ISBN");
}
}
//Checking ISBN-10 numbers are valid
private static boolean CheckISBN10(String isbn){
int sum = 0;
String dStr;
for (int d = 0; d < 10; d++){
dStr = isbn.substring(d, d + 1);
if (d < 9 || dStr != "X"){
sum += Integer.parseInt(dStr) * (10-d);
}else {
sum += 10;
}
}
return (sum %11 == 10);
}
private static boolean CheckISBN13(String isbn){
int sum = 0;
int dVal;
for (int d = 0; d < 13; d++){
dVal = Integer.parseInt(isbn.substring(d, d + 1));
if (d % 2 == 0){
sum += dVal * 1;
}else {
sum += dVal * 3;
}
}
return (sum % 10 == 0);
}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at lab3.Lab3.readTestScore(Lab3.java:93) at lab3.Lab3.main(Lab3.java:25)

package lab3;
import java.util.Scanner;
public class Lab3
{
static int n;
static double testScores[] = new double[n];
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int studentID;
System.out.println("Enter student ID: ");
studentID = input.nextInt();
System.out.println("Enter number of test scores");
n = input.nextInt();
readTestScore(n);
//double[] testarray = readTestScore(n);
//System.out.println("Test Scores are:" + testarray);
}
public char getLetterGrade(double score)
{
char letter;
if(score >= 90)
{
printComment('A');
letter = 'A';
}
if(score >= 80)
{
printComment('B');
letter = 'B';
}
if(score >= 70)
{
printComment('D');
letter = 'C';
}
else if(score >= 60)
{
printComment('D');
letter = 'D';
}
else if(score < 60)
printComment('F');
letter = 'F';
return letter;
}
static void printComment(char grade)
{
if(grade == 'A')
System.out.println("COMMMENT\t\t\t:\t" + "Very Good!");
if(grade == 'B')
System.out.println("COMMMENT\t\t\t:\t" + "Good!");
if(grade == 'C')
System.out.println("COMMMENT\t\t\t:\t" + "Satisfactory.");
if(grade == 'D')
System.out.println("COMMMENT\t\t\t:\t" + "Needs Improvement.");
if(grade == 'F')
System.out.println("COMMMENT\t\t\t:\t" + "Poor.");
}
static void printTestResults(double []testList)
{
for(int i = 0; i < testList.length; i++)
{
System.out.println("Test Score\t\t\t" + "Letter Grade\t\t\t" + "Comment\t\t\t");
System.out.println(testScores[i] + "\t\t\t" );//+ getLetterGrade(score) + "\t\t\t" + printComment(grade));
}
}
static double[] readTestScore(int size)
{
Scanner input = new Scanner(System.in);
int i =0;
System.out.println("Enter Test Scores:\t");
for(i = 0; i<n; i++)
{
testScores[i] = input.nextDouble();
}
testScores[i] /=n;
System.out.println("these are test scores: " + testScores);
return (testScores);
}
}
Because of this
static int n;
static double testScores[] = new double[n];
array is set to default size of int ie 0 giving you that error in the method

Array not printing properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why does the array "prevScore" not print the value "points"?
I want it to print out points for prevScore [0], and then null 0
This is the array, after the // is something I thought I could use.
int [] prevScore = new int[10]; //{ 0 };
String [] prevScoreName = new String[10]; //{"John Doe"};
public static int[] scoreChange (int prevScore[], int points)
{
for(int i = 9; i > 0; i--){
prevScore[i] = prevScore[i-1];
}
prevScore[0]= points;
return prevScore;
}
I dont know if the print of prevScore is needed.
//a method that prints high scores
public static void printScores (int prevScore[], String prevScoreName[])
{
for (int i = 10; i > 0; i--){
System.out.println(prevScore[i] + " " + prevScoreName[i]);
}
}
Here is the rest of my program I am working on... currently only i get one, 0 John Doe.
public class Main
{
static BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); // user input
public static void main (String args[]) throws IOException
{
int press = 0;
do {
int menuchoice = 0;
int [] prevScore = new int[] { 0 };
String [] prevScoreName = new String[] {"John Doe"};
System.out.println("Menu choice: 1 to start game, 2 print instructions,"
+ "3 prev score");
Scanner input = new Scanner(System.in);
int userinput = Integer.parseInt(input.next());
int points;
menuchoice = userinput;
if (menuchoice == 1){
points = startGame();
String newName = endGame(points);
prevScore = scoreChange(prevScore,points);
prevScoreName = nameChange(prevScoreName, newName);
}
if (menuchoice == 2){
printInstructions();
}
if(menuchoice == 3) {
printScores(prevScore, prevScoreName); }
if (menuchoice != 1 && menuchoice != 2 && menuchoice !=3 ) {
System.out.println("ERROR"); }
} while (press !=4 );
}
//a method that initializes a new game
public static int startGame () throws IOException //a method that initializes a new game
{
int lives = 3;
int points = 0;
System.out.println("Good Luck!");
do {
System.out.println("Points: " + points);
System.out.println("Lives: " + lives);
int correct = displayNewQuestion();
Scanner userinput = new Scanner(System.in);
int userAnswer = Integer.parseInt(userinput.nextLine());
if (userAnswer == correct){
points ++;
System.out.println("Correct");
}
if (userAnswer != correct ){
lives --;
System.out.println("Incorrect");
}
} while (lives > 0);
return points;
}
public static String endGame (int points) throws IOException // a method that tells the user the game is over
{
System.out.println("GAME OVER");
Scanner nameinput = new Scanner(System.in);
System.out.println("Please enter your name for the score charts!");
String newName = nameinput.next();
return newName;
}
public static int[] scoreChange (int prevScore[], int points)
{
// for(int i = 0; i < 10; i--){
// prevScore[i] = prevScore[i-1];
// }
// prevScore[1]= prevScore[0];
prevScore[0]= points;
return prevScore;
}
public static String[] nameChange (String prevScoreName[], String newName)
{
/*for(int i = 0; i < 10; i++){
prevScoreName[i] = prevScoreName[i-1];
}
//prevScoreName[1] = prevScoreName[0];*/
prevScoreName[0] = newName;
return prevScoreName;
}
public static void printInstructions () //a method that will print the instructions to the user
{
System.out.println("Instructions");
}
public static void printScores (int prevScore[], String prevScoreName[]) //a method that prints high scores
{
/* for (int i = 0; i < 10; i--){
System.out.println(prevScore[i] + " " + prevScoreName[i]);
}*/
for (int i = prevScore.length; i > 0; i--){
System.out.println(prevScore[i-1] + " " + prevScoreName[i-1]);
}
}
public static int displayNewQuestion () // a method that displays a random arithmetic question
{
int correctAnswer = 0;
int num1 = randInt (12,-12);
int num2 = randInt(12, -12);
Random rand = new Random();
int operator = rand.nextInt((4 - 1) + 1) + 1;
if (operator == 1)
{
System.out.println(num1 + " + " + num2);
correctAnswer = num1 + num2;
}
if (operator == 2)
{
System.out.println(num1 + " - " + num2);
correctAnswer= num1 - num2;
}
if (operator == 3)
{
System.out.println(num1 + " x " + num2);
correctAnswer= num1*num2;
}
if (operator == 4)
{
if (num2 == 0) {
System.out.println(num1*num2 + " / " + num1);
correctAnswer= ((num1*num2)/num1);
}
if (num2 != 0) {
System.out.println(num1*num2 + " / " + num2);
correctAnswer= ((num1*num2)/num2);
}
}
return correctAnswer;
}
public static int randInt(int max , int min) {
Random rand = new Random();
min = -12;
max = 12;
int randnum = rand.nextInt((max - min) + 1) + min;
return randnum;
}
}
Use this loop:
for (int i = prevScore.length; i > 0; i--){
System.out.println(prevScore[i-1] + " " + prevScoreName[i-1]);
}
I think it should solve your problem.
Update
based on your updated program. Move the following code above the start of the 'do' loop.
int [] prevScore = new int[] { 0 };
String [] prevScoreName = new String[] {"John Doe"};
That is you are moving these lines out of the loop. It should work now.
That is the start of your 'main' method should look something like this:
public static void main(String args[]) throws IOException {
int press = 0;
int[] prevScore = new int[] { 0 };
String[] prevScoreName = new String[] { "John Doe" };
do {
int menuchoice = 0;
System.out.println("Menu choice: 1 to start game, 2 print instructions," + "3 prev score");
Your printScore() method is trying to access element [10] of an array whose index range is 0 - 9, and is never accessing element [0]. You may want to print the most recent score first:
for (int i = 0; i < 10; i++) {
Or conversely, to print the most recent score last:
for (int i = 9; i >= 0; i--) {
Thank you so much! It Works! The only problem still is that the scorelist prints backwards.
public class Main
{
static BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); // user input
public static void main (String args[]) throws IOException
{
int press = 0;
int[] prevScore = new int[10];
String[] prevScoreName = new String[10];
do {
int menuchoice = 0;
System.out.println("Menu choice: 1 to start game, 2 print instructions,"
+ "3 prev score");
Scanner input = new Scanner(System.in);
int userinput = Integer.parseInt(input.next());
int points;
menuchoice = userinput;
if (menuchoice == 1) {
points = startGame();
String newName = endGame(points);
prevScore = scoreChange(prevScore,points);
prevScoreName = nameChange(prevScoreName, newName);
}
if (menuchoice == 2) {
printInstructions();
}
if(menuchoice == 3) {
printScores(prevScore, prevScoreName);
}
if (menuchoice != 1 && menuchoice != 2 && menuchoice !=3 ) {
System.out.println("ERROR");
}
} while (press !=4 );
}
//a method that initializes a new game
public static int startGame () throws IOException //a method that initializes a new game
{
int lives = 3;
int points = 0;
System.out.println("Good Luck!");
do {
System.out.println("Points: " + points);
System.out.println("Lives: " + lives);
int correct = displayNewQuestion();
Scanner userinput = new Scanner(System.in);
int userAnswer = Integer.parseInt(userinput.nextLine());
if (userAnswer == correct) {
points ++;
System.out.println("Correct");
}
if (userAnswer != correct ) {
lives --;
System.out.println("Incorrect");
}
} while (lives > 0);
return points;
}
public static String endGame (int points) throws IOException // a method that tells the user the game is over
{
System.out.println("GAME OVER");
Scanner nameinput = new Scanner(System.in);
System.out.println("Please enter your name for the score charts!");
String newName = nameinput.next();
return newName;
}
public static int[] scoreChange (int prevScore[], int points)
{
// for(int i = 0; i < 10; i--){
// prevScore[i] = prevScore[i-1];
// }
// prevScore[1]= prevScore[0];
prevScore[0]= points;
return prevScore;
}
public static String[] nameChange (String prevScoreName[], String newName)
{
/*for(int i = 0; i < 10; i++){
prevScoreName[i] = prevScoreName[i-1];
}
//prevScoreName[1] = prevScoreName[0];*/
prevScoreName[0] = newName;
return prevScoreName;
}
public static void printInstructions () //a method that will print the instructions to the user
{
System.out.println("Instructions");
}
public static void printScores (int prevScore[], String prevScoreName[]) //a method that prints high scores
{
/* for (int i = 0; i < 10; i--){
System.out.println(prevScore[i] + " " + prevScoreName[i]);
}*/
System.out.println("Scores: ");
for (int i = prevScore.length; i > 0; i--){
System.out.println(prevScore[i-1] + " " + prevScoreName[i-1]);
}
}
public static int displayNewQuestion () // a method that displays a random arithmetic question
{
int correctAnswer = 0;
int num1 = randInt (12,-12);
int num2 = randInt(12, -12);
Random rand = new Random();
int operator = rand.nextInt((4 - 1) + 1) + 1;
if (operator == 1)
{
System.out.println(num1 + " + " + num2);
correctAnswer = num1 + num2;
}
if (operator == 2)
{
System.out.println(num1 + " - " + num2);
correctAnswer= num1 - num2;
}
if (operator == 3)
{
System.out.println(num1 + " x " + num2);
correctAnswer= num1*num2;
}
if (operator == 4)
{
if (num2 == 0) {
System.out.println(num1*num2 + " / " + num1);
correctAnswer= ((num1*num2)/num1);
}
if (num2 != 0) {
System.out.println(num1*num2 + " / " + num2);
correctAnswer= ((num1*num2)/num2);
}
}
return correctAnswer;
}
public static int randInt(int max , int min) {
Random rand = new Random();
min = -12;
max = 12;
int randnum = rand.nextInt((max - min) + 1) + min;
return randnum;
}
}

Finding the two highest scores using file input

Since we haven't covered arrays and lists yet, he said to stick to loops and if statements.
I can't seem to figure out how to make it display the names of the two highest scores from the text file. Also I can't figure out how to make it display two scores that are identical. Here is what I've done so far:
package lab06;
import java.util.*;
import java.io.*;
public class Lab06 {
public static void main(String[] args) throws Exception {
Scanner lab06txt = new Scanner(new File("Lab06.txt"));
Scanner duplicateScanner = new Scanner(new File("Lab06.txt"));
int totalstudents = 0;
int grade = 0;
int grade2 = 0;
int record = 0;
int Highest = 0;
int Highest2 = 0;
int ACounter = 0;
int BCounter = 0;
int CCounter = 0;
int DCounter = 0;
int FCounter = 0;
double average = 0;
String lastName = "";
String lastNameHigh = "";
String lastNameHigh2 = "";
String firstName = "";
String firstNameHigh = "";
String firstNameHigh2 = "";
while (lab06txt.hasNext()){
record ++;
totalstudents++;
lastName = lab06txt.next();
firstName = lab06txt.next();
grade = lab06txt.nextInt();
{
average += grade;
if (grade >= Highest){
Highest = grade;
firstNameHigh = firstName;
lastNameHigh = lastName;
}
}
{
if ((grade >= 90) && (grade <= 100))
{
ACounter++;
}
if ((grade >= 80) && (grade <= 89))
{
BCounter++;
}
if ((grade >= 70) && (grade <= 79))
{
CCounter++;
}
if ((grade >= 60) && (grade <= 69))
{
DCounter++;
}
if ((grade < 60))
{
FCounter++;
}
if ((grade < 0) || (grade > 100))
{
System.out.print("Score is out of bounds in record " + record + ": " + lastName + " "+ firstName + " " + grade + ".\nProgram ending\n");
return;
}
}
}
while(lab06txt.hasNext())
{
lastName = lab06txt.next();
firstName = lab06txt.next();
grade2 = lab06txt.nextInt();
if(grade2 > Highest2 && grade2 < Highest){
Highest2 = grade2;
firstNameHigh2 = firstName;
lastNameHigh2 = lastName;
}
}
System.out.println("Total students in class: " +totalstudents);
System.out.println("Class average: " + average/totalstudents);
System.out.println("Grade Counters: ");
System.out.println("A's B's C's D's F's");
System.out.printf(ACounter + "%7d %7d %8d %7d\n", BCounter,CCounter,DCounter,FCounter);
System.out.println("");
System.out.println("Top Two Students: \n");
System.out.printf(lastNameHigh + " " + firstNameHigh + "%15d \n", Highest);
System.out.printf(lastNameHigh2 + " " + firstNameHigh2 + "%15d\n", Highest2);
}
}
The two parts of your code where you have:
firstName = firstNameHigh;
lastName = lastNameHigh;
should be:
firstNameHigh = firstName;
lastNameHigh = lastName;

Categories

Resources