permaculture playing cards*
The moose likes Groovy and the fly likes This should work in Groovy, but it doesn't--why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » Groovy
Bookmark "This should work in Groovy, but it doesn Watch "This should work in Groovy, but it doesn New topic
Author

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

Gregg Williams
Greenhorn

Joined: Oct 11, 2008
Posts: 2
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.


InfoML is a versatile format for storing and transforming "chunks" of content, adding metadata to them, and specifying arbitrary relationships between them. Check it out at InfoML.org!
Matthew Taylor
Rancher

Joined: Jun 13, 2004
Posts: 110
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.


Grails Consultant
http://dangertree.net
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4727

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'


A good workman is known by his tools.
Gregg Williams
Greenhorn

Joined: Oct 11, 2008
Posts: 2
Many thanks from a beginner for both answers! --gw
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: This should work in Groovy, but it doesn't--why?