gpt4 book ai didi

java - 如何检查文本文件中是否包含某个单词?

转载 作者:行者123 更新时间:2023-12-01 19:34:40 25 4
gpt4 key购买 nike

我正在尝试检查这些词的每次出现 - Calculate、EXE、I/O、YieldOut。我读入一个文件并将数据存储在一个称为进程的对象中。所有数据都已正确存储在对象中。 方法 prep 查找这些单词的每次出现。唯一的问题是所有 除了I/O之外,单词都有效,第一次出现的情况永远不会出现。

代码-

 public void contentArray(Process x)
{
for(int i=0; i<x.getListInfo().size(); i++)
{
System.out.println(x.getindex(i));
}
}

public void prep(Process x)
{
System.out.println("******************");
String xo ="oo";
while(xo!="EXE")
{
for(int i=0; i<x.getListInfo().size(); i++)
{
String processor = x.getindex(i);
if(processor.equals("I/O"))
{
System.out.println("I/O " + i );
xo="I/O";
}
else if(processor.equals("CALCULATE"))
{
System.out.println("Calculate "+ i);
xo="CALCULATE";
}
else if(processor.equals("YIELD"))
{
System.out.println("YIELD " + i);
xo="YIELD";
}
else if (processor.equals("OUT"))
{
System.out.println("Out " + i);
xo="OUT";
}
else if (processor.equals("EXE"))
{
System.out.println("EXE " + i);
xo="EXE";
}
}
}
}


import java.io.*;
import java.util.*;
public class FileReader {

public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int fileNum = 0;
String filename;
int runtime = 0;
int memory = 0;
int counter = 0;
String name = "";
int filecounter = 0;
String x = "";
ArrayList<Process> allfiles = new ArrayList<Process>();
System.out.println("How many files do you have");
x = in.nextLine();
while (filecounter < Integer.parseInt(x)) // number of files
{
System.out.println("enter file name");
filename = in.nextLine();
filecounter++;
File file = new File("C:\\Users\\documents\\git\\" + filename);
Scanner s = new Scanner(file);
//Scanner s = new Scanner(new File(filename));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {// takes in all file values
list.add(s.next());
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals("Name:")) {
name = list.get(i + 1);
System.out.println(name);
//list.remove(i);// removes name
//list.remove(i+1);// removes file name
}
if (list.get(i).equals("runtime:")) {
runtime = Integer.parseInt(list.get(i + 1));
System.out.println(runtime);
//list.remove(i); //removes runtime
//list.remove(i+1); //removes runtime value
}
if (list.get(i).equals("Memory:")) {
memory = Integer.parseInt(list.get(i + 1));
System.out.println(memory);
//list.remove(i);//removes memory
//list.remove(i+1); //removes memory value
}
}
counter++;//pid starts from 1 and goes up
Process p = new Process(name, counter, runtime, memory, ProcessState.New, list);
allfiles.add(p);



s.close();




}
Scheduler schedulerQ = new Scheduler();
schedulerQ.contentArray(allfiles.get(0));
schedulerQ.prep(allfiles.get(0));


}
}

输出-

输入测试文件 姓名: 洛 总运行时间:100 内存:90

I/0 90计算 80输入/输出 90计算 20计算 100计算 100屈服屈服出去执行文件

我的错误:发生的错误永远不会发生在第一个 I/O !!!!!!!!!!!!

控制台输出:计算9

I/O 11

计算13

计算 15

计算17

产量 19

产量 20

出21

EXE 22

最佳答案

输入文件似乎在第一次出现时有 I/0 而不是 I/O。你能再检查一下输入文件吗?

关于java - 如何检查文本文件中是否包含某个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59231475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com