Nhok V Nhok V - 1 month ago 12
Java Question

Getting 0.0 on NumberAnalyzer.java. Need helps

Write a class with a constructor that accepts a file name as its argument. Assume the file contains a series of numbers, each written on a separate line. The class should read the contents of the file into an array, and then displays the following data.


  • The lowest number in the array

  • The highest number in the array

  • The total of the numbers in the array

  • The average of the numbers in the array.



The file, Numbers.txt used for the above program contains these twelve numbers:

8.71
7.94
3.01
29.27
9.23
82.76
12.6
47.99
63.89
1.09
22.23
79.17


This is the main program: NumberAnalyzerDemo.java

import java.io.*; // Needed for IOException

/**
This program demonstrates a solution to the
Number Analysis Class programming challenge.
*/

public class NumberAnalyzerDemo
{
public static void main(String[] args) throws IOException
{
// Create a NumberAnalyzer object.
NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");

// Display data about the numbers in the file.
System.out.println("The lowest number in the file is " +
na.getLowest());
System.out.println("The highest number in the file is " +
na.getHighest());
System.out.println("The total of the numbers in the file is " +
na.getTotal());
System.out.println("The average of the numbers in the file is " +
na.getAverage());
}
}


This is the class: NumberAnalyzer.java

import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.io.File;

/**
The NumberAnalyzer class is to searching the numbers in a file.
*/
public class NumberAnalyzer
{
private double[] numbers;
private int count;
File file;
Scanner scan;

/**
Constructer that accepts the file name as its argument.
*/
public NumberAnalyzer(String filename) throws IOException
{
count = 0;
file = new File("Numbers.txt");
scan = new Scanner(file);
numbers = new double[11];
}

/**
The getLowest() method to search the file and pull out the lowest
number in the file.
@return Return the lowest number.
*/
public double getLowest()
{
double low = numbers[0];

for (int i = 0; i < numbers.length; i++)
{
if (low > numbers[i])
{
low = numbers[i];
}
}
return low;
}

/**
The getHighest() method to search the file and pull out the highest
number in the file.
@return Return the highest number.
*/
public double getHighest()
{
double high = numbers[0];

for (int i = 0; i < numbers.length; i++)
{
if (high < numbers[i])
{
high = numbers[i];
}
}
return high;
}

/**
This method calculate the total of all the number in the file.
@return Adding all number in the file.
*/
public double getTotal()
{
double total = 0;

for (int i = 0; i < numbers.length; i++)
{
total += numbers[i];
}
return total;
}

/**
This method used to calculate the average of the numbers in the file.
@return Using the getTotal() divided to the length of the numbers.
*/
public double getAverage()
{
return getTotal() / numbers.length;
}

/**
This method to read all the file txt and get the right number.
*/
private void getNumbers(String filename)
{
while(scan.hasNext())
{
numbers[count] = scan.nextDouble();
count++;
}
scan.close();
}

/**
This method
*/
private int getNumberOfValues(String filename)
{
return count ;
}
}


I'm getting 0.0 for all the output. Please give me some suggestions. Thanks!

Answer

solution

change you method

private void getNumbers(String filename)

to

public void getNumbers()

and then do

NumberAnalyzer na = new NumberAnalyzer("Numbers.txt");
na.getNumbers();