2017-11-13 3 views
0

私はプログラミングとJavaの新機能ですが、私は入力したテキストファイルを開いて読み込むプログラムを作成する必要があります。 12 の整数値が書き込まれています。作成した整数配列にテキストファイルの番号を読み込み、配列を低から高にソートするメソッドにパラメータとして渡してから、ソートされた配列番号を出力ファイル。出力ファイルには、ループを使用して計算され、ソートされた整数リストの最後に置かれたすべての整数の平均値も表示されます。入力ファイルを配列に読み込んでソートしてファイルに出力する

以下は私が今までに持っているものです。私は適切に配列をソートして元の関数に戻す方法を理解できないようです。平均を出して出力する方法もわかりません。誰かが助けることができれば、私はそれを非常に感謝します。 ありがとうございます。あなたのdata_arrayに読んで

import java.util.Scanner; 
import java.util.Arrays; 

public class NumberSorter { 

    public static void main(String[] args) throws Exception {  
     double sum = 0;  
     double avg = 0; 
     double total = 0; 
     int i = 0, 
     number = 0; 
     int[] data_array = new int[12]; 
     java.io.File file = new java.io.File("numbers.txt"); 
     Scanner input = new Scanner(file); 

     while(input.hasNext()){ 
      data_array[i] = input.nextInt(); 
      sortArray(data_array); 
      avg = sum/total; 
      java.io.PrintWriter output = new java.io.PrintWriter("dataout.txt"); 
      output.close(); 
     } 
    } 

    public static void sortArray(int[] data_array) 
    { 
     Arrays.sort(data_array); 
    } 
} 

答えて

0

あなたの主な問題は、あなたがあなたのwhileループでiの値をインクリメントしたことがないよう、あなただけの位置0に毎回読んでいることです。そのたびに、配列の最初の要素をテキストファイルの次の値で上書きします。理想的にすべての異なるメソッドやクラスで行わ:

これは単にi++;その後

data_array[i] = input.nextInt();の下を追加することによって解決することができ、私は、全体の separation of concernsアイデア( 注、出力を行うと、このループの外をソート推薦します問題に応じて、私はちょうど目的のためここにmain方法でそれを残すでしょう)。

だからそれゆえ、あなたが次の位置に次のintを追加しようとする、しかし、現在は、これはarrayを並べ替えますよう、whileループの外sortArrayコールを移動する方が良いでしょうarrayは異なる順序であるとして現在(おそらく)、それはあなたが思うところにそれを追加しません。

もう1つの問題は、dataoutファイルに何も書き込んでいないことです。

ファイルに書き込む方法はたくさんありますが、これは単なる一例です。

java.io.FileWriter fr = new java.io.FileWriter("dataout.txt"); 
     BufferedWriter br = new BufferedWriter(fr); 
     try (PrintWriter output = new PrintWriter(br)) { 
      for (int j = 0; j < data_array.length; j++) { 
       System.out.println(data_array[j]); 
       output.write(data_array[j] + "\r\n"); 
      } 
     } 

平均を計算してファイルの末尾に追加するだけです。

しかし、まず、配列内のすべての数値の合計を計算する必要があります。

したがって、別のループを作成するのではなく、最初のwhileループに追加して、各繰り返しで値を追加するだけです。

sum += data_array[i]; 

あなたはarray(すなわち固定長)を使用しているとして、あなたはあなたのtotal変数の値を取得するか、あるいは単にwhileループにtotal++;を追加するためにarray.length()を使用することができます。

あなたのavg = sum/total;が動作します。

全コード:

public class NumberSorter { 

    public static void main(String[] args) throws Exception { 
     double sum = 0; 
     double avg = 0; 
     double total = 0; 
     int i = 0; 
     int[] data_array = new int[12]; 
     java.io.File file = new java.io.File("numbers.txt"); 
     Scanner input = new Scanner(file); 

     while (input.hasNext()) { 
      data_array[i] = input.nextInt(); 
      //add to the sum variable to get the total value of all the numbers 
      sum += data_array[i]; 
      total++; 
      //increment the position of 'i' each time 
      i++; 
     } 
     //only sort the array after you have all the elements 
     sortArray(data_array); 

     //gets the average of all elements of the array 
     avg = sum/total; 

     java.io.FileWriter fr = new java.io.FileWriter("dataout.txt"); 
     BufferedWriter br = new BufferedWriter(fr); 
     try (PrintWriter output = new PrintWriter(br)) { 
      for (int j = 0; j < data_array.length; j++) { 
       //write each element plus a new line 
       output.write(data_array[j] + "\r\n"); 
      } 
      //write the average (to two decimal places - plus it doesn't allow 
      //you to write doubles directly anyway) to the file 
      output.write(String.format("%.2f", avg)); 
      output.close(); 
     } 
    } 

    public static void sortArray(int[] data_array) { 
     Arrays.sort(data_array); 
    } 
} 
関連する問題