简体   繁体   English

Java-按字符的Char文本文件读入2D数组

[英]Java - Char by char text file reading into 2D array

I'm attempting to return the array "seats" which is essentially supposed to return data from a text file - 15x30 grid of "#". 我正在尝试返回数组“ seats”,该数组本质上应该从文本文件-“#”的15x30网格中返回数据。 I've tried a multitude of things, but I'm getting frustrated as I have VERY little experience with java. 我尝试了很多事情,但由于对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: 下面的代码更新部分:

 public TicketManager(){
   try{
    BufferedReader br = new BufferedReader(new FileReader("seatAvailability.txt"));
       for (int i = 0; i < 15; i++)
        String line = br.readLine();    
         seats[i] = line.toCharArray();
       }
   }catch(IOException exception){
       System.out.println("Error processing file: " + exception);
   }
 }

It will read 15 rows(lines) for input. 它将读取15行(行)作为输入。 Make sure you enter exact 30 characters in your each line. 确保在每行中输入正确的30字符。 Also don't use spacing in input line characters eg your input for each line should be like X0000xx00xx0xx0xx000xx0xxx0x00 也不要在输入行字符中使用空格,例如,每行的输入应类似于X0000xx00xx0xx0xx000xx0xxx0x00

In your returnSeats method, use seats[0].length or seats[i].length in the inner loop and StringBuilder as: 在您的returnSeats方法中,在内循环和StringBuilder中使用returnSeats seats[0].lengthseats[0].length seats[i].length如下:

public String returnSeats(){
    StringBuilder result = new StringBuilder();
    for (int i =0; i <seats.length; i++){
        for (int j = 0; j < seats[i].length; j++){
            //append seat character in the result string
            result.append(seats[i][j]);
        }
        //append new line character in the string in between
        result.append(System.lineSeparator());
    }
    return result.toString();
}

Where you have for (int j = 0; j < seats.length; j++) , you need to have for (int j = 0; j < seats[i].length; j++) . for (int j = 0; j < seats.length; j++) ,您需要for (int j = 0; j < seats.length; j++) for (int j = 0; j < seats[i].length; j++)

This has to do with how multi-dimensional arrays work. 这与多维数组的工作方式有关。 They are arrays of arrays. 它们是数组的数组。 So when you write seats.length , you are asking how many rows there are. 因此,当您编写seats.length ,您要询问有多少行。 Calling seats[i].length asks how many members are in row i . 调用seats[i].length询问第i行有多少个成员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM