Julio Rosario Julio Rosario - 3 months ago 8
Java Question

Why do I get 0 every time i call the array from a class?

Here is what i'm supposed to do:


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


  • 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 content of the array.




every time I run the program I get the amount of numbers in the array, as 0 and the other outputs as well.

here is the worker
NumberAnalisis
class:

package chapter7;

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

public class NumberAnalisis {

private int[] array;

/**
* Constructor.
* @throws IOException
*/

public NumberAnalisis(String name) throws IOException{

File file = new File(name);
Scanner inputFile = new Scanner(file);

int count =0; //To count the amount of numbers
//in the file.

int index = 0; //Index for an array.

int number;

//Determine the amount of numbers in a file.
while(inputFile.hasNext()){
inputFile.nextInt();
count++;
}

//Create an array as big as the amount of numbers
//in the file.
array = new int[count];

//Store the content of the file into the array.
while(inputFile.hasNext() && index<array.length){
number = inputFile.nextInt();
array[index] = number;
index++;
}
inputFile.close(); //Close the file.
}

/**
* The getHigh method
* @return the highest number
* in the file.
*/

public int getHigh(){

int high;

high = array[0];

for(int index =1; index<array.length; index++){
if(array[index]>high){
high = array[index];
}
}

return high;
}

/**
* The getLow method
* @return The lowest number
* in the file.
*/

public int getLow(){
int low;

low = array[0];

for(int index =1; index<array.length; index++){

if(array[index]<low){
low = array[index];
}
}

return low;
}


/**
* The total method
* @return return the
* total in the file.
*/

public int total(){
int total =0;

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

/**
* The average method
* @return The average
* of the numbers in
* the file.
*/

public double average(){

double average =0;

for(int index =0; index<array.length; index++){
average += array[index];
}

return average/array.length;
}

/**
* The getFile method
* @return the content
* of the file.
*/

public int[] getFile(){
return array;
}
}//End of class.


Here is the
main
class:

package chapter7;

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

public class NumberAnalisisTest {

public static void main(String[]args)throws IOException{
String name;

name = createFile();
display(name);

}//End of main.

/**
* The createFile method prompt the user
* to create a file and store numbers.
* @throws IOException
*/

public static String createFile()throws IOException{

String name; //Name to hold the file.

int numbers =0; //To hold the number going into
//the file.

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter the name to create a file");
name = keyboard.nextLine();

//Create a file.
PrintWriter outputFile = new PrintWriter(name + ".txt");

System.out.println("Let's start adding numbers to the file\n");

//Store numbers into the file.
while(!(numbers==-1)){
System.out.println("Enter a number" + "\nto quit press -1");
numbers = keyboard.nextInt();
if(!(numbers==-1)){
outputFile.println(numbers);
}
}
outputFile.close();
keyboard.close();
return name;
}

/**
* The display method displays
* content of the file,
* the lowest number in the file
* the highest, the total and the
* average.
* @throws IOException
*/

public static void display(String fileName) throws IOException{

NumberAnalisis number = new NumberAnalisis(fileName + ".txt");

System.out.println("The content of the file is the following");

//Display the content of the file.
for(int index = 0; index<number.getFile().length; index++){
System.out.println(number.getFile()[index]);
}

System.out.println("The highest number in the file is "
+ number.getHigh() + "\nThe Lowest is "
+ number.getLow() + "\nThe average is "
+ number.average());
}
}//End of class.

Answer

The answer is pretty easy. You count amount of integers in file correctly. Initialize the array with correct length. By default array elements are 0s. Now when there is no number to read, for each number to read you change array values. It results with no changes so the array stays filled with 0s.

The solution will be the following:

    File file = new File(name);
    Scanner inputFile = new Scanner(file);

    int count =0;   //To count the amount of numbers 
                   //in the file.

    int index = 0;  //Index for an array.

    int number;

    //Determine the amount of numbers in a file.
    while(inputFile.hasNext()){
        inputFile.nextInt();
        count++;
    }

    //Create an array as big as the amount of numbers 
    //in the file.
    array = new int[count];
    inputFile.close();
    inputFile = new Scanner(file);
    //Store the content of the file into the array.
    while(inputFile.hasNext() && index<array.length){
        number = inputFile.nextInt();
        array[index] = number;
        index++;
    }
    inputFile.close();  //Close the file.
Comments