Hier mein Programm der Prüfung der Gruppe C: Es war ein Programm zu erstellen welches werte aus einer anderen Datei einliest, min und max bestimmt, die Werte sortiert und in einer anderen Datei wieder ausgibt.
import java.io.*; import java.util.Scanner; import java.util.Locale;
public class YourName { public static void main (String[] args) /*method reads data, needs file name */ { String inpFile="YourName.inp"; String outFile="YourName.out"; float[] data= new float[6]; // 6 float values in Strohmer.inp float[] data1= new float[6]; float minimum=0; float maximum=0; data= read(inpFile); //String for read operation as parameter data1=data; minimum=min(data); // maximum=max(data); System.out.println("Minimum "+minimum); System.out.println("Maximum "+maximum); write(outFile, data1, maximum, minimum); //System.out.println("daten"+data[0]); } /* function for reading*/ public static float[] read(String input) { // read from File float[] data= new float[6]; //6 float values are allowed to be stored int i=0; try { Scanner sc = new Scanner(new File(input)) ; sc.useLocale(Locale.US); /*uses US symbols and not german*/ while (sc.hasNextFloat()) { data[i] = sc.nextFloat(); System.out.println("Value: " +data[i]); i=i+1; } sc.close(); } catch (Exception e) /*catch error which may occurs, catches all possible acceptions*/ { System.out.println("Error reading file "); System.exit(1); /*close program*/ } return data; } /*Function minimum- sort function was choosen and the minimum at data[0] is returned */ public static float min(float[] input) { float temp; float data[] =new float[input.length]; data=input; for (int i = 1; i<data.length; i++) { for (int j = 0; j<data.length-i; j++) { if(data[j]>data[j+1]) { temp =data[j]; data [j]= data[j+1]; data [j+1]= temp; } } } return data[0]; } /*function maximum-sort function was choosen and the maximum at data[length-1] is returned */ public static float max(float[] input) { float temp; float data[] =new float[input.length]; data=input; for (int i = 1; i<data.length; i++) { for (int j = 0; j<data.length-i; j++) { if(data[j]>data[j+1]) { temp =data[j]; data [j]= data[j+1]; data [j+1]= temp; } } } return data[input.length-1]; } public static void write(String output, float[] data, float max, float min) { // Write in file BufferedWriter bw=null; try { bw=new BufferedWriter (new FileWriter (output)); bw.write("Original Values\r\n"); for (int i=0; i<data.length ; i++ ) { bw.write(data[i]+"\r\n"); } bw.write("Maximum: "+max+"\r\n"); bw.write("Minimum: "+min+"\r\n"); bw.close(); } catch(Exception e) { System.out.println("Error writing to file "); } } }
|