如何将我的 txt 文件转换为String
我在方法中读取的文件newGame
?我需要使用我给定的界面。使用的 txt 文件是一个 9x9 矩阵。然后我需要将其转换为 2D 数组,我该如何将 String 文件也转换为 2D int 文件。
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class GameManager implements SudokuBoardManager
{
private static GameManager myBoard;
public static void main(String[] args)
{
myBoard = new GameManager();
JFileChooser chooser = new JFileChooser();
myBoard.newGame(chooser.getSelectedFile());
System.out.println(myBoard.toString());
}
@Override
public void setValueAt(int r, int c, int v) throws InputOutOfRangeException, ValueNotValidException
{
}
@Override
public int getValueAt(int r, int c) throws InputOutOfRangeException
{
return 0;
}
@Override
public int[] displayPossibleValues(int r, int c)throws InputOutOfRangeException
{
return null;
}
public String toString()
{
return " ";
}
@Override
public void newGame(File gameFile)
{
JFileChooser chooser = new JFileChooser();
int status;
Scanner in = null;
chooser.setDialogTitle("Select Sudoku Game File");
status = chooser.showOpenDialog(null);
if(status == JFileChooser.APPROVE_OPTION)
{
try
{
gameFile = chooser.getSelectedFile();
in = new Scanner(gameFile);
}
catch(InputMismatchException e)
{
e.printStackTrace();
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}