Question

This is a java assignment on repl.it my code works but I keep failing the tests....

This is a java assignment on repl.it my code works but I keep failing the tests. Can anyone help me

Write the body of the fileAverage() method. Have it open the file specified by the parameter, read in all of the floating point numbers in the file and return their average (rounded to 1 decimal place)

For the testing system to work, don't change the class name nor the method name. Furthermore, you cannot add "throws IOException" to the fileAverage() header. Additionally, the file will have different contents during testing.

import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {
public double fileAverage( String filename ){
Scanner sc = null;
try{
sc = new Scanner( new File(filename));
}catch(IOException e){
e.printStackTrace();
}
float f;
double sum = 0;
int count = 0;
while(sc.hasNext()){
f = sc.nextFloat();
sum += f;
count += 1;
}
return Math.round(sum/count);
}
  


  
public static void main( String[] args ){
Main obj = new Main();
System.out.println( obj.fileAverage( "numbers.txt") );
}
}

Homework Answers

Answer #1
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class Main {
    public double fileAverage(String filename) {
        Scanner sc;
        try {
            sc = new Scanner(new File(filename));
        } catch (IOException e) {
            return 0.0;
        }
        double sum = 0;
        int count = 0;
        while (sc.hasNext()) {
            sum += sc.nextDouble();
            count += 1;
        }
        return Math.round(sum * 10 / count) / 10.0;
    }

    public static void main(String[] args) {
        Main obj = new Main();
        System.out.println(obj.fileAverage("numbers.txt"));
    }
}
Know the answer?
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
Similar Questions
I am a beginner when it comes to java codeing. Is there anyway this code can...
I am a beginner when it comes to java codeing. Is there anyway this code can be simplified for someone who isn't as advanced in coding? public class Stock { //fields private String name; private String symbol; private double price; //3 args constructor public Stock(String name, String symbol, double price) { this.name = name; this.symbol = symbol; setPrice(price); } //all getters and setters /** * * @return stock name */ public String getName() { return name; } /** * set...
This is the code I have written for my Java homework assignment but I can't seem...
This is the code I have written for my Java homework assignment but I can't seem to get it to run. Any help would be appreciated! import javax.swing.JOptionPane; import java.io.*; import java.util.Scanner; public class javaGamev5 { public static void main(String[] args) throws IOException { String question = null, answerA = null, answerB = null, answerC = null ; int menuChoice = 0, correctAnswer = 0, points = 0, score = 0, highscore = 0; displayIntro(); do { menuChoice = displayMainMenu();...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over...
Read in a file "numbers.txt" into a Java program. Sum up all the even numbers over 50. Print the result to the console using System.out.println(); The file will only have numbers. code below import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class ReadingFiles { public static void main(String[] args) throws FileNotFoundException { System.out.println(totalEven); } }
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
I have run huge piece of code. I need a UML for it. I dont what...
I have run huge piece of code. I need a UML for it. I dont what a UML is, but my professor said he needs it. Please help. Thanks. import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintWriter; import java.util.Scanner; public class CreditCardValidation { private String inputFileName; private String outputValidCardFileName; private String outputInvalidCardFileName; public static void main(String[] args) { CreditCardValidation ccValidationObj = new CreditCardValidation(); ccValidationObj.readFile(); } //Deafult Constrictor to inilialize instance variable public CreditCardValidation () { this.inputFileName = "data.txt"; this.outputValidCardFileName =...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the...
using java LO: (Analyze) Students will fix a loop that runs forever. This program runs the countdown sequence for a rocket launch. However, it seems to loop infinitely. Fix the program so it counts down and terminates properly. starter code : import java.util.Scanner; /* * Counts down to a blastoff, starting from a given number. */ public class Countdown {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int countdown = 0;...