Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regex Compile Error

 
Jo Roe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Jo Roe
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20751
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all! Good suggestions, and now I have it working and understand Java a bit better!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic