Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 6th, 2012, 08:33 AM
jasondj jasondj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 92 jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 5 h 52 m 39 sec
Reputation Power: 24
Determining whether a user entered string is a relative or an absolute path

Hey guys.

I am writing a small program that needs to determine whether a user entered string is a relative path or an absolute path. Basically, what I am trying to do in pseudo-code is...

Code:
private static boolean isRelative(String path) {
    //return true if the path is relative, false if it is absolute
}

...

String path = getUserEnteredPath();
if ( isRelative(path) ) {
     //append an environment variable to the path before we use it
} else {
     //utilize the path in its current form
}
...



The part I am struggling with is the best way to write the isRelative method. I was thinking about using a regular expression on the first three characters against "[A-Z]:\", but there are a few things I don't like about that approach. For one, it will only work on Windows, which is acceptable but not ideal. Second, I don't know how to use regular expressions and would just as soon avoid doing so if I can get by without it.

What do you guys think, is there a better way?

Reply With Quote
  #2  
Old November 6th, 2012, 10:09 AM
jasondj jasondj is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2010
Posts: 92 jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level)jasondj User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 5 h 52 m 39 sec
Reputation Power: 24
Here's another method of determining relative or absolute paths I came up with. I like it better than using regular expressions since it is platform independent, but I am still open to suggestions if anyone thinks there is a better way.

Code:
	private static String formatPath( String p ) {
		
		File absoluteFile = null;
		File relativeFile = null;
		
		
		absoluteFile = new File(p);
		
		String relativeFileName = ENV_VAR + SEP_CHAR + p;
		relativeFile = new File(relativeFileName);
		
		String path = "";
		
		if (absoluteFile.exists()) {
			//TODO use absolute file path
			path = absoluteFile.getAbsolutePath();
			System.out.println("Absolute: " + path);
		} else if (relativeFile.exists()) {
			//TODO use relative file path
			path = relativeFile.getAbsolutePath();
			System.out.println("Relative: " + path);
		} else {
			//TODO throw exception
			System.out.println("File could not be resolved");
		}

...
		
	}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Determining whether a user entered string is a relative or an absolute path

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap