Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (555)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  "Unclosed character class near index 0" ? [solved]  (Read 1467 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Posted 2014-01-19 20:46:40 »

Long story short, I'm making a class to read text files in a specific way. I'm storing a lot of information about my game (including enemy stats) in text files that I read in and parse appropriately. I am not writing that class, and getting a very biazzare error.

My centralized "parsing" involves taking a string and making two String[] arrays, one that contains everything in square brackets, and one that contains everything outside of square brackets.

For example, this string
1  
2  
3  
4  
5  
6  
7  
8  
NAME: [NAME]
FILE: [FILE]
TYPE: [TYPE1] [TYPE2]
COLOR: [COLOR]
HP: [HP]
PWR: [PWR]
DEF: [DEF]
SPD: [SPD]


Should be parsed, via the method "splitInBrackets" into:
1  
[NAME, FILE, TYPE1, TYPE2, COLOR, HP, PWR, DEF, SD]


Now, when I pass that exact string into my method, I get this error:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
Caused by: java.util.regex.PatternSyntaxException: Unclosed character class near index 0
[
^
   at java.util.regex.Pattern.error(Unknown Source)
   at java.util.regex.Pattern.clazz(Unknown Source)
   at java.util.regex.Pattern.sequence(Unknown Source)
   at java.util.regex.Pattern.expr(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 the line that says "String[] comp = ref.split("[");"

Why is this happening? (Method code below)
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
private String[] splitInBrackets(String ref){
      System.out.println(ref);
      String[] comp = ref.split("[");
      //split
     //NAME: [NAME] AGE: [AGE]
     //into
     //|NAME: |NAME] AGE: |AGE]
     //then for each component splits it further to get out the parts in the [ ]
     
      //will throw out of bounds exception if weird stuff isn't formatted with the brackets
     for(int i = 1; i < comp.length; i++){
         String[] dcomp = comp[i].split("]");
         comp[i] = dcomp[0];
      }
     
      return comp;
   }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #1 - Posted 2014-01-19 20:53:05 »

String.split(...)
takes a regex pattern as a parameter. Emo

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #2 - Posted 2014-01-19 20:55:31 »

I don't know anything about regex, I'm not gonna lie. But I was under the impression that "split" just returned an array of strings, cutting up your string using the argument as a divider

for example,

"A,B,C,D,E".split(",") = {"A", "B", "C", "D", "E"}

And that is actually true

If this ISN'T always the case, then how can I get the behavior I'm looking for?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 783
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2014-01-19 20:59:06 »

So, hilariously, you either have to write your own split function, or use the quirky StringTokenizer.

The Sun guys really screwed up with the Java 1.4 String class, but well, you'll be able to split your string also, using a regex-pattern, which requires double escaping, as Java doesn't support embedded regex - anyway, you'd end up with:
str.split("\\]")


 Emo

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline HeroesGraveDev

JGO Kernel


Medals: 246
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #4 - Posted 2014-01-19 20:59:25 »

String.split("\\[");


Edit: Ninja'd

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #5 - Posted 2014-01-19 21:01:49 »

str.split("\\]")


That's very weird. I did not know about that. Now it works though!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (29 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (38 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (24 views)
2014-08-16 06:20:21

Tekkerue (34 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (48 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!