Jon
Jon

Reputation: 11

Writing a Method to a text file

The methods within the my code produce the correct output. However, I'm having trouble taking the output from my methods and writing it out to a textfile. Is there some special function i need to use to accomplish this?

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();
    }
        }

Upvotes: 0

Views: 41

Answers (1)

A Paul
A Paul

Reputation: 8251

Your function MostEfficientRoute not returning any value.. just void.. so actually you are not writing anything in the file

outputFile.append(MostEfficientRoute(hotels));

This call is not writing anything In your function so instead of

System.out.print("Distance: " + HotelMarker.get(i) + " ");
System.out.println("Total Penalty: " + Penalty.get(i) + " ");

Declare a string at fuction level, append the above 2 lines to that string and then return that string.. then it should write in file

like

 String response = "";
    for(int i = 0; i < Storage.size(); i++) {                       //Printing the Values for both Markers and Penalty
               response  += "Distance: " + HotelMarker.get(i) + " ";
                response += "Total Penalty: " + Penalty.get(i) + " ";
            }

return response ;

also change the return type from void to String

Upvotes: 1

Related Questions