2016-08-26 11 views
-1

は私が行うことになってるものです:私はクラスから配列を呼び出すたびに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. 
+0

問題は、ファイル内のすべてを読み取ってアイテムの数を確認してから、実際にデータを取得するためにファイルを再オープンしないことです。 –

+1

デバッグのスキルを学ぶことをお勧めします。これらは、コードの記述方法を知ることと同じくらい重要です。 –

+0

'ファイル内の数字の数を決定する 'コメントの後のループは、ファイル内のすべての数字を読み込みます。つまり、' inputFile.hasNext() 'がfalseを返すまでです。'ファイルの内容を配列に格納する'の後の次のループは、 'inputFile.hasNext()'がtrueを返す限り** read **の読み込みを試みます。あなたがすでに終わっているので、何も見つかりませんファイルの 'hasNext()'は* still * falseです。解決策:2回は読まないでください。一度ループして、必要に応じて自動的にサイズを変更する 'ArrayList'に挿入してください。 – Andreas

答えて

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. 
+0

答えはとても簡単です: 'List'を使います。 – Andreas

+0

はい、リストは良いアプローチになります。 OPが自分自身で何かを書くようにしましょう – xenteros

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()は、2番目のwhileループの最初の繰り返しでfalseを返すので、決して実行されないため、配列の要素のいずれも初期化しません(デフォルトではゼロになります)。

-1

は、あなたがそれを持っているものの数をカウントするためにすべての方法を通じてファイルを読み込むように、あなたは、ファイルを巻き戻しせずに物事を読み始める見えます。 2番目の読み取りループの前に、ファイルポインタを最初にクローズして再オープンするか、リセットする必要があります。

+1

'Scanner'をファイルの先頭に戻すことはできません。 – Andreas

+2

Scannerの@nicompリセットメソッドは、ファイル内のポインタを移動しません。 – xenteros

+0

私はScannerオブジェクトをリセットすることについて何も書いていませんでした。戻って私の答えを調べてください。 – nicomp

1

配列のNumberAnalisis wher計算サイズでScannerを反復処理しました。あなたはあなたの第二のループ内inputFile.hasNext()がfalseを返すので、カウンタをリセットする必要がある値は、手のaoutで行くとデバッグのは、あなた自身のために表示されます

http://www.tutorialspoint.com/java/util/java_util_scanner.htm

と使用ステップ・バイ・ステップ

+1

もちろん、カウンタをリセットする必要はありません!ファイル内のポインタをリセットする必要があります – xenteros

関連する問題