I have read in a text file with Integers and Strings, I need to use all four pieces of information to calculate:
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"
Use the fs. readFileSync() method to read a text file into an array in JavaScript, e.g. const contents = readFileSync(filename, 'utf-8'). split('\n') . The method will return the contents of the file, which we can split on each newline character to get an array of strings.
An Object[] can hold both String and Integer objects. Here's a simple example: Object[] mixed = new Object[2]; mixed[0] = "Hi Mum"; mixed[1] = Integer.
To convert an array of strings to an array of numbers:Use the forEach() method to iterate over the strings array. On each iteration, convert the string to a number and push it to the numbers array.
The readString() method of File Class in Java is used to read contents to the specified file. Return Value: This method returns the content of the file in String format. Note: File. readString() method was introduced in Java 11 and this method is used to read a file's content into String.
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...
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With