这是我应该做的:
使用构造函数编写一个类,该构造函数接受文件名作为其参数。 假设文件包含一系列数字,每个数字都写在一个单独的行上。该类应将文件文件的内容读入数组,然后显示以下日期:
- 数组中的最小数字
- 数组中的最高数字
- 数组中的数字总数
- 数组中数字的平均值
- 阵列的内容。
每次运行程序时,我都会得到数组中的数字,0和其他输出。
这是工作人员NumberAnalisis
:
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.
以下是main
类:
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.
答案 0 :(得分:4)
答案很简单。您可以正确计算文件中的整数数量。使用正确的长度初始化数组。默认情况下,数组元素为0。现在,当没有要读取的数字时,对于要读取的每个数字,您更改数组值。结果没有任何变化,所以数组保持填充0。
解决方案如下:
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.
答案 1 :(得分:3)
//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.
您不会在这两者之间以任何方式重置文件。因此,inputFile.hasNext()
在第二个while循环的第一次迭代时返回false,因此永远不会运行,因此您永远不会初始化数组的任何元素(并且它们默认为零)。
答案 2 :(得分:1)
您已经在NumberAnalis中迭代扫描器,计算出阵列的大小。您必须重置计数器,因为第二个循环中的inputFile.hasNext()返回false
http://www.tutorialspoint.com/java/util/java_util_scanner.htm
并且使用逐步调配,当你的价值观出现时,你会亲眼看到
答案 3 :(得分:-1)
看起来你一直在阅读文件来计算它拥有的东西数量,然后你开始阅读这些东西而不用倒带文件。您需要在第二个读取循环之前关闭并重新打开或重置文件指针到开头。