Results 1 to 4 of 4
  1. #1
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default How can I prevent "found void but expected java.lang.String" ?

    Hello people,

    I wondered if anyone could be so kind as to shed some light on this probable schoolboy programming error please?

    The following method is part of a program for a basic calculator and I'm wondering what I should do to correct it to avoid the above error appearing.

    The getAndSetNumbers() method has been tested and successfully returns a boolean value, and the getOperator() method returns a String via a dialog box prompt (add, subtract, divide or multiply), so why doesn't the body of the if statement allocate the value of the resulting String to the variable 'operator' and how should I correct the code ?
    Many thanks in advance for your help.

    public void calculation()
    {
    if (this.getAndSetNumbers())
    {
    String operator = this.getOperator();
    }
    }

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    A couple of things:

    (minor point) Lose the "this" in front of the method invocations as these don't contribute anything useful.

    Post a SSCCE. The compiler does not lie. If it says "found void but expected java.lang.String" and points to the line that assigns to operator then I, for one, doubt your claim that "the getOperator() method returns a String".

    [edit]
    On closer reading, I'd expect something like "Type mismatch: cannot convert from void to String" if I tried to assign from a void method to a String, and "The method getAndSetNumbers(String) in the type Whatever is not applicable for the arguments ()" if the problem was with the boolean expression. So perhaps you could post the entire compiler message.
    Last edited by pbrockway2; 05-21-2009 at 03:06 AM.

  3. #3
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    Can you please post both the methods here.
    Tshegofatso Manakana
    a.k.a Untouchable

  4. #4
    trueblue is offline Member
    Join Date
    Mar 2009
    Posts
    57
    Rep Power
    0

    Default

    Hello everyone - again,

    I am delighted to announce that i have re-worked the code and it is now performing exactly as it should.
    I am still on a steep learning curve but I'm certainly enjoying the ride!

    Many thanks for your input.

Similar Threads

  1. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  2. Replies: 10
    Last Post: 01-29-2009, 08:00 AM
  3. Replies: 12
    Last Post: 12-05-2008, 05:37 AM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 1
    Last Post: 07-25-2007, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •