2016-04-28 14 views
0

ファイルを読み込んで整数の配列を返し、各データ行を整数に変換するメソッドを作成しようとしています。次に、結果の配列を取得してファイルに書き戻す前に、バブルソートを使用してデータをソートしようとしています。私はバブルソートコードが正しいと確信していますが、ファイルに整数を書き込もうとする際に問題があります。私は以下のコード全体をコピーして貼り付けました。整数配列をファイルに書き込む

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

public class Main { 


public static void main(String[] args) throws IOException, 
    FileNotFoundException { 

String filename = "/Users/Desktop/13-2/src/pkg13/pkg2/sort.txt"; 
processFile(filename); 
writeToFile(filename);//calls method processFile 
} 


public static void processFile (String file) 
throws IOException, FileNotFoundException{ 
//String line; 
//lines is declared as a string 

String filename = "/Users/Desktop/13-2/src/pkg13/pkg2/sort.txt"; 
try (BufferedReader inputReader = new BufferedReader (new  InputStreamReader(new  FileInputStream("/Users/Desktop/writeTofile/src/writetofile/scorewrite.txt")))) { 
     String line; 
     while ((line = inputReader.readLine()) != null) { 
      double number = Double.parseDouble(line); 
     } 
//Scanner scanner = new Scanner(new File(filename)); 
//int i = 0; 
//while(scanner.hasNextInt()){ 
    // bubble[i++] = scanner.nextInt(); 

} 
} 

public static void writeToFile (String filename) throws IOException { 
PrintWriter outputWriter = new PrintWriter(new FileWriter(filename)); 

    outputWriter.println(); 


outputWriter.flush(); 
outputWriter.close(); 
} 
private int[] array = new int[25]; 

public int maxi(int[]a, int first){ 
    int max = 0; 
    for(int i=first; i<a.length; i++) 
    { 
     if (a[max]<a[i]){ 
      max =i; 
     } 
    } 
    return max; 
} 

public void bubble(double number) { 
    boolean a = false; 
    for (int i=0; i<array.length-1; i++) { 
     if (array [i]> array [i+1]) { 
      int temp = array [i]; 
      array [i] = array [i+1]; 
      array [i+1] = temp ;} 
     a= true; 

     } 
    } 
} 

outputWriter.println(); 

あなたは、アレイからのすべての数字を書く必要がありますが、でも、どこでも、それらを保存しない:

double number = Double.parseDouble(line); 

あなたはここで、出力ファイルに一つだけ空行を記述している

答えて

1

この行の後、f ROM入力ファイルは配列に格納する必要があります。

ヒントスニペット:

int i=0; 
while ((line = inputReader.readLine()) != null) { 
     array[i++] = Integer.parseInt(line); 
    } 

また、あなたはまた、あなたのバブルソートが正しいことを間違っているint型:(

とダブルスを混合している:(

私はもっと単純なもので始まる示唆intの配列を読み込んでファイルに書き戻します。

関連する問題