在文件中搜索字符或字符串 (java)

searching for a character or string in a file (java)

我试图阅读一个 file.dat,其中包含许多命令,这些命令是每行前面的字符。但不知何故,我编写的代码似乎跳过或无法识别某行前面的某些字符。

这是文件(原件):

p 10 1 100
p 23 1 50
p 12 2 275
d 1
s 1
p 14 2 1050
d 3
x 4
p 37 2 25
p 41 1 500
d 2
s 2
q

这是结果:

 got p!
 x, y, z of p command are: 10, 1, 100
 got p!
 x, y, z of p command are: 23, 1, 50
 got p!
 x, y, z of p command are: 12, 2, 275
 command not found
 got p!
 x, y, z of p command are: 14, 2, 1050
 command not found
 got p!
 x, y, z of p command are: 37, 2, 25
 got p!
 x, y, z of p command are: 41, 1, 500
 got s
 x of s command is: 2
 java.util.NoSuchElementException
 Process finished with exit code 0

但是,当我尝试稍微编辑文件时(只需交换 2 行),出现的结果会带来另一个错误

这是编辑后的文件:

d 3
p 10 1 100
p 23 1 50
p 12 2 275
s 1
d 1
p 14 2 1050
x 4
p 37 2 25
p 41 1 500
d 2
s 2
q

编辑文件的结果:

command not found
command not found
command not found
got s
x of s command is: 1
command not found
command not found
got p!
x, y, z of p command are: 37, 2, 25
got p!
x, y, z of p command are: 41, 1, 500
got s
x of s command is: 2
java.util.NoSuchElementException
Process finished with exit code 0

这是我写的代码:

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class test3 {
    public static void main(String[] args) throws Exception {
        File file = new File("file.dat");
        try {
            Scanner input = new Scanner(file);
            while (input.hasNext()) {
                if ("p".equals(input.next())) {
                    System.out.println("got p!");
                    String x = input.next();
                    String y = input.next();
                    String z = input.next();

                //I put this print and variables x,y,z here for debug but also to implemented it later on
                    System.out.println("x, y, z of p command are: " + x + ", " + y + ", " + z);

                } else if ("d".equals(input.next())) {
                    System.out.println("got d!");
                    String x = input.next();
                    System.out.println("x of d command: " + x);

                } else if ("s".equals(input.next())) { //This is the show status operation from 's' command
                    System.out.println("got s");
                    String x = input.next();
                    System.out.println("x of s command is: " + x);
                } else if ("q".equals(input.next())) {
                    System.out.println("got q");
                } else {
                    System.out.println("command not found");
                }
            }
        } catch (NoSuchElementException e) {
            System.out.print(e);
        }
    }
}

感谢大家提供的任何解决方案

您正在通过调用 input.next() 跳过文件片段。将 input.next() 的结果存储在一个变量中,然后在您的 if else 中使用该变量。一旦你知道你正在处理哪个命令,你就可以调用 input.next() 来获得你期望给定命令的输入。

如果文件格式不正确,此解决方案仍然存在问题。如果命令后面没有预期的字符数,您将无法正确解析文件。您应该使用 .nextLine() 将这些行存储在条件语句开头的变量中,并从那里解析行变量以获取命令并避免此问题。

这里我修复了第一个命令的代码:

import java.io.File;
import java.util.NoSuchElementException;
import java.util.Scanner;

public class test3 {
    public static void main(String[] args) throws Exception {
        File file = new File("file.dat");
        try {
            Scanner input = new Scanner(file);
            while (input.hasNextLine()) {
                string currentLine = input.nextLine();
                string[] currentLineArray = currentLine.split(" ");
                if ("p".equals(currentLineArray[0])) {
                    System.out.println("got p!");
                    if(currentLineArray.length == 4){
                        String x = currentLineArray[1];
                        String y = currentLineArray[2];
                        String z = currentLineArray[3];
                        System.out.println("x, y, z of p command are: " + x + ", " + y + ", " + z);
                    } else {
                        System.out.println("Incorrect number of arugments for command p!")
                    }
                }
            }
        }
    }
}

这里的问题是你在 input.next() 上测试它会迭代你的文件输入,我建议你把 input.next() 的结果放在一个变量中并测试它的内容。