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 2 of 2

Thread: The operator + is undefined for the argument type(s) String, void

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default The operator + is undefined for the argument type(s) String, void

    I'm writing a calendar program, and I'm trying to create buttons that add or subtract a year.

    I'm getting the error in the title for this code:

    private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			label1.setText("" + c.add(Calendar.YEAR, -1));
    			}

    If I listen to it and remove the "+" sign,

    private class ButtonListener implements ActionListener{
    		public void actionPerformed(ActionEvent event){
    			label1.setText(c.add(Calendar.YEAR, -1));
    			}

    I get the error message: The method setText(String) in the type JLabel is not applicable for the arguments (void)

    I've done everything I can think of and am completely lost. Does anyone know how to fix this error?


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: The operator + is undefined for the argument type(s) String, void

    The operator + is undefined for the argument type(s) String, void
    The message means that you are using the + operator on two arguments... The first is the string "" and the compiler is happy with that. The second is "void" and the compiler is not happy with that. The thing is that the add() method is void: it does not return anything, and hence there is nothing for the + operator to treat as its second argument. (Check its API documentation to see that)

    I've done everything I can think of and am completely lost. Does anyone know how to fix this error?
    Well, you fix the error by not including that line! The real question is what code do you use instead? And no-one can really answer that without knowing what it was you were trying to do with that line.

    Call add() on the Calendar instance c perhaps. But recognise that it will not return anything that can be used to set the label text. Rather it will change the calendar in some way. You are free to get() fields from the calendar, and perhaps one of them is what you want to display in the label.

    These fields are a bit cryptic and you might have better luck doing whatever it is you are doing with a DateFormat instance like SimpleDateFormat or using the date formatting capabilities of printf() and the String format() methods. There are examples of formatting a calendar instance as text in the Formatter API docs.

    -----

    Variables like c and label1 are not good. Write your code with a stranger in mind: that stranger might be yourself in 6 months. Choose variables which are descriptive of whatever it is they represent. yearLabel or something is/will be more meaningful than label1.
    Last edited by pbrockway2; July 7th, 2012 at 01:14 AM.

Similar Threads

  1. Type mismatch: cannot convert from void to ClassX.MethodX
    By RevMoses77 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 16th, 2012, 01:05 PM
  2. Replies: 3
    Last Post: October 28th, 2011, 05:42 PM
  3. Operator % cannot be applied to java.lang.String,int?
    By javarum in forum Java Theory & Questions
    Replies: 8
    Last Post: July 12th, 2011, 09:06 PM
  4. Finding a substring from an array within a string argument
    By sitruz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2011, 10:33 AM
  5. he operator / is undefined for the argument type(s) String, int
    By mtbr00x in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 8th, 2009, 09:34 PM