Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

This should work in Groovy, but it doesn't--why?

 
Gregg Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using the latest version of Groovy (1.5.7). I was using Groovy to learn about formatting strings, and I was getting error messages were I shouldn't have. I searched the Internet for an answer, but I didn't find anything.

Here's some code from Sun's Java 1.5 doc pages; it works just as you would expect it in Java:

-----------------------------------------
import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;

public class Scratch {

public static void main(String[] args) {
Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
System.out.println(s);
// -> prints "Duke's Birthday: 5 23, 1995"
}
}
-----------------------------------------

Here's the code that I put into the GroovyConsole window:

-----------------------------------------
import java.util.Calendar;
import java.util.GregorianCalendar;
import static java.util.Calendar.*;
Calendar c = new GregorianCalendar(1995, MAY, 23);
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY", c);
System.out.println(s);

... and here's the output:

Exception thrown: groovy.lang.MissingPropertyException: No such property: tm for class: Script4

groovy.lang.MissingPropertyException: No such property: tm for class: Script4
at Script4.run(Script4:5)
-----------------------------------------

If you take out the import statements, you get a different error:

Exception thrown: groovy.lang.MissingPropertyException: No such property: MAY for class: Script5

groovy.lang.MissingPropertyException: No such property: MAY for class: Script5
at Script5.run(Script5:1)
-----------------------------------------

Any ideas why this is happening? Thanks.
 
Matthew Taylor
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this:



In your code, this: "Duke's Birthday: %1$tm %1$te,%1$tY" is parsed as a GString in Groovy, which means that Groovy will attempt to look through it to see if it needs to make any replacements, and one of the key characters it looks for is '$'. For example:



prints 'My name is Matt'. Groovy saw the '$'s in your GString and got confused, so you just had to escape out those characters.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Matthew Taylor:

Or you can use single quotes to prevent the GString behavior:
'Duke's Birthday: %1$tm %1$te,%1$tY'
 
Gregg Williams
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks from a beginner for both answers! --gw
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic