I have this program that works but not for the correct input file. I need the program to detect the commas
Input looks like:
first_name,last_name,grade1,grade2,grade3,grade4,grade5
Dylan,Kelly,97,99,95,88,94
Tom,Brady,100,90,54,91,77
Adam,Sandler,90,87,78,66,55
Michael,Jordan,80,95,100,89,79
Elon,Musk,80,58,76,100,95
output needs to look like:
Tom Brady
--------------------------
Assignment 1: A
Assignment 2: A
Assignment 3: E
Assignment 4: A
Assignment 5: C
Final Grade: 82.4 = B
The current program:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class main {
public static void main(String[] args) throws
IOException {
Scanner sc = null;
String fname, lname;
int score;
String namaes[] = null;
double tot = 0, finalGrade =
0;
double grades[] = null;
int cnt = 0;
try {
sc = new
Scanner(new File("students.txt"));
sc.nextLine();
while
(sc.hasNext()) {
sc.nextLine();
cnt++;
}
sc.close();
namaes = new
String[cnt];
grades = new
double[cnt];
sc = new
Scanner(new File("students.txt"));
sc.nextLine();
for (int i = 0;
i < cnt; i++) {
tot = 0;
fname = sc.next();
lname = sc.next();
namaes[i] = fname + " " + lname;
for (int j = 0; j < 5; j++) {
tot += sc.nextInt();
}
finalGrade = tot / 5;
grades[i] = finalGrade;
}
sc.close();
double average =
avg(grades);
double minimum =
min(grades);
double maximum =
max(grades);
System.out.println("Average of all Final Grades :" +
average);
System.out.println("Minimum of all Final Grades :" +
minimum);
System.out.println("Maximum of all Final Grades :" + maximum);
FileWriter fw
= new FileWriter(new File("OutputFile.txt"));
sc = new
Scanner(new File("students.txt"));
sc.nextLine();
for (int i = 0;
i < cnt; i++) {
fw.write(sc.next() + " " + sc.next() +
":");
for (int j = 0; j < 5; j++) {
fw.write(findgradeLetter(sc.nextInt()) + " ");
}
fw.write(grades[i] + "\n");
}
sc.close();
fw.close();
} catch (FileNotFoundException
e) {
System.out.println(e);
System.exit(0);
}
}
private static char findgradeLetter(double average)
{
char gradeLetter = 0;
if (average >= 90 &&
gradeLetter <= 100)
gradeLetter =
'A';
else if (average >= 80
&& average < 90)
gradeLetter =
'B';
else if (average >= 70
&& average < 80)
gradeLetter =
'C';
else if (average >= 60
&& average < 70)
gradeLetter =
'D';
else if (average < 60)
gradeLetter =
'F';
return gradeLetter;
}
private static double max(double[] grades) {
double max = grades[0];
for (int i = 0; i <
grades.length; i++) {
if (max <
grades[i]) {
max = grades[i];
}
}
return max;
}
private static double min(double[] grades) {
double min = grades[0];
for (int i = 0; i <
grades.length; i++) {
if (min >
grades[i]) {
min = grades[i];
}
}
return min;
}
private static double avg(double[] grades) {
double sum = 0;
for (int i = 0; i <
grades.length; i++) {
sum +=
grades[i];
}
return sum / grades.length;
}
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
// students.txt
first_name,last_name,grade1,grade2,grade3,grade4,grade5
Dylan,Kelly,97,99,95,88,94
Tom,Brady,100,90,54,91,77
Adam,Sandler,90,87,78,66,55
Michael,Jordan,80,95,100,89,79
Elon,Musk,80,58,76,100,95
======================================
// ReadFileScores.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class ReadFileScores {
public static void main(String[] args) throws
IOException {
Scanner sc=null;
String fname,lname,line =
null;
int score,a1,a2,a3,a4,a5;
double tot=0,finalGrade=0;
double grades[] = null;
int cnt=0;
try {
sc=new
Scanner(new File("students.txt"));
sc.nextLine();
while(sc.hasNext())
{
sc.nextLine();
cnt++;
}
sc.close();
grades=new double[cnt];
sc=new
Scanner(new File("students.txt"));
sc.nextLine();
for(int
i=0;i<cnt;i++)
{
tot=0;
line=sc.nextLine();
String arr[]=line.split(",");
a1=Integer.parseInt(arr[2]);
a2=Integer.parseInt(arr[3]);
a3=Integer.parseInt(arr[4]);
a4=Integer.parseInt(arr[5]);
a5=Integer.parseInt(arr[6]);
tot=a1+a2+a3+a4+a5;
finalGrade=tot/5;
grades[i]=finalGrade;
}
sc.close();
double
average=avg(grades);
double
minimum=min(grades);
double
maximum=max(grades);
System.out.println("Average of all Final Grades :"+average);
System.out.println("Minimum of all Final Grades :"+minimum);
System.out.println("Maximum of all Final Grades :"+maximum);
FileWriter
fw=new FileWriter(new File("OutputFile.txt"));
sc=new
Scanner(new File("students.txt"));
sc.nextLine();
for(int
i=0;i<cnt;i++)
{
line=sc.nextLine();
String arr[]=line.split(",");
fw.write("\n"+arr[0]+" "+arr[1]+"\n");
fw.write("----------------------------------------------\n");
fw.write("Assignment
1:"+findgradeLetter(Integer.parseInt(arr[2]))+"\n");
fw.write("Assignment
2:"+findgradeLetter(Integer.parseInt(arr[3]))+"\n");
fw.write("Assignment
3:"+findgradeLetter(Integer.parseInt(arr[4]))+"\n");
fw.write("Assignment
4:"+findgradeLetter(Integer.parseInt(arr[5]))+"\n");
fw.write("Assignment
5:"+findgradeLetter(Integer.parseInt(arr[6]))+"\n");
fw.write("Final Grade:"+grades[i]+" =
"+findgradeLetter(grades[i])+"\n");
}
sc.close();
fw.close();
} catch (FileNotFoundException e)
{
System.out.println(e);
System.exit(0);
}
}
private static char findgradeLetter(double average)
{
char gradeLetter =0;
if (average >= 90 &&
gradeLetter<=100)
gradeLetter =
'A';
else if (average
>= 80 && average < 90)
gradeLetter =
'B';
else if (average
>= 70 && average < 80)
gradeLetter =
'C';
else if (average
>= 60 && average < 70)
gradeLetter =
'D';
else if (average
< 60)
gradeLetter =
'F';
return gradeLetter;
}
private static double max(double[] grades) {
double max=grades[0];
for(int
i=0;i<grades.length;i++)
{
if(max<grades[i])
{
max=grades[i];
}
}
return max;
}
private static double min(double[] grades) {
double min=grades[0];
for(int
i=0;i<grades.length;i++)
{
if(min>grades[i])
{
min=grades[i];
}
}
return min;
}
private static double avg(double[] grades) {
double sum=0;
for(int
i=0;i<grades.length;i++)
{
sum+=grades[i];
}
return sum/grades.length;
}
}
======================================
// Output.txt (output file)
Dylan Kelly
----------------------------------------------
Assignment 1:A
Assignment 2:A
Assignment 3:A
Assignment 4:B
Assignment 5:A
Final Grade:94.6 = A
Tom Brady
----------------------------------------------
Assignment 1:A
Assignment 2:A
Assignment 3:F
Assignment 4:A
Assignment 5:C
Final Grade:82.4 = B
Adam Sandler
----------------------------------------------
Assignment 1:A
Assignment 2:B
Assignment 3:C
Assignment 4:D
Assignment 5:F
Final Grade:75.2 = C
Michael Jordan
----------------------------------------------
Assignment 1:B
Assignment 2:A
Assignment 3:A
Assignment 4:B
Assignment 5:C
Final Grade:88.6 = B
Elon Musk
----------------------------------------------
Assignment 1:B
Assignment 2:F
Assignment 3:C
Assignment 4:A
Assignment 5:A
Final Grade:81.8 = B
=====================Could you plz rate me well.Thank
You
I have this program that works but not for the correct input file. I need the...
I need to change the following code so that it results in the
sample output below but also imports and utilizes the code from the
GradeCalculator, MaxMin, and Student classes in the com.csc123
package. If you need to change anything in the any of the classes
that's fine but there needs to be all 4 classes. I've included the
sample input and what I've done so far:
package lab03;
import java.util.ArrayList;
import java.util.Scanner;
import com.csc241.*;
public class Lab03 {
public...
need help editing or rewriting java code, I have this program
running that creates random numbers and finds min, max, median ect.
from a group of numbers,array. I need to use a data class and a
constructor to run the code instead of how I have it written right
now. this is an example of what i'm being asked
for.
This is my code:
import java.util.Random;
import java.util.Scanner;
public class RandomArray {
// method to find the minimum number in...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...
Step 4: Add code that discards any extra entries at the propmt that asks if you want to enter another score. Notes from professor: For Step 4, add a loop that will validate the response for the prompt question: "Enter another test score? (y/n): " This loop must only accept the single letters of ‘y’ or ‘n’ (upper case is okay). I suggest that you put this loop inside the loop that already determines if the program should collect...
I have a program that reads a file and then creates objects from the contents of the file. How can I create a linked list of objects and use it with the package class instead of creating and using an array of objects? I am not allowed to use any arrays of objects or any java.util. lists in this program. Runner class: import java.util.Scanner; import java.io.*; class Runner { public static Package[] readFile() { try { File f = new...
I am currently doing homework for my java class and i am getting an error in my code. import java.util.Scanner; import java.util.Random; public class contact { public static void main(String[] args) { Random Rand = new Random(); Scanner sc = new Scanner(System.in); System.out.println("welcome to the contact application"); System.out.println(); int die1; int die2; int Total; String choice = "y";...
In the code shown above are
two parts of two different exercises. HOW WE CAN HAVE BOTH OF THEM
ON THE SAME CLASS BUT SO WE CAN RECALL them threw different
methods. Thank you.
1-First exercise
import java.util.Scanner;
public class first {
public static int average(int[] array){
int total = 0;
for(int x: array)
total += x;
return total / array.length;
}
public static double average(double[] array){
double total = 0;
for(double x: array)
total += x;
return total /...
Need help debugging. Create an application that keeps track of the items that a wizard can carry. console application which has no errors: import java.util.Scanner; public class Console { private static Scanner sc = new Scanner(System.in); public static String getString(String prompt) { System.out.print(prompt); String s = sc.nextLine(); return s; } public static int getInt(String prompt) { int i = 0; boolean isValid = false; while (!isValid) { System.out.print(prompt);...
Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to place it into your Lab5 directory. Look at the content of your directory to see the file using the command ls Look at the content of the file in your directory using the command more Greeting.java Compile the HelloClass program using the command javac Greeting.java. Then use ls to see your class file. Run the program without parameters using the command java Greeting Run...
Can someone explain me parts of this code I do not fully understand what its doing or what its purpose is. I have highlighted the parts I'm confused over. Please explain thoroughly. import java.util.Scanner; class A1Stats { public static void main(String args[]) { double min, max, sum, mean; int n; Scanner scanner = new Scanner(System.in); String input = scanner.nextLine(); String[] numbers = input.split(" "); double value[] = new double[numbers.length]; if(numbers.length > 0){ for (int i = 0; i < numbers.length;...