Question

I have this program that works but not for the correct input file. I need the...

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;
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

Add a comment
Know the answer?
Add Answer to:
I have this program that works but not for the correct input file. I need the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to change the following code so that it results in the sample output below...

    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...

    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...

  • I have a program that reads a file and then creates objects from the contents of...

    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...

  • public static void main(String[] args) {         System.out.println("Welcome to the Future Value Calculator\n");         Scanner sc...

    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...

    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...

  • Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file...

    Finish the given ProcessFile.java program that prompts the user for a filename and reprompts if file doesn’t exist. You will process through the file skipping any text or real (double) numbers. You will print the max, min, sum, count, and average of the integers in the file. You will want to create test files that contain integers, doubles, and Strings. HINT: Use hasNextInt() method and while loop. You may also want to use Integer.MAX_VALUE and Integer.MIN_VALUE for the initialization of...

  • Getting started with Java on elvis Download Greeting.java from the class web site. Use FileZilla to...

    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...

  • I am currently doing homework for my java class and i am getting an error in...

    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";...

  • I am given an input file, P1input.txt and I have to write code to find the...

    I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 {    public static...

  • In the code shown above are two parts of two different exercises. HOW WE CAN HAVE...

    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 /...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT