は私が行うことになってるものです:私はクラスから配列を呼び出すたびに0を得るのはなぜですか?ここ
は、引数としてファイル名を受け取るコンストラクタを持つクラスを作成します。 ファイルが一連の番号を保持し、それぞれが別々の行に書かれているとします。クラスは、配列にファイルファイルの内容を読み取り、その後、次の日付を表示しなければならない:
- アレイ
- における最小数のアレイ
- 数の合計最大数配列内の数字の平均
- 配列内の数字の平均
- 配列の内容です。
私も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.
問題は、ファイル内のすべてを読み取ってアイテムの数を確認してから、実際にデータを取得するためにファイルを再オープンしないことです。 –
デバッグのスキルを学ぶことをお勧めします。これらは、コードの記述方法を知ることと同じくらい重要です。 –
'ファイル内の数字の数を決定する 'コメントの後のループは、ファイル内のすべての数字を読み込みます。つまり、' inputFile.hasNext() 'がfalseを返すまでです。'ファイルの内容を配列に格納する'の後の次のループは、 'inputFile.hasNext()'がtrueを返す限り** read **の読み込みを試みます。あなたがすでに終わっているので、何も見つかりませんファイルの 'hasNext()'は* still * falseです。解決策:2回は読まないでください。一度ループして、必要に応じて自動的にサイズを変更する 'ArrayList'に挿入してください。 – Andreas