everyone. I have been thinking this for 3 hours, just cannot figure out. I have a program that requires reading the file into a 2D array. The file is like:
...##..#####........
########....####..##
.........##.........
#.#.#.#.#.#.#.#.#.#.
Basically, it is about a seat reservation system.
" . "means opened seats. " # " means reserved seats. The row and col are unknown to me, depend on the file. But every row has the same number of seats.
import java.util.Scanner;
import java.io.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Arrays;
public class Main
{
public static void main(String[] args) throws Exception
{
int rows = 0, cols = 0;
char[][] auditorium = new char[rows][cols];
Scanner sc = new Scanner(new BufferedReader(new FileReader("A1.txt")));
}
I am new to java, really don't have any thoughts on this program. Please read the file and put the data into a char 2D array.
Please check this code. It'll may help to you:
public static void main(String[] args) throws Exception {
List<String> stringList = new ArrayList<>();
Scanner sc = new Scanner(new BufferedReader(new FileReader("A1.txt")));
while (sc.hasNext()) {
stringList.add(sc.nextLine());
}
int rows = stringList.size();
int cols = 0;
if (rows > 0) {
cols = stringList.get(0).length();
}
char[][] auditorium = new char[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
auditorium[i][j] = stringList.get(i).charAt(j);
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.