Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Question Having trouble with delimiter

    So there is a file named scheduling.txt and contains the following

    ProcessFile=Processes.txt
    IOdelay=2
    ContextSwitchDelay=1
    CTSSQueues=5
    Debug=true
    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.

    So when i System.out.println(processFile);
    it prints out
    Processes.txt
    IOdelay
    I want it to be just
    Processes.txt
    Here is my code:
    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();
    
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    4
    Rep Power
    0

    Talking Thank you

    I changed the code to this
    Java 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]);
                }
    It works wonderfully. You are AWESOME. thanks
    Last edited by chris83190@hotmail.com; 03-13-2011 at 12:13 AM.

Similar Threads

  1. help plz with delimiter and while loop
    By karanhs6 in forum Advanced Java
    Replies: 1
    Last Post: 10-18-2010, 01:35 AM
  2. How to use delimiter in java
    By adityasirohi in forum New To Java
    Replies: 1
    Last Post: 02-12-2010, 10:01 PM
  3. Delimiter question
    By Kangaroo128 in forum New To Java
    Replies: 10
    Last Post: 09-09-2009, 09:28 AM
  4. using Delimiter with metacharacters
    By wntdaliv in forum New To Java
    Replies: 10
    Last Post: 12-02-2008, 06:42 AM
  5. delimiter
    By satin in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 10:50 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •