Results 1 to 14 of 14
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default method won't go any further

    The next method creates a new text file called untitled.doc
    If the file is present it will try untitled1.doc (untitled2.doc etc.)
    But when running the method it suddenly stops here see*:

    current dir /Users/willem/NetBeansProjects/WillemWriter
    temp /Users/willem/NetBeansProjects/WillemWriter/untitled.doc
    temp.getname untitled.doc
    i 1
    s untitled.doc <--- * here it stops but the program keeps running

    So what is going on here?

    Java Code:
    // Open a new file with name untitled(1-50)
        public void newFile(String fn)  {
            String currentDir = System.getProperty("user.dir");
            //new File(".").getAbsolutePath());
            System.out.println("current dir " + currentDir);
            String separator = System.getProperty("file.separator");
            if (currentDir.endsWith(separator)) {
                separator = "";
            }
            File temp = new File(currentDir + separator + fn);
            System.out.println("temp " + temp);
            System.out.println("temp.getname " + temp.getName());
            if(temp.isDirectory()) {
                fileName = null;
            } else {
                int i = 0;
                while (temp.exists()) {
                    i++;
                    System.out.println("i " + i);
                    String s = temp.getName();
                    System.out.println("s " + s);
                    String[] parts = s.split(".");
                    s = currentDir + separator + parts[0] + i + ".doc";
                    System.out.println("s2 " + s);
                    temp = new File(s);
                    System.out.println("temp " + temp);
                }
            fileName = "" + temp;
            }
            System.out.println("open new file " + fileName);
            this.canWriteFile = true;	 // Assume we can write
        }
    Last edited by willemjav; 01-10-2014 at 06:13 PM.

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: method won't go any further

    Are you certain split is splitting properly and not throwing an exception? Try replacing . with \\. in your regex.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    When I substitute parts[0] with "untitled"
    s = currentDir + separator + "untitled" + i + ".doc";
    It works, so I should check the split

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    current dir /Users/willem/NetBeansProjects/WillemWriter
    temp /Users/willem/NetBeansProjects/WillemWriter/untitled.doc
    temp.getname untitled.doc
    i 1
    s untitled.doc
    array length 0
    s2 /Users/willem/NetBeansProjects/WillemWriter/untitled1.doc
    temp /Users/willem/NetBeansProjects/WillemWriter/untitled1.doc
    i 2
    s untitled1.doc
    array length 0
    s2 /Users/willem/NetBeansProjects/WillemWriter/untitled2.doc
    temp /Users/willem/NetBeansProjects/WillemWriter/untitled2.doc
    open new file /Users/willem/NetBeansProjects/WillemWriter/untitled2.doc

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: method won't go any further

    Do what I suggested and it should work.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    Weird, now it works (I have indeed two untitled at the direction: untitled.doc and untitled1.doc)
    But still do not understand why the split is not working?

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    Yes this works: String[] parts = s.split("\\.");
    thanks

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: method won't go any further

    This split was not working because it takes a regex and "." has a special meaning to the regex engine. If you want to split on a real period (.), then you need to escape it from regex interpretation. So use "\\."

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    What makes the difference "." and "\\." ?

  10. #10
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    okay thanks (these things give me a head pain)

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,440
    Blog Entries
    7
    Rep Power
    20

    Default Re: method won't go any further

    Quote Originally Posted by willemjav View Post
    What makes the difference "." and "\\." ?
    Splitting on "." splits on any character; splitting on "\\." splits on a single dot character.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    Still a small problem, it is printing: untitled12.doc...
    But I can fix that one!

  13. #13
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: method won't go any further

    Well, to add to your headache, you could also have used "[.]". Check out the API on Pattern (Java Platform SE 7 ). It has a lot of information worth knowing.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: method won't go any further

    this one works

    Java Code:
    public void newFile(String filename)  {
            String currentDir = System.getProperty("user.dir");
            String separator = System.getProperty("file.separator");
            if (currentDir.endsWith(separator)) {
                separator = "";
            }
            File temp = new File(currentDir + separator + filename);
            
            String[] parts = filename.split("[.]");
            String splitFilename = null;
            if (parts.length > 0) {
                splitFilename = parts[0];
            }
    
            if(temp.isDirectory()) {
                this.fileName = null;
            } else {
                int i = 0;
                while (temp.exists() & i < 50) {
                    i++;
                    String s = currentDir + separator + splitFilename + i + ".doc";         
                    temp = new File(s);              
                }
            this.fileName = "" + temp;
            }
        }
    Last edited by willemjav; 01-10-2014 at 07:20 PM.

Similar Threads

  1. Replies: 1
    Last Post: 12-12-2013, 07:08 PM
  2. Replies: 7
    Last Post: 04-11-2013, 05:31 AM
  3. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  4. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  5. Replies: 18
    Last Post: 04-18-2011, 05:39 AM

Posting Permissions

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