EchoJ
EchoJ

Reputation: 69

How to read in a text file with integers and Strings to an array

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"

Upvotes: 6

Views: 5542

Answers (3)

Creative_Cimmons
Creative_Cimmons

Reputation: 265

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

Upvotes: 0

Alexander Sobin
Alexander Sobin

Reputation: 59

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.

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521289

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

Upvotes: 2

Related Questions