首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在文件中搜索字符或字符串(java)

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

Stack Overflow用户
提问于 2018-09-19 03:52:08
回答 2查看 328关注 0票数 2

我正在尝试读取一个包含许多命令的file.dat,这些命令是每行前面的字符。但不知何故,我写的代码似乎跳过或不能识别某些行前面的一些字符。

这是文件(原始文件):

代码语言:javascript
复制
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

这就是结果:

代码语言:javascript
复制
 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行)

编辑后的文件如下:

代码语言:javascript
复制
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

编辑文件的结果:

代码语言:javascript
复制
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

这是我写的代码:

代码语言:javascript
复制
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);
        }
    }
}

我非常感谢你们所有人提出的任何解决方案

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-09-19 04:02:34

您通过调用input.next()跳过了文件的各个部分。将input.next()的结果存储在一个变量中,然后在if elses中使用该变量。一旦您知道您正在处理的是哪个命令,您就可以为给定命令的任意数量的输入调用input.next()

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

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

代码语言:javascript
复制
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!")
                    }
                }
            }
        }
    }
}
票数 2
EN

Stack Overflow用户

发布于 2018-09-19 04:04:10

这里的问题是,您在迭代文件输入的input.next()上进行测试,我建议您将input.next()的结果放在一个变量中,并对其内容进行测试。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52393672

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档