aspose file tools*
The moose likes Beginning Java and the fly likes Regex Compile Error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Regex Compile Error" Watch "Regex Compile Error" New topic
Author

Regex Compile Error

Jo Roe
Greenhorn

Joined: Aug 26, 2011
Posts: 3
I am attempting to take a file of numbers of the format:

123456789
987654321 (etc.)

and place each digit in an array one at a time. I am getting a compile error (3, actually). Could someone help me understand what the errors are referring to? Let me know if you need to see more of my code (the program is a bit long and ugly at the moment).


...
line: 36


File: C:\Users\Jo\Projects\ProjectEuler\PE013.java [line: 36]
Error: C:\Users\Jo\Projects\ProjectEuler\PE013.java:36: illegal character: \92
File: C:\Users\Jo\Projects\ProjectEuler\PE013.java [line: 36]
Error: C:\Users\Jo\Projects\ProjectEuler\PE013.java:36: ';' expected
File: C:\Users\Jo\Projects\ProjectEuler\PE013.java [line: 36]
Error: C:\Users\Jo\Projects\ProjectEuler\PE013.java:36: illegal start of expression
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Welcome to the Ranch!

Regular expressions in Java must be written down as proper Java Strings. To start with, they must be enclosed in double quotes. Second, you must escape the \: "\\d".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jo Roe
Greenhorn

Joined: Aug 26, 2011
Posts: 3
When I change the expression to read
scan.next("\\d");

My errors change to:
File: C:\Users\Jo\Projects\ProjectEuler\PE013.java [line: 36]
Error: C:\Users\Jo\Projects\ProjectEuler\PE013.java:36: incompatible types
found : java.lang.String
required: int
File: C:\Users\Jo\Projects\ProjectEuler\PE013.java [line: 69]
Error: C:\Users\Jo\Projects\ProjectEuler\PE013.java:69: int cannot be dereferenced

I'm not sure what's going on with line 69 either, but I don't think it would be related.

Line 69:
sumString = carry.toString();
R Johnson
Greenhorn

Joined: Jan 05, 2010
Posts: 28

You need to look into the Integer Wrapper class this is where your problem lies.



When working with primitives and strings you can just concat the primitive int to a null string like so:

int i=0;
String s=i+"";

Otherwise you would have to use the Integer class wrapper of Java in order to use the toString() method



Your line 36 problem is pretty much the same you would need to use the Integer class wrapper to parse out strings to integers.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19544
    
  16

Or just use Scanner's nextInt method. As for converting anything to a String, there are a few overloaded static valueOf methods in the String class. You can use those to convert any value, including primitives and null, into Strings.
Jo Roe
Greenhorn

Joined: Aug 26, 2011
Posts: 3
Thanks all! Good suggestions, and now I have it working and understand Java a bit better!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Regex Compile Error
 
Similar Threads
Create a user class help
Craps Game Code
[newbie] See anything wrong here?
illegal space character