我在一个带有整数和字符串的文本文件中读过,我需要使用所有四条信息来计算:
我应该使用什么样的数组来执行此操作以及如何实现它?这是我正在努力的部分。
这是我到目前为止所拥有的:
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);
}
}
}
SAMPLE JOB QUEUE:
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"
答案 0 :(得分:2)
数据库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...
}
答案 1 :(得分:1)
这解决了您收到错误的原因。试试这个:
//main
while (input.hasNext()) {
String jobName = input.next();
int arrival = input.nextInt(); // assumes this is your arrivalTime
int cpuTime = input.nextInt();
String inturrupt = input.next(); //assuming this is your pageFault
DataObject data = new DataObject(jobName, arrival, cpuTime, inturrupt);
list.add(data);
input.nextLine(); // advance input file to the next line
}
此代码仅在读取每行上的4个令牌时才推进输入文件。如果您的问题仍然存在,那么知道输入文件通常是什么样子也会很有帮助。
答案 2 :(得分:0)
我真的不认为代码会有任何问题,只需检查文本文件中的输入以及它们与next()和nextInt()的匹配方式,以便更安全一边,去BufferedReader readLine(),然后进行分割
BufferedReader br=new BufferedReader(new FileReader("G:\\Codemagic\\sample01.txt"));
String current_line=null;
while((current_line=br.readLine())!=null)
{
current_line.split().do something with this
}
通过这种方式,您可以知道每个字符串[]
中的内容