My code work and part2.txt file writes everything correctly. But when sorting and reading data from the part2.txt file, problems begin with the array. When I want a sorted array to write down to the end file part2_sorted.txt – it records the question marks everywhere. How to prevent this and make the correct entry 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 & lt; input.length; i ++) {
INPUT [i] = (int) (math.random () * 50);
}
Try (FileWriter FileWriter = New FileWriter (File)) {
For (int i = 0; i & lt; 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 & lt; Output.Length; I ++) {
OUTPUT [i] = SC.NEXTINT ();
}
} Catch (IoException E) {
E.PrintStackTrace ();
} finally {
Algorithm (Output);
}
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 & lt; Output.Length; I ++) {
filewriter.write (Output [i]);
}
} Catch (IoException E) {
E.PrintStackTrace ();
}
StringBuilder StringBuilderInderPut = New StringBuilder ();
StringBuilder StringBuilderOutput = new stringbuilder ();
try (SCANNER Scanner = New Scanner (New FileReader (File));
Scanner Scanner1 = New Scanner (New FilerEader (File2)) {
stringbuilderinput.append ("Input == & gt;");
StringBuilderOutput.append ("Output == & gt;");
For (int i = 0; i & lt; input.length; i ++) {
stringbuilderinput.append (Input [i] + "");
}
For (int i = 0; I & lt; 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 & lt; arr.length; I ++) {
For (int j = 0; j & lt; i; j ++) {
if (Arr [i] & lt; arr [j]) {
int temp = arr [j];
arr [j] = arr [i];
ARR [I] = TEMP;
}
}
}
}
}
Answer 1, Authority 100%
Error in the following – In the first case, you call Void Write (String Str) Throws IoException
Class Writer
, and in the second – void write (int c) Throws IoException
Class OutputStreamwriter
, which “Writes a Single Character” (i.e., one char
is encoded by one Integer
Ohm.) .
The resulting error suggests to solve the following way – first write down everything into one line using StringBuilder
, and then write to the file of it (the resulting instance of the StringBuilder
class).