簡體   English   中英

從文件讀取網格到Java中的特定類

[英]Reading a grid from a file to a specific class in Java

嗨,大家好,我想要一種讀取文件的方式-它的鏈接位於qustion的末尾-然后將其寫入此2D數組:

public class Grid {
    private Square[][] sq = new Square[10][10];
}

包含它的類是這樣的:

public class Square {

    private int currentState;
    private int row;
    private char col;

    public Square() {
        super();
    }

    public Square(int currentState, int row, char col) {
        super();
        this.currentState = currentState;
        this.row = row;
        this.col = col;
    }

}

“ currentState”變量指的是以下之一:

如果等於1,則符號為“。” 如果等於2,則符號為“#”;如果等於3,則符號為“ *”

'row'變量是指行從1到10的順序,'col'變量是指列從A到J的順序

我要閱讀的文件: https : //drive.google.com/open?id=1uEk9zRcLMO-s5efOPWS5jlRyqsLzcv-M

希望我能找到答案

您應該使用FileInputStream .read()方法從文件讀取,將返回類型從int轉換為char直到到達文件末尾。 處理每個收到的字符並在數組中設置相應的Square

try {
    File file = new File("draft.txt");
    FileInputStream fis = new FileInputStream(file);
    char current;
    while (fis.available() > 0) {
        current = (char) fis.read();
        //process your character
    }
 } catch (IOException exc) {
 //handle the exception
 }
public void intilizeGrid() {
        try {
            File file = new File("draft.txt");
            FileInputStream fis = new FileInputStream(file);
            char current;
            int i = 0 , k = 0;   //from 1 to 10
            char j = 'A';    //from A to J
            while (fis.available() > 0) {
                current = (char) fis.read(); // we have to transform this to int
                while(i < maxLimit) {
                    for(k = 0;k < maxLimit;k++) {
                        sq[i][k] = new Square(determineState(current) ,i , j);
                        j++;
                    }
                    i++;
                }   
            }
         } catch (IOException exc) {
             System.out.println("Error , IO exception has been caught !!\n");
         }
    }

這樣:

public int determineState(char currState) {
        switch (currState) {
        case '.':
            return 1;
        case '#':
            return 2;
        case '*':
            return 3;
        case 'u':
            return 4;
        }
        return 0;
    }

public void showGrid() {
        for(int i = 0;i < maxLimit;i++) {
            System.out.println();
            for(int j = 0;j < maxLimit;j++) {
                int currState = sq[i][j].getCurrentState();
                sq[i][j].showSquare(currState);
            }
        }
    }

public void showSquare(int currState) {
        char res = '\0';
        switch(currState) {
        case 1:
            res = '.';
            break;
        case 2:
            res = '#';
            break;
        case 3:
            res = '*';
            break;
        case 4:
            res = 'u';
            break;
        }
        System.out.print("    " + res + "    ");
    }

總的來說,我有:

public class Main {

    public static void main(String[] args) {
        Grid g = new Grid();
        g.intilizeGrid();
        g.showGrid();
    }
}

而且它在控制台上什么也不打印,所以怎么了?

//maxLimit = 10
public void intilizeGrid() {

             String draftFile = "draft.txt";
             FileInputStream draft = null;

            try {   
                draft = new FileInputStream(draftFile);     
                int temp;
                char current = '\0';
                int i = 0, j = 0;
                char k = 'A';
                while ((temp = draft.read()) != -1) {
                    if(current != ' ') {
                        if (j == maxLimit) {
                            j = 0; i++; k = 'A';
                        }
                        if (i == maxLimit)
                            break;
                        current = (char) temp;
                        sq[i][j] = new Square(determineState(current), i, k);
                        j++; k++;
                    }

                }
             } catch (IOException exc) {
                 System.out.println("Error , IO exception has been caught !!\n");
             }
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM