Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Stripping A String Of Non-Digits

Status
Not open for further replies.

JDub2k

Programmer
Nov 23, 2004
4
CA
Hi,

I'm having a problem with a program I'm making for a class. Basically what it does is read in a file using JFileChooser and load the level in the file. The format for the filename is "levelX.lvl" where X is the level number.

For example,
level0.lvl
level1.lvl
...
level10.lvl
level11.lvl
etc...

Now based on the number in the filename, I need to set a variable curLevel to that number. That way the game knows that it is running that level.

I know how to save the filename to a string. But how can I strip that string of everything but the numbers?

Thanks in advance.
 
Code:
String filename = "level1.lvl";
String lvl = filename.split("\.")[0];
lvl = lvl.replace("level","");
int curLevel = Integer.parseInt(lvl);
I haven't tested it though...

--Chessbot

"Violence is the last refuge of the incompetent." -- Asimov, Foundation
 
I get an error using that saying:

Invalid escape sequence (valid ones are \b \t \n \f \r \" \' \\ )

Any ideas?
 
Okay, that works for level1.lvl through level9.lvl but as soon as the levels hit double digits, for example level13.lvl, I get the following error at runtime.

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 13
at a3Solution.TileGame.fileIn(TileGame.java:240)
at a3Solution.TileGame.access$0(TileGame.java:226)
at a3Solution.TileGame$3.actionPerformed(TileGame.java:95)
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.plaf.basic.BasicButtonListener.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)
 
You have an array of something, which you are trying to access at subscript 13, which is causing "java.lang.ArrayIndexOutOfBoundsException: 13".

The stack trace shows that it is not part of the split() code shown above, but something else. What is the line 240 in TileGame.java doing ?


--------------------------------------------------
Free Database Connection Pooling Software
 
Oh thanks. I found the problem. It was just an error in my level file. Everything works fine now. Thanks again!
 
Just another way to do it:
Code:
String filename = "level1.lvl";
String lvl = filename.replaceAll("\\D", "");
 
ishnid :

That gives an output of "1" - but the OP wanted "level1" ...

--------------------------------------------------
Free Database Connection Pooling Software
 
OP's question was to "strip that string of everything but the numbers" and doesn't mention keeping the string "level".
 
Sorry, my fault, must remember to read the question better !

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top