Home>

My code is working and writes everything correctly to the file part2.txt. But when sorting and reading data from the file part2.txt into an array, problems begin. When I want to write a sorted array to the final file part2_sorted.txt -it writes question marks everywhere. How to prevent this and make the correct record of the sorted array?

import java.io. *;
import java.util.Scanner;
public class Part2 {
    public static void main (String [] args) {
        File file= new File ("C: \\ Users \\ kostevich.bogdan \\ IdeaProjects \\ untitled13 \\ part2.txt");
        try {
            file.createNewFile ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        int [] input= new int [10];
        for (int i= 0; i <
 input.length; i ++) {
            input [i]= (int) (Math.random () * 50);
        }
        try (FileWriter fileWriter= new FileWriter (file)) {
            for (int i= 0; i <
 input.length; i ++) {
                fileWriter.write (input [i] + "");
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
        int [] output= new int [input.length];
        try (FileReader fileReader= new FileReader (file);
             Scanner sc= new Scanner (fileReader)) {
            for (int i= 0; i <
 output.length; i ++) {
                output [i]= sc.nextInt ();
            }
        } catch (IOException e) {
            e.printStackTrace ();
        } finally {
            algorithm (output);
        }
        File file2= new File ("C: \\ Users \\ kostevich.bogdan \\ IdeaProjects \\ untitled13 \\ part2_sorted.txt");
        try {
            file.createNewFile ();
        } catch (IOException e) {
            e.printStackTrace ();
        }
        try (FileWriter fileWriter= new FileWriter (file2)) {
            for (int i= 0; i <
 output.length; i ++) {
                fileWriter.write (output [i]);
            }
        } catch (IOException e) {
            e.printStackTrace ();
        }
        StringBuilder stringBuilderInput= new StringBuilder ();
        StringBuilder stringBuilderOutput= new StringBuilder ();
        try (Scanner scanner= new Scanner (new FileReader (file));
             Scanner scanner1= new Scanner (new FileReader (file2))) {
            stringBuilderInput.append ("input== >
 ");
            stringBuilderOutput.append ("output== >
 ");
            for (int i= 0; i <
 input.length; i ++) {
                stringBuilderInput.append (input [i] + "");
            }
            for (int i= 0; i <
 output.length; i ++) {
                stringBuilderOutput.append (output [i] + "");
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace ();
        } finally {
            System.out.println (stringBuilderInput.toString ());
            System.out.println (stringBuilderOutput.toString ());
        }
    }
    public static void algorithm (int [] arr) {
        for (int i= 0; i <
 arr.length; i ++) {
            for (int j= 0; j <
 i; j ++) {
                if (arr [i] <
 arr [j]) {
                    int temp= arr [j];
                    arr [j]= arr [i];
                    arr [i]= temp;
                }
            }
        }
    }
}

@Miron but the sorting happens for me and it outputs everything correctly, only it does not write it correctly to the final file ...

Богдан Костевич2021-02-23 19:18:58

my cant, I'm going to study mat. part

Miron2021-02-23 19:18:58
  • Answer # 1

    The error is as follows -in the first case, you callvoid write (String str) throws IOExceptionclassWriter, and in the second -void write (int c) throws IOExceptionclassOutputStreamWriter, which is "Writes a single character" (that is, onecharcoded by oneinteger'om.).
    I propose to solve the error that occurs in the following way -first write everything in one line usingStringBuilder, and then write it to the file (the resulting instance of the classStringBuilder) string representation.

Trends