Results 1 to 4 of 4
- 05-21-2009, 02:40 AM #1
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
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();
}
}
- 05-21-2009, 02:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,540
- Rep Power
- 11
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.
- 05-21-2009, 09:00 AM #3
Member
- Join Date
- Apr 2009
- Location
- Pretoria, Gauteng, South Africa
- Posts
- 43
- Rep Power
- 0
Can you please post both the methods here.
Tshegofatso Manakana
a.k.a Untouchable ™
- 05-21-2009, 03:48 PM #4
Member
- Join Date
- Mar 2009
- Posts
- 57
- Rep Power
- 0
Similar Threads
-
final String currentWorld = "Java Forums"; String.format("Hello %s", currentWorld);
By mcfrog in forum IntroductionsReplies: 0Last Post: 04-02-2009, 07:02 PM -
[SOLVED] pls help :S . "Exception in thread "AWT-EventQueue-0" java.lang.NullPointerE
By ara in forum New To JavaReplies: 10Last Post: 01-29-2009, 08:00 AM -
xception in thread "main" java.util.NoSuchElementException: No line found
By Tenn in forum New To JavaReplies: 12Last Post: 12-05-2008, 05:37 AM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
Error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
By romina in forum New To JavaReplies: 1Last Post: 07-25-2007, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks