Die Methoden innerhalb des my-Codes erzeugen die korrekte Ausgabe. Jedoch habe ich Probleme, die Ausgabe von meinen Methoden zu nehmen und sie in eine Textdatei zu schreiben. Gibt es eine spezielle Funktion, die ich verwenden muss, um dies zu erreichen?Eine Methode in eine Textdatei schreiben
import java.util.*;
import java.io.*;
class First {
public static ArrayList BestRouteArray(int[] someintlist) { //Sorting method to turn int list into array
ArrayList<Integer> BestRouteArray = new ArrayList<Integer>();
for (int index = 0; index < someintlist.length; index++) {
BestRouteArray.add(someintlist[index]);
}
return BestRouteArray;
}
public static void MostEfficientRoute(ArrayList<Integer> array) {
ArrayList<Integer> hotel = array;
int[] BestRoute = new int[hotel.size()]; //Defining Variables
int[] Stop = new int[hotel.size()];
for (int i = 0; i < hotel.size(); i++)
{
BestRoute[i] = (int) (Math.pow((200 - hotel.get(i)), 2)); //Penalty Calculations
Stop[i] = 0; //Stop, to index values
for (int j = 0; j < i; j++)
{
if (BestRoute[j] + Math.pow((200 - (hotel.get(i) - hotel.get(j))), 2) < BestRoute[i])
{
BestRoute[i] = (int) (BestRoute[j] + Math.pow((200 - (hotel.get(i) - hotel.get(j))), 2));
Stop[i] = j + 1;
}
}
}
// Printing values
ArrayList<Integer> Storage = new ArrayList<>();
int index = Stop.length-1;
while(index>=0)
{
Storage.add((index + 1)); //Loop to get index of Hotel and Penalty Values
index = Stop[index]-1;
}
Collections.reverse(Storage); //Reversing order of Storage
ArrayList<Integer> StoredValues = BestRouteArray(BestRoute); //Taking values from best path int list and storing in array list
if(StoredValues.get(0) != 0){
StoredValues.add(0, 0);
}
ArrayList<Integer> Penalty = new ArrayList<>();
ArrayList<Integer> HotelMarker = new ArrayList<>(); //Initializing Penalty, and hotel maker as arraylist
ArrayList<Integer> StorageHotel = Storage; //Storing Storage values in Storage hotel values to manipulate
for(int i = 0; i < Storage.size(); i++) //Using the storage values as an index to pull Penalty values from
{
Penalty.add(StoredValues.get(Storage.get(i)));
}
for(int i = 0; i < Storage.size(); i++) //Creating new array list to hold storage values -1
{
int value = StorageHotel.get(i);
StorageHotel.set(i, (value - 1));
}
for(int i = 0; i < Storage.size(); i++) //Using the new StorageHotel list as index to pull hotel values from
{
HotelMarker.add(hotel.get(StorageHotel.get(i)));
}
for(int i = 0; i < Storage.size(); i++) { //Printing the Values for both Markers and Penalty
System.out.print("Distance: " + HotelMarker.get(i) + " ");
System.out.println("Total Penalty: " + Penalty.get(i) + " ");
}
}
public static void main(String[] args) throws FileNotFoundException, java.io.IOException {
Scanner dataFile = new Scanner(new File("C:\\Users\\USER\\Documents\\Data.txt"));
ArrayList<Integer> hotels = new ArrayList<>();
hotels.add(0);
while (dataFile.hasNextLine()) {
hotels.add(dataFile.nextInt());
}
dataFile.close();
MostEfficientRoute(hotels);
PrintWriter outputFile = new PrintWriter(new FileWriter("C:\\Users\\USER\\Documents\\OutputData.txt", true));
outputFile.append(MostEfficientRoute(hotels));
outputFile.append("account1 balance: ");
outputFile.close();
}
}
, was nicht funktioniert ändern ..? in Datei schreiben? Haben Sie einen Fehler aufgetreten –