I’m attempting to return the array “seats” which is essentially supposed to return data from a text file – 15×30 grid of “#”. I’ve tried a multitude of things, but I’m getting frustrated as I have VERY little experience with java. My code compiles but it doesn’t print correctly when calling the method.
If anyone can help fix either the constructor or the method I would greatly appreciate it…and if there’s any way you could refrain from using code that is terribly complicated I would appreciate it as well. And explain! Thank you!
import java.io.IOException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.*;
public class TicketManager
{
private static int NUMROWS = 15;
private static int NUMCOLS = 30;
private char[][] seats = new char[NUMROWS][NUMCOLS];
private double[] price = new double[NUMROWS];
private int seatsSold;
private int totalRevenue;
public TicketManager()
{
try
{
BufferedReader br = new BufferedReader(new FileReader("seatAvailability.txt"));
String line = br.readLine();
while (line != null)
{
for (int i = 0; i < 15; i++)
{
seats[i] = line.toCharArray();
}
}
}
catch(IOException exception)
{
System.out.println("Error processing file: " + exception);
}
}
public String returnSeats()
{
String result = "";
for (int i =0; i <seats.length; i++)
{
for (int j = 0; j < seats.length; j++)
{
result += seats[i][j] + " ";
}
System.out.println("");
}
return result;
}
}
Slight problem in your loop. Updated section of code below:
It will read 15 rows(lines) for input. Make sure you enter exact
30characters in your each line. Also don’t use spacing in input line characters e.g. your input for each line should be likeX0000xx00xx0xx0xx000xx0xxx0x00In your
returnSeatsmethod, useseats[0].lengthorseats[i].lengthin the inner loop and StringBuilder as: