如何让Java扫描器从该文件中读取这些字符串?

如何让Java扫描器从该文件中读取这些字符串?,java,arrays,input,io,java.util.scanner,Java,Arrays,Input,Io,Java.util.scanner,我有这个输入文件: 6 8 ABBEJCDD ULSCALAR FUGLVRUT STOUASTO STOALGOT LALGOSLU 8 SCALA JAVA ALGOS ALGORITHM SLUG SLUR GOES TURTLE 目前,我让它做的是读取前两项,即int,并将它们添加到ArrayList中。然后,我只想读取接下来的6行,不包括“8”,并将它们添加到数组中 我将如何修改我的代码来做到这一点 Scanner fileScanner = null; File i

我有这个输入文件:

6
8
ABBEJCDD
ULSCALAR
FUGLVRUT
STOUASTO
STOALGOT
LALGOSLU
8
SCALA
JAVA
ALGOS
ALGORITHM
SLUG
SLUR
GOES
TURTLE
目前,我让它做的是读取前两项,即int,并将它们添加到ArrayList中。然后,我只想读取接下来的6行,不包括“8”,并将它们添加到数组中

我将如何修改我的代码来做到这一点

    Scanner fileScanner = null;
    File inFile = new File("input.dat");

    ArrayList<Integer> rc = new ArrayList<Integer>();

    try {
        fileScanner = new Scanner(inFile);
        System.out.println("The input has been loaded successfully.");

        while (fileScanner.hasNextInt()) {
            int tempRowsCols = fileScanner.nextInt();
            rc.add(tempRowsCols);
        } // end while

        while (fileScanner.hasNext()) {
            System.out.print(fileScanner.next());
        }
    } // end try

    catch (Exception e) {
        System.out.println("Did not work.");
    }
Scanner fileScanner=null;
文件填充=新文件(“input.dat”);
ArrayList rc=新的ArrayList();
试一试{
fileScanner=新扫描仪(infle);
System.out.println(“输入已成功加载”);
while(fileScanner.hasnetint()){
int tempRowsCols=fileScanner.nextInt();
rc.add(tempRowsCols);
}//结束时
while(fileScanner.hasNext()){
System.out.print(fileScanner.next());
}
}//结束尝试
捕获(例外e){
System.out.println(“不起作用”);
}

嗯,你的问题有点含糊不清,但这是你的想法吗?我看不出
ArrayList
的用途,所以我只是直接读取行和列:

try {
        fileScanner = new Scanner(inFile);
        System.out.println("The input has been loaded successfully.");

        String rows = fileScanner.next();   // read the number of rows
        String cols = fileScanner.next();   // read the number of columns
        String token = fileScanner.next();  // read next token
        System.out.println("\n\tROWS (" + rows + ")\n");
        while (!token.equals(cols)) {  // while token doesn't equal to the number of columns, keep read tokens
            System.out.println("\t" + token);
            token = fileScanner.next();
        }
        int colsInt = Integer.parseInt(token); // get number of columns as an int
        System.out.println("\n\tCOLS (" + cols + ")\n"); // read that many tokens
        for (int i = 0; i < colsInt; i++) {
            System.out.println("\t" + fileScanner.next());
        }
    } // end try
注意如果文件不是此格式,此代码将中断

通常,如果您不知道要读取多少个
int
Strings
,您可以创建一个循环,然后一次读取一个令牌,然后根据它是
字符串还是
int
,执行适当的操作:

while(fileScanner.hasNext() {
    if(fileScanner.hasNextInt()) {
        // process int
    } else {
        // process string
    }
}

这个问题是让代码以不同的方式读取数据。假设前两个值是整数,表示文本网格的高度和宽度

// read the width and height
int height = scan.nextInt();
int width = scan.nextInt();
// build the 2D array to store the char grid.
char[][] chars = new char[height][];
for (int line = 0; line < height; line++) {
    chars[line] = scan.next().toCharArray();
}
//读取宽度和高度
int height=scan.nextInt();
int width=scan.nextInt();
//构建2D数组以存储字符网格。
字符[][]字符=新字符[高度][];
用于(int line=0;line
然后得到8,表示有8个单词,然后是单词:

// get the number of words to expect
int wordcount = scan.nextInt();
// make a place to store the words.
String[] words = new String[wordcount];
for (int i = 0; i < wordcount; i++) {
    words[i] = scan.next();
}
//获取期望的字数
int wordcount=scan.nextInt();
//找一个地方存放单词。
String[]words=新字符串[wordcount];
for(int i=0;i
这将为您提供2d字符数组中的数据,以及字符串数组中的单词……

ArrayList fileStrings=new ArrayList();
ArrayList<String> fileStrings = new ArrayList<String>();
FileInputStream fis = new FileInputStream("file.txt");
BufferedReader reader = new BufferedReader(fis);
while((String tempVar = reader.readLine()) != "8")   // or whatever you want your reader to stop at
{
  fileStrings.add(tempVar);
}

for(String x : fileStrings) System.out.println(x);
FileInputStream fis=新的FileInputStream(“file.txt”); BufferedReader读取器=新的BufferedReader(fis); 而((String tempVar=reader.readLine())!=“8”)//或任何您希望您的阅读器停止的内容 { add(tempVar); } 对于(字符串x:fileStrings)System.out.println(x);
所以,您要做的是读取一个6×8字符的网格,然后,您必须在该网格中搜索接下来的8个单词?@rolfl是的,这就是我要做的。@jdc987您想只读取接下来的6个单词吗?或者您希望字符串的数量由您读取的数字决定?如果是这样的话,第二行中的8的目的是什么?@rolfl 8是我需要在网格中查找的单词数,8后面的单词是我需要查找的单词。好的,我想我找到了。您使用第二个8作为从何处开始读取8个单词的标记?我如何在try/catch循环之外访问chars数组和words数组?这将比一点点做更多的功课,但作为提示,您可能应该将我答案中的两个代码部分放在两个函数中,一个返回char[]]网格,另一个返回单词。然后,用它们来构建一个类,用于解决谜题/搜索。那么它们是否需要处于try/catch循环中呢?您应该始终有捕获异常的策略,所以,是的。这种策略通常取决于你的计划有多“严肃”,以及所做的工作有多重要。对于健壮的代码和质量报告,那么是的,您应该有一个日志系统、try/catch块和更多关于
hasNext()
ArrayList<String> fileStrings = new ArrayList<String>();
FileInputStream fis = new FileInputStream("file.txt");
BufferedReader reader = new BufferedReader(fis);
while((String tempVar = reader.readLine()) != "8")   // or whatever you want your reader to stop at
{
  fileStrings.add(tempVar);
}

for(String x : fileStrings) System.out.println(x);