如何在带有整数和字符串的文本文件中读取数组

时间:2022-09-06 00:21:34

I have read in a text file with Integers and Strings, I need to use all four pieces of information to calculate:

我在一个带有Integers和Strings的文本文件中读过,我需要使用所有四条信息来计算:

  1. Throughput
  2. Avg waiting time
  3. 平均等待时间

  4. Avg turnaround time
  5. 平均周转时间

  6. CPU idle time for an OS using First Come First Serve and Shortest Remaining Time processor algorithms. (Also, the page faults are being read in as a String but look something like this in the text file: "12, 7, 5, 79")
  7. 使用First Come First Serve和Shortest Remaining Time处理器算法的OS的CPU空闲时间。 (此外,页面错误正在以字符串形式读入,但在文本文件中看起来像这样:“12,7,5,79”)

What kind of array should I use to do this and how should I implement it? This is the part I'm struggling with.

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

Here's what I have so far:

这是我到目前为止所拥有的:

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);
        }
    }
}

Edit on 04/22:
Exception:

编辑于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)

Here's the code I've made so far:

这是我到目前为止所做的代码:

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);
        }
    }
}

And:

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());
    }
}

EDIT #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:

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"

3 个解决方案

#1


2  

The database ORM style approach to this would be to create a class representing each row in your file:

数据库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
}

Then create an ArrayList to store an arbitrary number of rows from your file:

然后创建一个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);
}

Once you have finished reading the entire file, you can iterate over the ArrayList like this:

读完整个文件后,可以像这样迭代ArrayList:

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

#2


1  

This is addressing why you are getting an error. Try this:

这解决了您收到错误的原因。尝试这个:

//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
}

This code only advances the input file when it has finished reading the 4 tokens on each line. It would also be helpful to know what the input file generally looked like if your issues persist.

此代码仅在读取每行上的4个令牌时才使输入文件前进。如果问题仍然存在,那么知道输入文件通常是什么样子也会很有帮助。

#3


0  

I really don't think there would be any problem with the code,, just check out the inputs from the text file and how they match up with next() and nextInt(),, to be on the safer side, go for BufferedReader readLine() and then do a line split

我真的不认为代码会有任何问题,只需查看文本文件中的输入以及它们如何与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
    }

In this way you know what is there in each string[]

通过这种方式你知道每个字符串[]中有​​什么

#1


2  

The database ORM style approach to this would be to create a class representing each row in your file:

数据库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
}

Then create an ArrayList to store an arbitrary number of rows from your file:

然后创建一个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);
}

Once you have finished reading the entire file, you can iterate over the ArrayList like this:

读完整个文件后,可以像这样迭代ArrayList:

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

#2


1  

This is addressing why you are getting an error. Try this:

这解决了您收到错误的原因。尝试这个:

//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
}

This code only advances the input file when it has finished reading the 4 tokens on each line. It would also be helpful to know what the input file generally looked like if your issues persist.

此代码仅在读取每行上的4个令牌时才使输入文件前进。如果问题仍然存在,那么知道输入文件通常是什么样子也会很有帮助。

#3


0  

I really don't think there would be any problem with the code,, just check out the inputs from the text file and how they match up with next() and nextInt(),, to be on the safer side, go for BufferedReader readLine() and then do a line split

我真的不认为代码会有任何问题,只需查看文本文件中的输入以及它们如何与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
    }

In this way you know what is there in each string[]

通过这种方式你知道每个字符串[]中有​​什么