• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

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

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie sneak
Posts: 4727
Mac VI Editor Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Many thanks from a beginner for both answers! --gw
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic