This program is supposed to find the maximum, minimum, and average of grades. User inputs int inputGrade and the program displays letter it is. It's supposed to do this how however many students are needed. I'm having trouble writing the method where it finds the max and min. (yes I've talked to my teacher if anyone's wondering...) I pasted the methods below (they don't work). Just like IN GENERAL, does anyone know how to find the maximum and minimum of a set of entered numbers? (not using arrays, lists, or any unusual imports other than scanner) ** note I've updated this a lot...
import java.util.Scanner;
public class GetLetterGrade
{
static int inputGrade; // input grade
public static void main(String [] args)
{
Scanner reader = new Scanner(System.in);
int classAverage;
int classMin; // class's minimum grade
int classMax; // class's maximum grade
while (inputGrade != -1) // while user is entering grades
{
System.out.println("Welcome to the grade calculator. \nPlease enter a
numeric grade. After the last student in the class, enter a grade of
-1.");
inputGrade = reader.nextInt();
letterGrade(inputGrade); // calls letter grade method
findMaxAndMin();
result();
}
}
// find letter grade
public static String letterGrade(int numGrade)
{
String gradeMessage = "";
{
if (numGrade >= 96 && numGrade <= 100) // if numeric grade is 96-100 then
it's A+
{
gradeMessage = "That's an A+.";
result();
// DOES THIS FOR GRADES A+ TO F, NOT SHOWN, too much to paste!
}
}
}
return gradeMessage;
}
public static int findCharGrade(int numGrade)
{
char letter;
if (numGrade >= 90 && numGrade <= 100) // A
{
letter = 'A';
}
else if (numGrade >= 80 && numGrade < 90) // B
{
letter = 'B';
}
else if (numGrade >= 70 && numGrade < 80) // C
{
letter = 'C';
}
else if (numGrade >= 60 && numGrade < 70) // D
{
letter = 'D';
}
else if (numGrade < 60) // F
{
letter = 'F';
}
}
// finds maximum and minimum grades
public static int findMaxAndMin(int inputGrade)
{
int max = Math.max(inputGrade, max);
int min = Math.min(inputGrade, min);
if (inputGrade < max)
{
inputGrade = max;
findCharGrade(inputGrade);
}
else if (inputGrade > min)
{
inputGrade = min;
findCharGrade(inputGrade);
}
}
public static void calcAverage(int sumOfGrades, int numOfStudents)
{
// something goes here
}
// finds results
public static void result()
{
int min = findMaxAndMin(inputGrade);
int max = findMaxAndMin(inputGrade);
System.out.println("Please enter a numeric grade");
int inputGrade = reader.nextInt();
letterGrade(inputGrade);
if (inputGrade == -1)
{
System.out.println("You entered " + numOfStudents + " students. Class
Average: " + average + " Class Minimum: " + min + " Class maximum: " + max
+ " \nThanks for using the class grade calculator!");
}
}
here is a more simplistic way of doing it not using Lists or arrays
double sum = 0; // use double so that you do not do integer arithmetic
int count = 0;
int min = Integer.MAX_VALUE; // set to very high value
int max = Integer.MIN_VALUE; // set to bery low value
Scanner scan1 = new Scanner(System.in);
System.out.println("enter numbers (-1 to quit");
while (scan1.hasNextInt()) {
int i = scan1.nextInt(); // get the number (assuming only int value)
if (i == -1) break;
min = Math.min(i, min);
max = Math.max(i, max);
sum += i;
count++;
}
if (count > 0) {
System.out.println("min " + min);
System.out.println("max " + max);
System.out.println("avg " + sum / count);
}
disclaimer
This code will not handle wrong type of input e.g. Strings
edit
If you want the average to be calculated in a separate method you can have a method like
double calcAvg (double sum, int count) {
return sum / count;
}
this can then be called as
if (count > 0) {
System.out.println("min " + min);
System.out.println("max " + max);
System.out.println("avg " + calcAvg (sum, count));
}
You can (and should) divide your problem into the smaller methods.
I'll drop the code, and you read and study it.
I admit I haven't pay to much attention of this simple quest, but still...
Here you are:
import java.util.List;
public class Answer {
public static void main(String[] args) {
//test with some grades (integers)
Answer answer = new Answer();
List<Integer> someGrades = List.of(12, 66, 34, 96, 3, 77, 2);
System.out.println("max = " + answer.findMaxGrade(someGrades));
System.out.println("min = " + answer.findMinGrade(someGrades));
System.out.println("avg = " + answer.findAverageGrade(someGrades));
}
private int findMaxGrade(List<Integer> grades) {
int max = Integer.MIN_VALUE;
for (int grade : grades) {
if (grade > max) max = grade;
}
return max;
}
private int findMinGrade(List<Integer> grades) {
int min = Integer.MAX_VALUE;
for (int grade : grades) {
if (grade < min) min = grade;
}
return min;
}
private double findAverageGrade(List<Integer> grades) {
double average = 0;
for (int grade : grades) {
average += grade;
}
return average / grades.size();
}
}
package example;
import java.util.Scanner;
class Example {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int m = 1, total = 0, max = 0, min = 100;
double avg = 0;
while (m <= 5) {
System.out.print("Input marks " + m + " = ");
int inp = r.nextInt();
total += inp;
m++;
min=min<inp?min:inp;
max=max<inp?inp:max;
}
avg = (double)(total) / 5;
System.out.println("Total : " + total);
System.out.println("Max : " + max);
System.out.println("Min : " + min);
System.out.println("Average : " + avg);
}
}
Related
public static void main(String[] args) {
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
String message = "Welcome to Simple Gradebook!";
promptForInt(message, minValue, maxValue);
// Declaring variables for the loop & the sentinel variable
int score = 0;
boolean doneYet = false;
do {
// Input Validation
if (score < minValue || score > maxValue) {
System.err.printf(
"Invalid value. The acceptable range is" + " between %d and %d\n" + "Please try again\n",
minValue, maxValue);
score = promptForInt(message, minValue, maxValue);
} else {
doneYet = true;
}
} while (doneYet == false);
}
public static int promptForInt(String message, int minValue, int maxValue) {
// Declaring variables for the loop & the sentinel variable
int sum = 0;
int numStudents = 0;
int score = 0;
System.out.println(message);
// Creating the sentinel loop
do {
System.out.printf("Enter the score for student #%d" + "(or -1 to quit): ", numStudents);
Scanner keyboard = new Scanner(System.in);
score = Integer.parseInt(keyboard.nextLine());
if (score != -1) {
sum += score;
numStudents += 1;
}
} while (score != -1);
double avgScore = (double) sum / numStudents;
// Passing method to this method to convert grade to letter
convertToLetter(avgScore);
System.out.println("The average score is: " + avgScore + " which equates to a " + avgScore);
return 0;
}
How can I get my input validation to work so anything outside range -1 - 100 will display the error message? I want to use the "do-while" loop and thought I was doing it all correctly. If a user enters a value outside the defined range, it should display the error message and prompt again for the score. What am I missing?
A good place to do the validation is inside the method, promptForInt.
Since there is no use of the value returned from method, promptForInt, it's better to declare it as void.
Do not instantiate the Scanner object inside the loop.
The following code has incorporated all these comments:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Defining the constants for min and max range
final int minValue = -1;
final int maxValue = 100;
String message = "Welcome to Simple Gradebook!";
promptForInt(message, minValue, maxValue);
}
public static void promptForInt(String message, int minValue, int maxValue) {
// Declaring variables for the loop & the sentinel variable
int sum = 0;
int numStudents = 0;
int score = 0;
boolean valid;
Scanner keyboard = new Scanner(System.in);
System.out.println(message);
// Loop to continue getting score for students until -1 is entered to quit
do {
System.out.printf("Enter the score for student #%d" + "(or -1 to quit): ", numStudents);
// Loop for getting input of score for the current student
do {
valid = true;
score = Integer.parseInt(keyboard.nextLine());
// Input Validation
if (score < minValue || score > maxValue) {
System.err.printf(
"Invalid value. The acceptable range is" + " between %d and %d\n" + "Please try again\n",
minValue, maxValue);
valid = false;
}
} while (!valid);
if (score != -1) {
sum += score;
numStudents += 1;
}
} while (score != -1);
double avgScore = (double) sum / numStudents;
// Passing method to this method to convert grade to letter
convertToLetter(avgScore);
System.out.println("The average score is: " + avgScore + " which equates to a " + convertToLetter(avgScore));
}
public static char convertToLetter(double avg) {
char gradeLetter;
// Identifying the ranges for the grade letter
if (avg >= 90) {
gradeLetter = 'A';
} else if (avg >= 80) {
gradeLetter = 'B';
} else if (avg >= 70) {
gradeLetter = 'C';
} else if (avg >= 60) {
gradeLetter = 'D';
} else {
gradeLetter = 'F';
}
return gradeLetter;
}
}
A sample run:
Welcome to Simple Gradebook!
Enter the score for student #0(or -1 to quit): -2
Invalid value. The acceptable range is between -1 and 100
Please try again
-4
Invalid value. The acceptable range is between -1 and 100
Please try again
45
Enter the score for student #1(or -1 to quit): 50
Enter the score for student #2(or -1 to quit): -1
The average score is: 47.5 which equates to a F
import java.util.Scanner;
public class BA4 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello Drews, how many total grades do you want to process?");
int numberOfGrades = keyboard.nextInt();
int[] storeGrades = new int[numberOfGrades];
for (int i = 0; i < numberOfGrades; i++) {
System.out.println("Please enter grade " + (i + 1) + ": ");
storeGrades[i] = keyboard.nextInt();
}
System.out.println("Total score is: " + (getTotalScore(storeGrades)));
System.out.println("Lowest score is: " + (getLowestScore(storeGrades)));
System.out.println("Highest score is: " + (getHighestScore(storeGrades)));
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
}
public static int getTotalScore(int[] storeGrades) {
int sum = 0;
for (int i = 0; i < storeGrades.length; i++) {
sum += storeGrades[i];
}
return sum;
}
public static int getLowestScore(int[] storeGrades) {
int getLowestScore = 0;
for (int i = 0; i > storeGrades.length; i++) {
getLowestScore = storeGrades[i];
}
return getLowestScore;
}
public static int getHighestScore(int[] storeGrades) {
int getHighestScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
getHighestScore = storeGrades[i];
}
return getHighestScore;
}
public static double averageScore(double[] storeGrades) {
double averageScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
averageScore = (double) storeGrades[i];
}
return averageScore;
}
public static int printGrade(int[] storeGrades) {
int printGrade;
if (printGrade > 89) {
String gradeSoFar = "A";
System.out.println("Your grade so far is an " + gradeSoFar);
}
else if ((printGrade > 79) && (printGrade < 90)) {
String gradeSoFar = "B";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 69) && (printGrade < 80)) {
String gradeSoFar = "C";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 59) && (printGrade < 70)) {
String gradeSoFar = "D";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 0) && (printGrade < 60)) {
String gradeSoFar = "F";
System.out.println("Your grade so far is an " + gradeSoFar);
}
return printGrade;
}
}
I am trying to figure out where I am going wrong. I have a couple of errors which leads me to believe I really just don't understand methods as well as I thought I did.
The goal is to create 5 methods displaying to the user the total, lowest, highest and average scores, and then to print the letter grade. Thank you for your assistance to this noobie java coder! :)
You are passing in a String when you should be passing in a Double[] into averageScore function in this line:
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
and you did not initialize the printGrade variable inside the printGrade function, you need to give it an initial value if you are going to use it in a comparison.
That's all the errors that
I did everything as far as concepts. I made my class, and my client class. The assignment is to make a program that allows the user to input 10 grades into a gradebook, and get the max, min, and average grade of class.
My only problem is I want to make sure the user cannot put anything in the program that is not an integer; do I put instructions like that in my class or client java doc?
This is my class:
import java.util.Arrays;
public class ExamBook{
int grades[];
int classSize;
int MIN = 0;
int MAX = 100;
public ExamBook(int[] gradeBook)
{
classSize = 10;
//instantiate array with same length as parameter
grades = new int[gradeBook.length];
for ( int i = 0; i <= gradeBook.length-1; i++ )
{
grades[i] = gradeBook[i];
}
Arrays.sort(grades);
}
//setter, or mutator
public void setClassSize( int newClass )
{
classSize = newClass;
}
//get return method
public int getClassSize()
{
return classSize;
}
//calculate highest grade
public int calculateMaxGrade()
{
int max = grades[0]; //assuming that the first index is the highest grade
for ( int i = 0; i <= grades.length - 1; i++ )
{
if ( grades[i] > max )
max = grades[i]; //save the new maximum
}
return max;
}
//calculate lowest grade
public int calculateMinGrade()
{
int min = grades[0]; //assuming that the first element is the lowest grade
for ( int i = 0; i <= grades.length - 1; i++ )
{
if ( grades[i] < min)
min = grades[i]; //save the new minimum
}
return min;
}
//calculate average
public double calculateAverageGrades()
{
double total = 0;
double average = 0;
for ( int i = 0; i < grades.length; i++ )
{
total += grades[i];
}
average = total/grades.length;
return average;
}
//return an assorted array
public int[] assortedGrades()
{
Arrays.sort(grades);
return grades;
}
//return printable version of grades
#Override
public String toString()
{
String returnString = "The assorted grades of the class in ascending order is this: " + "\t";
for ( int i = 0; i <= grades.length - 1; i++ )
{
returnString += grades[i] + "\t";
}
returnString += " \nThe class average is a/an " + calculateAverageGrades() + "." + "\nThe highest grade in the class is " + calculateMaxGrade() + "." + "\nThe lowest grade in the class is " + calculateMinGrade() + ".";
returnString += "\n";
return returnString;
}
}
**This is my client:**
import java.util.Scanner;
import java.util.Arrays;
public class ExamBookClient
{
public static ExamBook classRoom1;
public static void main( String[] args)
{
int MAX = 100;
int MIN = 0;
Scanner scan = new Scanner(System.in);
//create array for testing class
int[] grading = new int [10];
System.out.println("Please enter 10 grades to go into the exam book.");
if(scan.hasNextInt())
{
for (int i = 0; i < grading.length; i++)
{
int x = scan.nextInt();
if( x>MIN && x<MAX)
{
grading[i] = x;
}
}
}
classRoom1 = new ExamBook (grading);
System.out.println("The classroom size is " + classRoom1.getClassSize() + "."
+ "\n" + classRoom1.toString() + ".");
}
}
Prompt for scan.hasNextInt() in your for loop of your client instead of outside the for loop. Like this:
boolean failed = false;
for (int i = 0; i < grading.length; i++)
{
if (failed)
scan.nextLine();
failed = false;
if (scan.hasNextInt()) {
int x = scan.nextInt();
if(x >= MIN && x <= MAX)
{
grading[i] = x;
} else {
System.out.println("Grade must be from 0-100!");
i--;
continue;
}
} else {
// jump back to the start of this iteration of the loop and re-prompt
i--;
System.out.println("Number must be an int!");
failed = true;
continue;
}
}
You might want to do this in two parts - your API should specify that it works with only integers - perhaps the method which processes the grades will accept Integer arguments only. The parser of the String can specify in its Javadocs what it does when the argument passed to it is not an integer. You client should also validate that the input is an integer (maybe within the valid range). If the user input is incorrect, then maybe it can display a usage manual.
You can check using the below code. If you pass other than number it would throw NumberFormatException
public static boolean checkIfNumber(String input) {
try {
Integer in = new Integer(input);
} catch (NumberFormatException e) {
return false;
}
return true;
}
You can change this part as follows. This way the user can enter non-integers but in those cases you will print out warnings and you will ignore them.
System.out.println("Please enter 10 grades to go into the exam book.");
int i = 0;
int x = -1;
while (scan.hasNext() && i < 9) {
String sx = scan.next();
try {
x = Integer.parseInt(sx);
i++;
if (x > MIN && x < MAX) {
grading[i] = x;
}
} catch (NumberFormatException e) {
System.out.println("Not an integer.");
}
}
classRoom1 = new ExamBook(grading);
Chech this link, it has the solution.
You must use the method hasNextInt() of Scanner.
If you do not want to use exceptions you can always use a Regex match to check that what you have in the string is a number valid for you.
Bearing in mind that your valid numbers are between 0 and 100, and 0 and 100 are not included seeing you code, the reg ex will be:
s.matches("[1-9][0-9]{0,1}")
Basically what this means is that you are going to have a character that is a number between 1 and 9 as first char, and then you could have one between 0 and 9, this way you do not allow 0 at the beginning (01 is not valid) and 0 by it self is also not valid. 100 has 3 chars so is not valid neither.
Below is what I have so far:
I don't know how to exclude 0 as a min number though. The assignment asks for 0 to be the exit number so I need to have the lowest number other than 0 appear in the min string. Any ideas?
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
System.out.print("Enter a Value: ");
val = s.nextInt();
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
};
System.out.println("Min: " + min);
System.out.println("Max: " + max);
Here's a possible solution:
public class NumInput {
public static void main(String [] args) {
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
Scanner s = new Scanner(System.in);
while (true) {
System.out.print("Enter a Value: ");
int val = s.nextInt();
if (val == 0) {
break;
}
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
}
System.out.println("min: " + min);
System.out.println("max: " + max);
}
}
(not sure about using int or double thought)
You just need to keep track of a max value like this:
int maxValue = 0;
Then as you iterate through the numbers, keep setting the maxValue to the next value if it is greater than the maxValue:
if (value > maxValue) {
maxValue = value;
}
Repeat in the opposite direction for minValue.
It is better
public class Main {
public static void main(String[] args) {
System.out.print("Enter numbers: ");
Scanner input = new Scanner(System.in);
double max = Double.MIN_VALUE;
double min = Double.MAX_VALUE;
while (true) {
if ( !input.hasNextDouble())
break;
Double num = input.nextDouble();
min = Math.min(min, num);
max = Math.max(max, num);
}
System.out.println("Max is: " + max);
System.out.println("Min is: " + min);
}
}
//for excluding zero
public class SmallestInt {
public static void main(String[] args) {
Scanner input= new Scanner(System.in);
System.out.println("enter number");
int val=input.nextInt();
int min=val;
//String notNull;
while(input.hasNextInt()==true)
{
val=input.nextInt();
if(val<min)
min=val;
}
System.out.println("min is: "+min);
}
}
This is what I did and it works try and play around with it. It calculates total,avarage,minimum and maximum.
public static void main(String[] args) {
int[] score= {56,90,89,99,59,67};
double avg;
int sum=0;
int maxValue=0;
int minValue=100;
for(int i=0;i<6;i++){
sum=sum+score[i];
if(score[i]<minValue){
minValue=score[i];
}
if(score[i]>maxValue){
maxValue=score[i];
}
}
avg=sum/6.0;
System.out.print("Max: "+maxValue+"," +" Min: "+minValue+","+" Avarage: "+avg+","+" Sum: "+sum);}
}
here you need to skip int 0
like following:
val = s.nextInt();
if ((val < min) && (val!=0)) {
min = val;
}
System.out.print("Enter a Value: ");
val = s.nextInt();
This line is placed in last.The whole code is as follows:-
public static void main(String[] args){
int min, max;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Value: ");
int val = s.nextInt();
min = max = val;
while (val != 0) {
if (val < min) {
min = val;
}
if (val > max) {
max = val;
}
System.out.print("Enter a Value: ");
val = s.nextInt();
}
System.out.println("Min: " + min);
System.out.println("Max: " + max);
}
I tried to optimize solution by handling user input exceptions.
public class Solution {
private static Integer TERMINATION_VALUE = 0;
public static void main(String[] args) {
Integer value = null;
Integer minimum = Integer.MAX_VALUE;
Integer maximum = Integer.MIN_VALUE;
Scanner scanner = new Scanner(System.in);
while (value != TERMINATION_VALUE) {
Boolean inputValid = Boolean.TRUE;
try {
System.out.print("Enter a value: ");
value = scanner.nextInt();
} catch (InputMismatchException e) {
System.out.println("Value must be greater or equal to " + Integer.MIN_VALUE + " and less or equals to " + Integer.MAX_VALUE );
inputValid = Boolean.FALSE;
scanner.next();
}
if(Boolean.TRUE.equals(inputValid)){
minimum = Math.min(minimum, value);
maximum = Math.max(maximum, value);
}
}
if(TERMINATION_VALUE.equals(minimum) || TERMINATION_VALUE.equals(maximum)){
System.out.println("There is not any valid input.");
} else{
System.out.println("Minimum: " + minimum);
System.out.println("Maximum: " + maximum);
}
scanner.close();
}
}
You are to write a program to compute statistics on a list of exam scores, the Exam Statistics Program (ESP). The input is the name of a text file that contains the number of scores followed by the list of scores. (See the example below.) You should then display the following information:
a. The number of scores
b. The minimum, maximum, and average scores
c. The number of As, Bs, Cs, Ds, and Fs, using a 90-80-70-60 scale.
My problem is: I don't know how to have variable grade take numbers.
When I run this program, it gives me this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at IntegersFromFile.main(IntegersFromFile.java:60)
Here is my code:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class IntegersFromFile
{
public static void main(String[] args)
{
Scanner file = null;
int grade = 0;
int min = 100;
int max = 0;
ArrayList<Integer> grades = new ArrayList<Integer>();
int a, b, c, d, f;
a = 0;
b = 0;
c = 0;
d = 0;
f = 0;
double aA, aB, aC, aD, aF;
aA = 0;
aB = 0;
aC = 0;
aD = 0;
aF = 0;
System.out.println("***Welcome to the Exam Statistics Program!!***");
System.out.println("Please enter the name of your data file:");
Scanner input = new Scanner(System.in);
String file_name = input.nextLine();
try
{
file = new Scanner(new File(file_name));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
while (file.hasNext())
{
if (file.hasNextInt())
{
grades.add(file.nextInt());
}
else
file.next();
}
do
{
grade = file.nextInt();
if (grade >= 0)
{
grades.add(grade);
if (grade < 60)
{
f++;
aF += grade;
}
else if (grade < 70)
{
d++;
aD += grade;
}
else if (grade < 80)
{
c++;
aC += grade;
}
else if (grade < 90)
{
b++;
aB += grade;
}
else
{
a++;
aA += grade;
}
if (grade < min)
min = grade;
if (grade > max)
max = grade;
}
}
while (grade >= 0);
if (aA > 0)
aA /= a;
if (aB > 0)
aB /= b;
if (aC > 0)
aC /= c;
if (aD > 0)
aD /= d;
if (aF > 0)
aF /= f;
double avg = 0;
for (int i = 0; i < grades.size(); i++) {
avg += grades.get(i);
}
avg /= grades.size();
System.out.println("Total Number of Scores: " + grades.size());
System.out.println("Total As: " + a);
System.out.println("Total Bs: " + b);
System.out.println("Total Cs: " + c);
System.out.println("Total Ds: " + d);
System.out.println("Total Fs: " + f);
System.out.println("Average A: " + aA);
System.out.println("Average B: " + aB);
System.out.println("Average C: " + aC);
System.out.println("Average D: " + aD);
System.out.println("Average F: " + aF);
System.out.println("The highest grade was " + max);
System.out.println("The lowest grade was " + min);
System.out.println("The average grade was " + avg);
}
}
The problem is that when the execution of the program reaches line 60, the input for the scanner is "exhausted". You should re-initialize the file variable (recommend you to rename it to scanner or similar) just before beginning with the do- cycle.
Not quite sure of the logic of the application, especially whether the do- cycle should be inside the while- cycle or not, but I would recommend you to use brackets even if the control structure has only one statement. This you will outline your intention better and will reduce the possibility for errors which are hard to find.