Ok so I'm supposed to be reading and printing from a text file with the code yet every time I run I get a "java.utilNoSuchElementException" on line 31 "grade = in.nextInt();". The current text file is
2 80 97
5 69 79 89 99 58
7 60 70 80 90 100 0 59
where the first number is the number of scores in each section, each section is supposed to be counted (ie. 1, 2, 3) Anyways one problem at a time. here's the current code.
import java.util.Scanner;
import java.io.*;
public class Prog2
{
public static void main (String args []) throws IOException
{
Scanner in = new Scanner (new File ("test1.txt"));
int Lowest, Highest, grade = 0;
float section_average, class_average;
int count = 0, A = 0, B = 0, C = 0, D = 0, F = 0, total_number_of_sections = 0, total_number_of_scores = 0, number = 0;
while (in.hasNextInt())
{
number = in.nextInt();
System.out.println (in.nextInt());
number ++;
while (count < number)
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;
count++;
}
}
if (number > 0)
{
System.out.println ("Scores for section "+count);
}
else
{
System.out.println ("Scores for section 0");
}
if (grade >= 90)
{
A ++;
}
if (grade >= 80 && grade < 90)
{
B ++;
}
if (grade >= 70 && grade < 80)
{
C ++;
}
if (grade >= 60 && grade < 70)
{
D ++;
}
if (grade < 60)
{
F ++;
}
System.out.println (" ");
System.out.println ("Scores for section "+count);
System.out.println ("A's" + A);
System.out.println ("B's" + B);
System.out.println ("C's" + C);
System.out.println ("D's" + D);
System.out.println ("F's" + F);
System.out.println ("Lowest Score: ");
System.out.println ("Highest Score: ");
System.out.println (" ");
System.out.println (" ");
System.out.println ("Total number of sections: " + total_number_of_sections);
System.out.println ("Total number of scores: " + total_number_of_scores);
System.out.println ("Class Average: ");
}
}
try this
while (in.hasNextInt())
{
count = 0;
number = in.nextInt();
System.out.println (in.nextInt());
while (in.hasNextInt())
{
grade = in.nextInt();
total_number_of_sections += number;
total_number_of_scores += grade;
total_number_of_scores ++;
if(++count == number ){ break;}
}
}
Related
I am trying to have the user input their grades and if the grades are above 90 to print them in one row, below 65 print them in one row, and if between 65 and 90 print them in one row. However, say every time there is a grade above 90, it will print "above 90" and then grade for each grade when I only want it to print the statement once. I understand I have that inside the condition so it will print it every time the condition is met, but where would I put that line of code so that this problem does not occur.
import java.util.Scanner;
import java.util.ArrayList;
public class StudentApplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many courses did you take during the school year? : ");
int takeCoursesNum = sc.nextInt();
int grades[] = new int[takeCoursesNum]; // array number start "0" ~ ex) { [0], [1], [2] }
for (int i = 0; i < takeCoursesNum; i++) {
System.out.print("\nEnter your grade for that course : ");
int grade = sc.nextInt();
grades[i] = grade;
}
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] < 65) {
System.out.println("\nGrades below 65 : " + grades[i]);// System.out.println : auto change line. no need \n.
}
if (grades[i] > 90) {
System.out.println("Grades above 90 : " + grades[i]);
}
if (grades[i] > 65 && grades[i]<90) {
System.out.println("Grades between 65 and 90: " + grades[i]);
}
}
}
}
You have to loop the array three separate times also have the label ("Grades below 65") out of the loop so it will print the statement only once. Finally use System.out.print instead of System.out.println so the grades will be printed in the same line.
System.out.print("\nGrades below 65 : ");
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] < 65) {
System.out.print(grades[i]);
}
}
System.out.print("\nGrades below 90 : ");
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] > 90) {
System.out.print(grades[i]);
}
}
System.out.print("\nGrades between 65 and 90: ");
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] > 65 && grades[i]<90) {
System.out.print(grades[i]);
}
}
Hello user11833872!
Scanner sc = new Scanner(System.in);
System.out.print("How many courses did you take during the school year? : ");
int takeCoursesNum = sc.nextInt();
int grades[] = new int[takeCoursesNum]; // array number start "0" ~ ex) { [0], [1], [2] }
for (int i = 0; i < takeCoursesNum; i++) {
System.out.print("Enter your grade for that course : ");
int grade = sc.nextInt();
grades[i] = grade;
}
System.out.println("below 65 : ");
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] < 65) {
System.out.print(i+", ");// System.out.println : auto change line. no need \n.
}
}
System.out.println("above 90 : ");
for (int i = 0; i < takeCoursesNum; i++) {
if (grades[i] > 90) {
System.out.print(i+", ");
}
}
i change it. i hope this is what you want. :)
Im trying to get my test score program to work. Whenever I type in the pathfile to read from the text file the program would read 4 out of the 5 numbers that are there.Also, no matter what number there are it is always going to display my minimum as 0 when it is not true. Any help is truly appreciated!
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Exam{
public static void main(String[] args) throws IOException {
System.out.println("Welcome!" + "\n");
//File location
System.out.println("Where is the data file:");
Scanner userInput = new Scanner(System.in);
String userFile = userInput.nextLine();
int i = 0;
int scores[] = readScores(userFile);
System.out.println("Minimum score: " + scores[0]);
System.out.println("Maximum score: " + scores[(scores.length - 1)]);
//Average Calculation
double gradesTotal = 0;
for (i=0; i<scores.length; ++i){
gradesTotal = gradesTotal + scores[i];
}
double mean = gradesTotal/scores.length;
System.out.println("Average score: " + mean);
//Mean Calculation
double median;
if (scores.length % 2 == 0)
median = ((scores[(scores.length/2) - 1]) + scores[(scores.length/2)]) / 2;
else
median = scores[(scores.length/2)];
System.out.println("Median score: " + median + "\n");
//Number of Grades
int gradeA = 0;
int gradeB = 0;
int gradeC = 0;
int gradeD = 0;
int gradeF = 0;
for (i=0; i<scores.length; i++)
{
if (scores[i] >= 90 && scores[i] <=100){
gradeA++;
}
else if (scores[i] <= 89 && scores[i] >=80){
gradeB++;
}
else if (scores[i] <= 79 && scores[i] >=70){
gradeC++;
}
else if (scores[i] <= 69 && scores[i] >=60){
gradeD++;
}
else if (scores[i] <= 59 && scores[i] >=1){
gradeF++;
}
}
System.out.println("Scores by letter grade: ");
System.out.println("A: " + gradeA);
System.out.println("B: " + gradeB);
System.out.println("C: " + gradeC);
System.out.println("D: " + gradeD);
System.out.println("F: " + gradeF);
}
//Reads the data from the submitted file
private static int[] readScores(String userFile) throws FileNotFoundException
{
File inputFile = new File(userFile);
Scanner stats = new Scanner(inputFile);
try {
int scores[] = new int[stats.nextInt()];
int i = 0;
while (stats.hasNext()){
scores[i] = stats.nextInt();
i++;
}
System.out.println("\n" + "There are " + (i) + " scores" + "\n");
Arrays.sort(scores);
return scores;
}
finally {
stats.close();
}
}
}
Text file:
72
31
13
39
74
Program output:
There are 4 scores
Minimum score: 0
Maximum score: 74
Average score: 2.1805555555555554
Median score: 0.0
Number of scores by letter grade:
A: 0
B: 0
C: 1
D: 0
F: 3
int scores[] = new int[stats.nextInt()];
here you take the first of your values and then you dont have it where you need it.
That is probably also the reason why your calculations are so messed up. you create an array of lenght 72 and use that lenght for the number of values.
Maybe you want to use a list instead. It allows you to add as many values as you want without specifying a number like you have to do when you use an array.
This is the prompt that I'm asked to follow.
Write a method called average that takes in an array of test grades and returns the letter grade
of the class average.
The grade ranges are as follows:
average >= 90 -> A
80 <= average < 90 -> B
70 <= average < 80 -> C
60 <= average < 70 -> D
average < 60 -> F
Use the following method header: public static char average(int[] grades)
This is the output example I'm supposed to follow.
How many grades do you want to enter? 10
Enter grade 1: 70
Enter grade 2: 87
Enter grade 3: 95
Enter grade 4: 80
Enter grade 5: 80
Enter grade 6: 78
Enter grade 7: 85
Enter grade 8: 90
Enter grade 9: 66
Enter grade 10: 89
The class average for the test is: B
This is what I have so far but I don't know how to include the method header stated above. public static char average(int[] grades)
import java.util.Scanner;
public class ClassAverage{
//Main Method
public static void main(String[] args){
//Variables
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b >= 90)
average = 'A';
else if(b <= 90 && b >= 80)
average = 'B';
else if(b <= 80 && b >= 70)
average = 'C';
else if(b <= 70 && b >= 60)
average = 'D';
else if(b <= 60)
average = 'F';
else
average = '?';
System.out.println("The class average for the test is: " + average);
}
}
public class MainClass {
//Main Method
public static void main(String[] args) {
//Variables
char avg;
int i;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
avg = average(array);
System.out.println("The class average for the test is: " + avg);
}
private static char average(int[] array) {
final int sum = Arrays.stream(array).sum();
final int b = sum / array.length;
if (b >= 90) {
return 'A';
} else if (b <= 90 && b >= 80) {
return 'B';
} else if (b <= 80 && b >= 70) {
return 'C';
} else if (b <= 70 && b >= 60) {
return 'D';
} else if (b <= 60) {
return 'F';
} else {
return '?';
}
} }
See that i accepted all the marks into an array and passed that int array into a method where i calculated the sum and grade and returned grade
Your instructor wants you to create a method called average that could be called from within main.
Essentially, all you need to do to complete the assignment is wrap your code inside of that method. So main would call average having passed in the ints to it.
I've written some pseudocode to help you take the code you have now and encapsulate it in the method you are requesting.
You already have the pieces written, you just need to move them from your main method to this new average method and make any necessary changes that come with changing scope (declaring or renaming certain variables in the new method).
Then to get the desired output you can call the method from within main by using average(gradeArray) and using the char value returned in a print statement.
public static char average(int[] gradeArray) {
// Find the sum of grades in gradeArray
// Use this sum and the size of the array to compute the average
// Use logic to determine which letter grade to print out based on the average
// Return the average
}
It is very simple code. My answer is
import java.util.Scanner;
public class AverageMethod {
public static void main(String[] args) {
char average;
int i;
int sum = 0;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for(i = 0;i < grades; i++){
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
sum = sum + array[i];
}
b = sum / array.length;
if(b<=100 && b>=0){
if(b<=100 && b >= 90)
average = 'A';
else if(b >= 80)
average = 'B';
else if(b >= 70)
average = 'C';
else if(b >= 60)
average = 'D';
else
average = 'F';
System.out.println("The class average for the test is: " + average);
}else{
System.out.println("Not in range");
}
}
}
I am also add my github uploaded coed for you. If you have any trouble please follow my githublink. Please study it for more clarification.
Another solution:
package test163;
import java.util.Scanner;
public class ClassAverage {
//Main Method
public static void main(String[] args) {
//Variables
char average;
int i;
int b;
Scanner scan = new Scanner(System.in);
System.out.print("How many grades do you want to enter: ");
int grades = scan.nextInt();
int array[] = new int[grades];
for (i = 0; i < grades; i++) {
System.out.print("Enter grade " + (i + 1) + ": ");
array[i] = scan.nextInt();
}
average = average(array);
System.out.println("The class average for the test is: " + average);
}
public static char average(int[] grades) {
int sum = 0;
for (int grade: grades) {
sum += grade;
}
int b = sum / grades.length;
if (b >= 90) {
return 'A';
} else if (b >= 80) {
return 'B';
} else if (b >= 70) {
return 'C';
} else if (b >= 60) {
return 'D';
} else {
return 'F';
}
}
}
My project is about finding the average, minimum and maximum of students' grades and their standing, but it keeps showing me an error on the output.
Here are the names and the grades:
Students:
Alia Nahid Eiman Suad Lamia Salma Mai Wedad Haya Sanaa
Quiz:
10 20 50 70 80 50 30 90 60 40
Midterm:
30 80 100 40 80 70 70 80 50 30
Final Exam:
40 80 70 100 90 60 70 50 40 80
And here is the Java code:
package java_final_project;
import java.util.*;
public class Java_Final_Project {
static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Declare inputs:
String letter1, letter2, letter3;
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
// Declare the four array structures
int[] quiz = new int[10];
int[] midterm = new int[10];
int[] finalexam = new int[10];
String[] students = new String[10];
// Input elements to the arrays
int counter, sum1 = 0;
int sum2 = 0;
int sum3 = 0;
double ave1, ave2, ave3;
int max1, max2, max3;
int min1, min2, min3;
for (counter = 0; counter < 10; counter++) {
System.out.println ("Enter student names and grades of quiz, midterm "
+ "and final with space between them");
students[counter] = console.next();
quiz[counter] = console.nextInt();
sum1 = sum1 + quiz[counter];
midterm[counter]= console.nextInt();
sum2 = sum2 + midterm[counter];
finalexam[counter]= console.nextInt();
sum3 = sum3 + finalexam[counter];
}
ave1 = sum1 / 10;
ave2 = sum2 / 10;
ave3 = sum3 / 10;
// min and max values
max1 = Math.max(quiz[counter], max);
max2 = Math.max(midterm[counter], max);
max3 = Math.max(finalexam[counter], max);
min1 = Math.min(quiz[counter], min);
min2 = Math.min(midterm[counter], min);
min3 = Math.min(finalexam[counter], min);
// if statement for the standing
// ave1
if (ave1 >= 90 && ave1 <= 100) {
letter1 = "E";
} else if (ave1 >= 70 && ave1 <= 80) {
letter1 = "G";
} else if (ave1 >= 50 && ave1 <= 60) {
letter1 = "S";
} else {
letter1 = "P";
}
// ave2
if (ave2 >= 90 && ave2 <= 100) {
letter2 = "E";
} else if (ave2 >= 70 && ave2 <= 80) {
letter2 = "G";
} else if (ave2 >= 50 && ave2 <= 60) {
letter2 = "S";
} else {
letter2 = "P";
}
//ave3
if (ave3 >= 90 && ave3 <= 100) {
letter3 = "E";
} else if (ave3 >= 70 && ave3 <= 80) {
letter3 = "G";
} else if (ave3 >= 50 && ave3 <= 60) {
letter3 = "S";
} else {
letter3 = "P";
}
// Display the elements of the four arrays
System.out.println("Here is the elements of the four arrays");
System.out.println("students" + "\t" + "quiz" + "\t" + "midterm" + "\t" + "finalexam");
for (counter = 0; counter < 10; counter++) {
System.out.println(students[counter] + "\t" + quiz[counter] + "\t" + midterm[counter] + "\t" + finalexam[counter]);
}
System.out.println("Summary Report:");
System.out.println();
System.out.println("students" + "\t" + "\t" + "Quiz" + "\t" + "\t" + "Midterm" + "\t" + "\t" + "FinalExam");
System.out.println();
System.out.println("Average" + "\t" + "\t" + ave1 + "\t" + "\t" + ave2 + "\t" + "\t" + ave3);
System.out.println();
System.out.println("Max" + "\t" + "\t" + max1 + "\t" + "\t" + max2 + "\t" + "\t" + max3);
System.out.println();
System.out.println("Min" + "\t" + "\t" + min1 + "\t" + "\t" + min2 + "\t" + "\t" + min3);
System.out.println();
System.out.println("Standing" + "\t" + "\t" + letter1 + "\t" + "\t" + letter2 + "\t" + "\t" + letter3);
}
}
The six lines where you set min1, min2, min3, max1, max2, max3 need to be up, inside the for loop where you gather the input; because you want to check after each input whether it's bigger or smaller than the current maximum and minimum. Where you have those lines at the moment, they can't possible work, because counter is no longer pointing to an entry in the array.
I'm not sure if you are allowed to use streams in your assignment. If you are then there is a much easier way of getting min, max, average in Java 8:
int[] scores = {1, 3, 6, 8, 10, 11, 2};
int max = Arrays.stream(scores).max();
int min = Arrays.stream(scores).min();
int avg = Arrays.stream(scores).average();
You don't really need to use loops often now that Java has streams.
I've figured out the codes and corrected some mistakes. Here's the updated java code and it worked :D! and I thank you for your comments and help :)
package java_final_project;
import java.util.*;
public class Java_Final_Project
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
// Declare inputs:
String letter1, letter2, letter3;
double ave1, ave2, ave3;
// Declare the four array structures
int[] quiz = new int[10];
int[] midterm = new int[10];
int[] finalexam = new int[10];
String [] students = new String[10];
int [] scores = new int[100];
// Input elements to the arrays
int counter, sum1=0;
int sum2=0;
int sum3=0;
for ( counter = 0; counter < 10; counter++)
{
System.out.println ("Enter student names and grades of quiz, midterm "
+ "and final with space between them");
students[counter] = console.next();
quiz[counter]= console.nextInt();
sum1 = sum1 + quiz[counter];
midterm[counter]= console.nextInt();
sum2 = sum2 + midterm[counter];
finalexam[counter]= console.nextInt();
sum3 = sum3 + finalexam[counter];
}
// max and min values for the quiz
int minGrade = quiz[0];
int maxGrade = quiz[0];
for (int i = 0; i < 10; i++)
{
if (minGrade > quiz[i])
minGrade = quiz[i];
if (maxGrade < quiz[i])
maxGrade = quiz[i];
}
// max and min values for the midterm
int minGrade1 = midterm[0];
int maxGrade1 = midterm[0];
for (int i = 0; i < 10; i++)
{ if (minGrade1 > midterm[i])
minGrade1 = midterm[i];
if (maxGrade1 < midterm[i])
maxGrade1 = midterm[i];}
// max and min values for the final
int minGrade2 = finalexam[0];
int maxGrade2 = finalexam[0];
for (int i = 0; i < 10; i++)
{if (minGrade2 > finalexam[i])
minGrade2 = finalexam[i];
if (maxGrade2 < finalexam[i])
maxGrade2 = finalexam[i];}
//calculate the average of quiz, midterm, final
ave1 = sum1/10;
ave2 = sum2/10;
ave3 = sum3/10;
// if statement for the standing
// ave1
if (ave1 > 90 && ave1 > 100)
{letter1 = "E";}
else if (ave1 > 70 && ave1 > 80)
{letter1 = "G" ;}
else if (ave1 > 50 && ave1 > 60)
{letter1 = "S";}
else
{letter1 = "P";}
// ave2
if (ave2 > 90 && ave2 > 100)
{letter2 = "E";}
else if (ave2 > 70 && ave2 > 80)
{letter2 = "G" ;}
else if (ave2 > 50 && ave2 > 60)
{letter2 = "S";}
else
{letter2 = "P";}
//ave3
if (ave3 > 90 && ave3 > 100)
{letter3 = "E";}
else if (ave3 > 70 && ave3 > 80)
{letter3 = "G" ;}
else if (ave3 > 50 && ave3 > 60)
{letter3 = "S";}
else
{letter3 = "P";}
// Display the elements of the four arrays
System.out.println (" Here is the elements of the four arrays");
System.out.println ("students"+"\t"+"quiz"+"\t"+"\t"+"midterm"+"\t"+"\t"+"finalexam");
for ( counter = 0; counter < 10; counter++)
{
System.out.println (students[counter]+"\t"+"\t"+quiz[counter]+"\t"+"\t"+midterm[counter]
+"\t"+"\t"+finalexam[counter]);
}
System.out.println (" ");
System.out.println ("Summary Report:");
System.out.println ("\t"+"\t"+"Quiz"+"\t"+"\t"+"Midterm"+"\t"+"\t"+"FinalExam");
System.out.println ("Average"+"\t"+"\t"+ave1+"\t"+"\t"+ave2+"\t"+"\t"+ave3);
System.out.println ("max"+ "\t"+"\t"+maxGrade+ "\t"+"\t"+ maxGrade1+ "\t"+"\t"+ maxGrade2);
System.out.println ("min"+ "\t"+"\t"+minGrade+ "\t"+"\t"+ minGrade1+ "\t"+"\t"+ minGrade2);
System.out.println ();
System.out.println ("Standing"+ "\t"+ letter1+ "\t"+ "\t"+ letter2+ "\t"+"\t"+ letter3);
}
}
I have code which should work, but the braces at the bottom give me an error. There is probably a simple solution that I can't find, probably because I am exhausted. Both of these codes are in different classes. If I erase a brace then another brace gives me an error.
import java.util.Scanner;
public class question {
public static void main(String[] args) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
int grade = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade scores: ");
do {
System.out.print("Grade: ");
grade = keyboard.nextInt();
if (grade >= 0 && grade < 60) {
f++;
} else if (grade < 70) {
d++;
} else if (grade < 80) {
c++;
} else if (grade < 90) {
b++;
} else if (grade <= 100) {
a++;
}
while (grade >= 0) {
System.out.println("Total scores: " + (a + b + c + d + f));
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("F: " + f);
}
} // this one gives error
}
}
The syntax is
do {
} while ( boolean expression) ;
You're missing the while part
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
If you read the documentation this is what a do while loop should look like:
do {
statement(s)
} while (expression);
What you currently have is:
do {
}
and so you are missing the while (expression) ; portion.
You're missing the 'while' part in your code. A do-while loop has to have a condition to check against after each iteration. Your syntax should look something like:
do
{
//code here
}
while (condition-here);
Note: do-while loops take a semi-colon at the end of the statement. Don't miss that
I think you are trying to do this:
public static void main(String[] args){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int f = 0;
int grade = 0;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter grade scores: ");
do{
System.out.print("Grade: ");
grade = keyboard.nextInt();
if(grade >= 0 && grade < 60){
f++;
}
else if(grade < 70){
d++;
}
else if(grade < 80){
c++;
}
else if(grade < 90){
b++;
}
else if(grade <= 100){
a++;
}
} while (grade >= 0)
System.out.println("Total scores: " + (a + b + c + d + f));
System.out.println("A: " + a);
System.out.println("B: " + b);
System.out.println("C: " + c);
System.out.println("D: " + d);
System.out.println("F: " + f);
}