-2

I have used the writer before but for some reason I am having trouble implementing it in the following method. I have never put the writer in a static method.

    //replace an empty seat with a person in the seating chart
    public static void seatingChart(String seat[]) {
        for(int i = 0; i < seat.length; i++) {  
            if(seat[i]!=null) {
                System.out.print(seat[i] + " ");
            } else {
                System.out.print("empty seat  ");
            }    

            if(i % 8 == 0) {
                System.out.println();
            }
        }
    }

Does anything change for using the writer in java when the method is static compared to when the method is not?

I have only got the writer to print "empty seat" in the notepad once so far.

The final output I want to see in the notepad would be something like this:

empty seat  
empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  
empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  
empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  empty seat  

Depending on what the user enters, some of the empty seats may have a name.

2 Answers2

-1

You don't need to use a try catch for every single use of the writer. You can just use one.

public static void seatingChart(String seat[]) {
    try(PrintWriter writer = new PrintWriter("C:\\Users\\Bryce\\Downloads\\Hola\\SeatingChart.txt", "UTF-8") {

        for (int i = 0; i < seat.length; i++) {
            if (seat[i] != null) {
                System.out.print(seat[i] + " ");
                writer.print(seat[i] + " ");
                writer.print("");
            } else {
                System.out.print("empty seat  ");
                writer.print("empty seat  ");
                writer.print("");
            }

            if (i % 8 == 0) {
                System.out.println();
                writer.println();
                writer.print("");
            }
        }
    } catch (Exception e) {
        System.out.println("Error! " + e.getMessage());
    }
}

and note

try(PrintWriter writer = new PrintWriter("C:\\Users\\Bryce\\Downloads\\Hola\\SeatingChart.txt", "UTF-8") {

this is a try with resources, it will automatically close the writer for you.

ilightwas
  • 152
  • 7
-3

Well I figured it out. Probably over complicated but... the answer was:

    // replace an empty seat with a person in the seating chart
    public static void seatingChart(String seat[]) {
        try {
            writer = new PrintWriter("C:\\Users\\Bryce\\Downloads\\Hola\\SeatingChart.txt", "UTF-8");
            } 
            catch (Exception e)
            {
                System.out.println("Error! " + e.getMessage());
            }
        for (int i = 0; i < seat.length; i++) {
            if (seat[i] != null) {
                    try {
                        System.out.print(seat[i] + " ");
                        writer.print(seat[i] + " ");
                        writer.print("");
                    } 
                    catch (Exception e)
                    {
                        System.out.println("Error! " + e.getMessage());
                    }
            } else {
                try {
                    System.out.print("empty seat  ");
                    writer.print("empty seat  ");
                    writer.print("");
                    } 
                    catch (Exception e)
                    {
                        System.out.println("Error! " + e.getMessage());
                    }
            }

            if (i % 8 == 0) {
                try {
                    System.out.println();
                    writer.println();
                    writer.print("");
                    } 
                    catch (Exception e)
                    {
                        System.out.println("Error! " + e.getMessage());
                    }
            }
        }
        writer.close();
    }