0

I wrote this code which takes a .txt file and scans it. Each line represents a separate process with its attributes. I need to be able to loop through each line of the .txt file and assign the different values to the process's fields.

Here's my process class:

public class Process {

    private String name;
    private int arrive_time= 0;
    private int burst_time = 0;
    private int remain_time = 0;

    public Process (String name, int arr_time, int bur_time) {

        this.arrive_time = arr_time;
        this.burst_time = bur_time;
        this.remain_time = burst_time;
        this.name = name;
    }

    public int getArrTime() {return arrive_time;}
    public int getBurTime() {return burst_time;}
    public int getRemTime() {return remain_time;}
    public String getName() {return name;}

    public void decRemTime() {this.remain_time--;}
}

Here's my .txt file:

P1 0 8
P2 1 4
P3 2 9
P4 3 3
END 4 9999

p1 is supposed to be assigned to the name variable of the first process. 0 is the arrival time. and 8 is the burst time. Then we move onto the next line and do the same for a new process that we will be creating every time I move to a new line in the .txt

Here's my code for assigning things:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class Test {

    public static void main(String[] args) {

        //Priority queue for storing the initialized processes
        PriorityQueue<Process> prq = new PriorityQueue<Process>(5, new Comparator<Process> () {

            @Override
            public int compare(Process p1, Process p2) {
                return p1.getArrTime() - p2.getArrTime();
            }
        });

        BufferedReader br = null;
        String line;

        try {
            br =  new BufferedReader(new FileReader("C:\\Users\\Veni\\Desktop\\test\\test.txt\\"));
        }
        catch (FileNotFoundException fnfex) {
            System.out.println(fnfex.getMessage() + "File not found");
            System.exit(0);
        }

        try {
            int localProcessIndex = 0;

            /* Count number of lines in .txt and store number in localProcessIndex.
             * Then declare exactly that many processes.
             * 
             * Then move to the loop below and start reading each line's values
             * and start initialising the processes with those values.
             * 
             * Then move all the processes to the prq priority queue.
            */

            while((line = br.readLine()) != null) {


                //Process localProcessIndex = new Process(line.split);
                //System.out.println(line);
            }
        }
        catch(IOException ioex) {
            System.out.println(ioex.getMessage() + "Error reading");
        }

        SPN spn = new SPN(prq);
        spn.SPN_ALG();
    }
}
0

2 Answers 2

1

Assuming that your file will always have that same structure, you could use the split(String regex) method to process your data:

while((line = br.readLine()) != null) {
    try {
    String[] params = line.split(" ");
    prq.add(new Process(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]))),
    ...
    }
    catch(Exception e) {
        //Log
    }
}

EDIT: What you need to do is to have a list of Process items. This will allow you to create the amount of processes you need and make them available at a later stage. I have modified the code above to provide this functionality.

Sign up to request clarification or add additional context in comments.

5 Comments

Excellent. Now I need to figure out how I can generate the exact number of processes as there are lines. And do that automatically. If it helps, I wrote exactly what my problem is in the last code snippet - in comments.
@RumenHristov: I have updated my answer. Please try to use collections for these sort of scenarios, as opposed to creating a new item manually.
that's exactly what I did. Here:prq.add(new Process(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2])));
@RumenHristov: Yes sorry my bad. Should be fixed now.
Nice job. Thanks very much. Love this place! :) :)
1

For each line, split it by a space. Next use parseInt to get the numbers. Finally call the constructor with these values:

String line = "P1 0 8";
String params = line.split("\s");
Process process = new  Process(params[0], Integer.parseInt(params[1]), Integer.parseInt(params[2]));

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.