gpt4 book ai didi

java - 如何将包含整数和字符串的文本文件读入数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:10 26 4
gpt4 key购买 nike

我读入了一个包含整数和字符串的文本文件,我需要使用所有四种信息来计算:

  1. 吞吐量
  2. 平均等待时间
  3. 平均周转时间
  4. 使用先来先服务和最短剩余时间处理器算法的操作系统的 CPU 空闲时间。 (此外,页面错误正在作为字符串读取,但在文本文件中看起来像这样:"12, 7, 5, 79")

我应该使用什么样的数组来执行此操作以及我应该如何实现它?这是我正在努力解决的部分。

这是我目前所拥有的:

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

public class TextFile {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("JobQueue.txt"));

String jobName;
int arrivalTime;
int cpuTime;
String pageFault;

while (input.hasNext()) {
jobName = input.next();
arrivalTime = input.nextInt();
cpuTime = input.nextInt();
pageFault = input.next();

System.out.printf("\n%s %d %d %s\n", jobName, arrivalTime, cpuTime, pageFault);
}
}
}

04/22 编辑:
异常:

java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at TextFile.main(TextFile.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

这是我到目前为止编写的代码:

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

public class TextFile {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("JobQueue.txt"));
ArrayList<DataObject> list = new ArrayList<DataObject>();

while (input.hasNext()) {
String jobName = input.next();
int arrival = input.nextInt();
input.nextLine();
int cpuTime = input.nextInt();
input.nextLine();
String inturrupt = input.next();

DataObject data = new DataObject(jobName, arrivalTime, cpuTime, pageFault);
list.add(data);
}
}
}

和:

public class DataObject {
private String jobName;
private int arrivalTime;
private int cpuTime;
private String pageFault;

public DataObject(String job, int arrival, int cpu, String interrupt) {
jobName = job;
arrivalTime = arrival;
cpuTime = cpu;
pageFault = interrupt;
}

public void setjobName(String job) {
jobName = job;
}

public String getJobName() {
return jobName;
}

public void setArrivalTime(int arrival) {
arrivalTime = arrival;
}

public int getArrivalTime() {
return arrivalTime;
}

public void setcpuTime(int cpu) {
cpuTime = cpu;
}

public int getcpuTime() {
return cpuTime;
}

public void setPageFault(String interrupt) {
pageFault = interrupt;
}

public String getPageFault() {
return pageFault;
}

public String toString() {
return String.format("\n%s %d %d %s\n", getJobName(), getArrivalTime(), getcpuTime(), getPageFault());
}
}

编辑 #2

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.IOException;

public class TextFile
{
public static void main(String[] args) throws IOException
{
Scanner input = new Scanner(new File("JobQueue.txt"));

ArrayList<DataObject> list = new ArrayList<DataObject>();

while(input.hasNext())
{
String jobName = input.next();
int arrivalTime = input.nextInt();
int cpuTime = input.nextInt();
String pageFault = input.next();

DataObject data = new DataObject(jobName, arrivalTime, cpuTime, pageFault);
list.add(data);
}
}
}

示例作业队列:

J1  0   90  "7,27,73,86"
J2 1 39 "12"
J3 2 195 "11,31,73,94,120,134,183"
J4 3 198 "7,25,57,83,112,138,190"
J5 4 103 "11,26,58,94"
J6 5 158 "15,39,63,79,120,144"
J7 6 168 "9,46,66,84,125,147"

最佳答案

为此,数据库 ORM 样式的方法是创建一个代表文件中每一行的类:

public class RecordObject {
private String jobName;
private int arrivalTime;
private int cpuTime;
private String pageFault;

public RecordObject(String jobName, int arrivalTime, int cpuTime, String pageFault) {
this.jobName = jobName;
this.arrivalTime = arrivalTime;
this.cpuTime = cpuTime;
this.pageFault = pageFault;
}

// getters and setters
}

然后创建一个 ArrayList 来存储文件中任意数量的行:

ArrayList<RecordObject> list = new ArrayList<RecordObject>();

while(input.hasNext()) { // <-- this is your original code
jobName = input.next();
arrivalTime = input.nextInt();
cpuTime = input.nextInt();
pageFault = input.next();

RecordObject record = new RecordObject(jobName, arrivalTime, cpuTime, pageFault);
list.add(record);
}

读取完整个文件后,您可以像这样遍历 ArrayList:

for (RecordObject record : list) {
// compute throughput, average waiting time, etc...
}

关于java - 如何将包含整数和字符串的文本文件读入数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665448/

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