Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Java beginner code not working in Ireport :(

  1. #1
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Java beginner code not working in Ireport :(

    I have member IDs and their date of births so I need to generate this final structure: member ID + First Capital letter of birth month +2 digit day of birth

    member ID: 123456
    DOB: 6/7/2011
    final result: 123456J07

    here are the fields in my Ireport:

    member ID: $F{localId} Expression Class: (java.lang.String)

    DOB: $F{person}.getDob() Expression Classjava.lang.string) Patttern: MM/dd/yyyy

    I am having issues with the cast to string error so can anyone help create the right code? here is my code below:

    CONCATENATE($F{localId},LEFT(new SimpleDateFormat("MM", Locale.ENGLISH).format($F{person}.getDob())),new SimpleDateFormat("dd").format($F{person}.getDob()) )

    Thanks for any help.

  2. #2
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java beginner code not working in Ireport :(

    SimpleDateFormat.format() expects a java.util.Date object or a java.lang.Number object, not a String. If you need to convert a String like "09/27/2017" into a Date object, that sounds like a job for SimpleDateFormat.parse() instead.

  3. #3
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner code not working in Ireport :(

    so how would I change this code or is there a better way? CONCATENATE($F{localId},LEFT(new SimpleDateFormat("MM", Locale.ENGLISH).format($F{person}.getDob())),new SimpleDateFormat("dd").format($F{person}.getDob()) )

    Thanks

    --- Update ---

    so I changed the coding to this: CONCATENATE($F{localId},LEFT(new SimpleDateFormat.parse($F{person}.getDob())),Simpl eDateFormat.parse("dd")($F{person}.getDob()))

    I am getting this error: Element type "topPen" must be declared.

  4. #4
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java beginner code not working in Ireport :(

    Sounds like you're past the initial ClassCastException and on to something else now. Are you building an XML document with this output? Is there an XSD or DTD somewhere that requires a "topPen" element to exist in the XML? I'm not at all familiar with Ireport, but I'd look around for that kind of thing.

  5. #5
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner code not working in Ireport :(

    yes it is an XML format definition. This is part of that format definition at the beginning if that will help?

    <?xml version="1.0" encoding="UTF-8" ?>
    <!-- Created with iReport - A designer for JasperReports -->
    <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport

  6. #6
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java beginner code not working in Ireport :(

    The structure of your XML document is beyond what I can help with. I cannot access that DTD file, nor do I know Ireport or JasperReports at all. What I can say is, your resulting XML document does not have an element called "topPen" and that DTD is requiring it to exist. For more help on that, I'd suggest creating a new question, with Ireport or JasperReports in the subject line, so people who are familiar with those tools can see your post and help.

    But I would like to point out that you should print out the result of your change. You're not getting what you expect by directly calling SimpleDateFormat.parse(). It probably looks more like "Fri Jan 07 00:06:00 EST 2011", not "J07" like you want. That's because SimpleDateFormat.parse() returns a java.util.Date object, and its toString() function does its own formatting. To get the individual fields (day, month, year, etc.) from a Date, use GregrorianCalendar and the Calendar constants like this:
    String dobString = "6/7/2011";
    Date dob = new SimpleDateFormat("MM/dd/yyyy").parse(dobString);
     
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(dob);
    int birthMonth = cal.get(Calendar.MONTH);
    int birthDay = cal.get(Calendar.DAY_OF_MONTH);
    The months are zero-based, so if birthMonth is 0 then it's January, 1 is February, etc. They do this so you can make a lookup table with your own date names:
    String[] monthNames = new String[] {
    	"J","F","M","A", // etc...
    };
    String monthName = monthNames[birthMonth];

  7. #7
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner code not working in Ireport :(

    I am still having the 'cast to string error' so will need to solve this before the 'topPen" issue. Thanks
    Last edited by jakehngo; September 28th, 2017 at 09:03 AM.

  8. #8
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java beginner code not working in Ireport :(

    Hi,

    I thought you got the ClassCastException resolved when you changed it to use SimpleDateFormat.parse() instead of format().

    CONCATENATE($F{localId},LEFT(new SimpleDateFormat.parse($F{person}.getDob())),Simpl eDateFormat.parse("dd")($F{person}.getDob()))

    Can you please try the following and post the output of the two print statements?

    Object dobObject = $F{person}.getDob();
    System.out.println("dobObject [" + dobObject.getClass().getName() +"]: " + dobObject);
    Date dobDate = new SimpleDateFormat("MM/dd/yyyy").parse(dobObject.toString());
    System.out.println("dobDate: " + dobDate);

    Once you have the Date object, use the GregorianCalendar to get the date formatted as "J07" like you need. In my last post, I showed you everything you need to do that. Once you have it formatted like "J07", the final result should look something like this:

    CONCATENATE($F{localId}, dobString);

    (Note that we're not using LEFT anymore. That function is no good for the kind of formatting you need here.)

  9. #9
    Junior Member
    Join Date
    Sep 2017
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java beginner code not working in Ireport :(

    so here is the errors that came back with this code: CONCATENATE($F{localId},LEFT(new SimpleDateFormat.parse($F{person}.getDob())),Simpl eDateFormat.parse("dd")($F{person}.getDob()))

    Errors were encountered when compiling report expressions class file:
    1. SimpleDateFormat.parse cannot be resolved to a type
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    <-------------------->
    2. Cannot make a static reference to the non-static method parse(String) from the type DateFormat
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    <-------------------------->
    3. Syntax error on token ")", , expected after this token
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    ^
    4. SimpleDateFormat.parse cannot be resolved to a type
    value = CONCATENATE(((java.lang.String)field_localId.getOl dValue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getOldValue()).getDob())), SimpleDateFormat.parse("dd")(((com.x2dev.sis.model .beans.SisPerson)field_person.getOldValue()).getDo b())); //$JR_EXPR_ID=27$
    <-------------------->
    5. Cannot make a static reference to the non-static method parse(String) from the type DateFormat
    value = CONCATENATE(((java.lang.String)field_localId.getOl dValue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getOldValue()).getDob())), SimpleDateFormat.parse("dd")(((com.x2dev.sis.model .beans.SisPerson)field_person.getOldValue()).getDo b())); //$JR_EXPR_ID=27$
    <-------------------------->
    6. Syntax error on token ")", , expected after this token
    value = CONCATENATE(((java.lang.String)field_localId.getOl dValue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getOldValue()).getDob())), SimpleDateFormat.parse("dd")(((com.x2dev.sis.model .beans.SisPerson)field_person.getOldValue()).getDo b())); //$JR_EXPR_ID=27$
    ^
    7. SimpleDateFormat.parse cannot be resolved to a type
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    <-------------------->
    8. Cannot make a static reference to the non-static method parse(String) from the type DateFormat
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    <-------------------------->
    9. Syntax error on token ")", , expected after this token
    value = CONCATENATE(((java.lang.String)field_localId.getVa lue()),LEFT(new SimpleDateFormat.parse(((com.x2dev.sis.model.beans .SisPerson)field_person.getValue()).getDob())),Sim pleDateFormat.parse("dd")(((com.x2dev.sis.model.be ans.SisPerson)field_person.getValue()).getDob())); //$JR_EXPR_ID=27$
    ^
    9 errors[COLOR="Silver"]

  10. #10
    Member
    Join Date
    Apr 2014
    Posts
    81
    Thanks
    3
    Thanked 6 Times in 6 Posts

    Default Re: Java beginner code not working in Ireport :(

    The first error message says, "SimpleDateFormat.parse cannot be resolved to a type." So check that line of code carefully and see if there's any misuse of SimpleDateFormat.

    You would also be well advised to clean up that CONCATENATE statement by preparing the variables before the call. That will help identify the cause of the other error messages. For example, something like this:

    String localId = (String)field_localId.getValue();
    com.x2dev.sis.model.beans .SisPerson sisPerson = (com.x2dev.sis.model.beans .SisPerson)field_person.getValue();
    java.util.Date dateOfBirth = (java.util.Date)sisPerson.getDob();
    String dobString = new SimpleDateFormat("MM/hh/dd").parse(dateOfBirth);
    String formattedDob = "???"; // To be determined
     
    CONCATENATE(localId, formattedDob);
    Last edited by BinaryDigit09; September 28th, 2017 at 05:28 PM.

Similar Threads

  1. Beginner Java code problems
    By return0 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2013, 11:52 AM
  2. Complete beginner (college student) - code not working!!
    By javanewbie085 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2013, 08:19 AM
  3. java ireport
    By aman_chauhan in forum Java Theory & Questions
    Replies: 0
    Last Post: July 4th, 2012, 01:46 PM
  4. Beginner for Java, need help with this code I wrote
    By SilentNite17 in forum What's Wrong With My Code?
    Replies: 30
    Last Post: February 14th, 2012, 07:01 PM
  5. Beginner pleasee help with java code
    By chuy525 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 24th, 2011, 06:38 PM

Tags for this Thread