Results 1 to 3 of 3
Thread: Having trouble with delimiter
- 03-12-2011, 11:44 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Having trouble with delimiter
So there is a file named scheduling.txt and contains the following
I am using = as the delimiter, but when i use it, it picks up the words after the = and the words before the = on the next line.ProcessFile=Processes.txt
IOdelay=2
ContextSwitchDelay=1
CTSSQueues=5
Debug=true
So when i System.out.println(processFile);
it prints out
I want it to be justProcesses.txt
IOdelayHere is my code:Processes.txt
Java Code:package project1; import java.io.*; import java.util.*; //import java.lang.*; public class project1 { static String processFile = ""; static int ioDelay = 0; static int contextSwitchDelay = 0; static int ctssQueues = 0; static boolean debug = false; static class ProcessFile { int pID; int arrTime; int totCPU; int avgBurst; void openSchedFile() throws IOException { File s = new File("scheduling.txt"); canRead(s); Scanner sin = new Scanner(s).useDelimiter("="); if (sin.hasNextLine()) { if (sin.next().equals("ProcessFile")) processFile = sin.next(); else if(sin.next().equals("IOdelay")) ioDelay = Integer.parseInt(sin.next()); else if(sin.next().equals("ContextSwitchDelay")) contextSwitchDelay = Integer.parseInt(sin.next()); else if(sin.next().equals("CTSSQueues")) ctssQueues = Integer.parseInt(sin.next()); else if(sin.next().equals("Debug")) debug = Boolean.parseBoolean(sin.next()); } sin.close(); System.out.println(processFile); } void canRead(File f) { while (!f.canRead()) { System.out.println("Could not open file"); System.out.println("Program will now terminate."); System.exit(1); } } } public static void main(String[] args) throws IOException { ProcessFile proc = new ProcessFile(); proc.openSchedFile(); } }
-
I would use a Scanner without a specified delimiter to read the file line by line, and then split the lines read in with the "=" delimiter.
e.g.,
Java Code:Scanner sin = new Scanner(s); while (sin.hasNextLine()) { String line = sin.nextLine(); String[] tokens = line.split("="); System.out.printf("[%s, %s]%n", tokens[0], tokens[1]); // your code to handle the text would go here } sin.close();Last edited by Fubarable; 03-13-2011 at 12:00 AM.
- 03-13-2011, 12:11 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 4
- Rep Power
- 0
Thank you
I changed the code to this
It works wonderfully. You are AWESOME. thanksJava Code:Scanner sin = new Scanner(s); while (sin.hasNextLine()) { String line = sin.nextLine(); String[] tokens = line.split("="); if (tokens[0].equals("ProcessFile")) processFile = tokens[1]; else if(tokens[0].equals("IOdelay")) ioDelay = Integer.parseInt(tokens[1]); else if(tokens[0].equals("ContextSwitchDelay")) contextSwitchDelay = Integer.parseInt(tokens[1]); else if(tokens[0].equals("CTSSQueues")) ctssQueues = Integer.parseInt(tokens[1]); else if(tokens[0].equals("Debug")) debug = Boolean.parseBoolean(tokens[1]); }Last edited by chris83190@hotmail.com; 03-13-2011 at 12:13 AM.
Similar Threads
-
help plz with delimiter and while loop
By karanhs6 in forum Advanced JavaReplies: 1Last Post: 10-18-2010, 01:35 AM -
How to use delimiter in java
By adityasirohi in forum New To JavaReplies: 1Last Post: 02-12-2010, 10:01 PM -
Delimiter question
By Kangaroo128 in forum New To JavaReplies: 10Last Post: 09-09-2009, 09:28 AM -
using Delimiter with metacharacters
By wntdaliv in forum New To JavaReplies: 10Last Post: 12-02-2008, 06:42 AM -
delimiter
By satin in forum New To JavaReplies: 2Last Post: 11-17-2008, 10:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks