Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用BufferedReader在从文本文件读取字符串行之前读取一行整数_Java_File_Text_Bufferedreader - Fatal编程技术网

在java中使用BufferedReader在从文本文件读取字符串行之前读取一行整数

在java中使用BufferedReader在从文本文件读取字符串行之前读取一行整数,java,file,text,bufferedreader,Java,File,Text,Bufferedreader,我有一个如下所示的文本文件: 5 10 ahijkrewed llpuvesoru irtxmnpcsd kuivoqrsab eneertlqzr tree alike cow dud able dew 第一行的第一个数字5是单词搜索拼图的行数,10是拼图的列数。接下来的五行是拼图。我需要知道如何将5放入一个行整数,将10放入一个列整数。然后我需要跳到下一行来读取字符串。使用一个只有5行的修改后的文件,我找到了如何将拼图部分放入2d数组的方法,但我需要一种从正确文本fle的第一行设置数组大小

我有一个如下所示的文本文件:

5 10
ahijkrewed
llpuvesoru
irtxmnpcsd
kuivoqrsab
eneertlqzr
tree
alike
cow
dud
able
dew
第一行的第一个数字5是单词搜索拼图的行数,10是拼图的列数。接下来的五行是拼图。我需要知道如何将5放入一个行整数,将10放入一个列整数。然后我需要跳到下一行来读取字符串。使用一个只有5行的修改后的文件,我找到了如何将拼图部分放入2d数组的方法,但我需要一种从正确文本fle的第一行设置数组大小的方法

我写道:

import java.io.*;

class SearchDriver {
public static void processFile(String filename) throws FileNotFoundException, IOException {

    FileReader fileReader = new FileReader(filename);
    BufferedReader in = new BufferedReader(fileReader);

    // declare size of array needed
    //
    // int rows and columns need to be read in from first
    // line of the file which will look like: X Y
    //
    // so i need rows = X and columns = Y
    int rows = 5;
    int columns = 10;
    String[][] s = new String[rows][columns];

    // start to loop through the file
    //
    // this will need to start at the second line of the file
    for(int i = 0; i < rows; i++) {
        s[i] = in.readLine().split("(?!^)");
    }

    // print out the 2d array to make sure i know what i'm doing
    for(int i=0;i<rows;i++) {
        for(int j=0;j<columns;j++) {
            System.out.println("(i,j) " + "(" + i + "," + j + ")");
            System.out.println(s[i][j]);
        }
    }
}
public static void main(String[] args)
    throws FileNotFoundException, IOException {
        processFile("puzzle.txt");
        }
    }
import java.io.*;
类搜索驱动程序{
公共静态void processFile(字符串文件名)抛出FileNotFoundException、IOException{
FileReader FileReader=新的FileReader(文件名);
BufferedReader in=新的BufferedReader(文件读取器);
//声明所需数组的大小
//
//int行和列需要从第一个开始读入
//文件的行,看起来像:X Y
//
//所以我需要行=X,列=Y
int行=5;
int列=10;
字符串[][]s=新字符串[行][列];
//开始循环浏览文件
//
//这需要从文件的第二行开始
对于(int i=0;i对于(int i=0;i我建议一个更简单的解决方案:改用。有很多在线使用的例子(在我提供的链接中),但这可能会让您开始:

Scanner sc = new Scanner(new File(filename));
int rows = sc.nextInt();
int cols = sc.nextInt();
sc.nextLine();  // move past the newline
for(int i = 0; i < rows; i++) {
  String line = sc.nextLine();
  // etc...
}
String firstLine = in.readLine();
//You now have the two integers in firstLine, and you know that they're 
//separated by a space. How can you extract them into int rows and int columns?
String[][] s = new String[rows][columns];
Scanner sc=新的扫描仪(新文件(文件名));
int rows=sc.nextInt();
int cols=sc.nextInt();
sc.nextLine();//移过换行符
对于(int i=0;i
这看起来像是家庭作业,所以我不会给你完整的解决方案,但这里有一个提示让你开始:

Scanner sc = new Scanner(new File(filename));
int rows = sc.nextInt();
int cols = sc.nextInt();
sc.nextLine();  // move past the newline
for(int i = 0; i < rows; i++) {
  String line = sc.nextLine();
  // etc...
}
String firstLine = in.readLine();
//You now have the two integers in firstLine, and you know that they're 
//separated by a space. How can you extract them into int rows and int columns?
String[][] s = new String[rows][columns];

您可以使用
Scanner
方法
hasNextInt
nextInt
(用于整数)和
hasNext
next
用于字符串。谢谢,这可能是额外的几个步骤,但我就是这样做的。
String[]firstline=in.readLine().split(“”;int rows=Integer.parseInt(firstline[0]);int columns=Integer.parseInt(firstline[1]);
谢谢,我最终会测试出来。它看起来肯定简单多了。这将我的processFile方法减半了。谢谢!