Home arrow static arrow Java Programming [Archive] - Splitting problem
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Splitting problem
This topic has 5 replies on 1 page.

Posts:22
Registered: 8/3/04
Splitting problem  
Aug 4, 2004 3:45 AM



 
why does this not work?
public String getOnlyFileName(String nameWithPath){
String[] parts = nameWithPath.split("
");
return parts[parts.length - 1];
}

if have the following error if i use this

Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.split(Unknown Source)
at java.lang.String.split(Unknown Source)
at editor.MainForm.getOnlyFileName(MainForm.java:226)
at editor.MainForm.showSaveChooser(MainForm.java:221)
at editor.MainForm.actionPerformed(MainForm.java:284)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.menuDragMouseReleased(Unknown Source)
at javax.swing.JMenuItem.fireMenuDragMouseReleased(Unknown Source)
at javax.swing.JMenuItem.processMenuDragMouseEvent(Unknown Source)
at javax.swing.JMenuItem.processMouseEvent(Unknown Source)
at javax.swing.MenuSelectionManager.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

 

Posts:319
Registered: 11/8/00
Re: Splitting problem  
Aug 4, 2004 4:00 AM (reply 1 of 5)



 
try this

public String getOnlyFileName(String nameWithPath){    String[] parts = nameWithPath.split(File.separator);    return parts[parts.length - 1];}

hope that helps
 

Posts:5,119
Registered: 16.08.01
Re: Splitting problem  
Aug 4, 2004 4:09 AM (reply 2 of 5)



 
Read this:
http://forum.java.sun.com/thread.jsp?forum=31&thread=452985
 

Posts:13
Registered: 8/3/04
Re: Splitting problem  
Aug 4, 2004 4:10 AM (reply 3 of 5)



 
public String getOnlyFileName(String nameWithPath){          String[] parts = nameWithPath.split("\\\\");           return parts[parts.length - 1];} 


This works...
 

Posts:105
Registered: 6/8/04
Re: Splitting problem  
Aug 4, 2004 6:27 AM (reply 4 of 5)



 
The problem is that, when you send the escaped character '
' as a regular expression, the regex sees it as just the '\' character (since '
' is the escape for the '\' character). Within a regular expression, though, the '\' character is used to escape other things, so if the regex only gets '\', it doesn't know what you're trying to escape.

By using "\\\\", as Mike suggested, the regex reads it as "
", and thus knows that you're escaping a backslash, so it knows that you mean '\'.

Make sense?
 

Posts:79
Registered: 11/2/00
Re: Splitting problem  
Aug 4, 2004 6:59 AM (reply 5 of 5)



 
public String getOnlyFileName(String nameWithPath){
String[] parts = nameWithPath.split("
");
return parts[parts.length - 1];
}

You can also write this to get the same result

public String getOnlyFileName(String nameWithPath) {    String fileName = nameWithPath.substring(        nameWithPath.lastIndexOf(System.getProperty("file.separator")));    return fileName;}


With kind regards
F.-M. Jaeschke
 
This topic has 5 replies on 1 page.