6

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

  1. Throughput
  2. Avg waiting time
  3. Avg turnaround time
  4. 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")

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:

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:

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 Answers 3

2

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

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

for (RecordObject record : list) {
    // compute throughput, average waiting time, etc...
}
Sign up to request clarification or add additional context in comments.

7 Comments

There's 500 jobs in the text file, this should work fine with that many jobs in the queue right? Thank you btw.
You should have no issues handling 500 records, assuming you haven't set your JVM heap size to a very small number.
I'm receiving an a inputmismatchexception. I'm going to post the code I have now along with the error to my original post, I tried researching what might possibly be wrong but no luck, could you help me out again? Thanks.
Thank you, Edit should be uploaded.
Your current code in TextFile implies that jobName and arrival are on one line, cpuTime is a second line, and interrupt is on yet a third line. However, InputMismatchException you are getting is likely being caused by trying to read a String as an int. Please post your input file JobQueue.txt so that we may have a look.
|
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 Comments

I don't think this is going to solve the java.util.InputMismatchException.
I made some of the formatting changes you posted and then some, I'm going to post my code now...I'm no longer receiving the InputMismatchExcpetion but I just want to make sure it looks correct. Also, is there a way to post the .txt file or should I just post a snippet of it?
just post a snippet, enough so that we get the idea.
part of me thinks that CPU time and arrival time aren't Integer primitive types. can't know without seeing the input file.
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

    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[]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.