-7

In MainActivity class:

Log.i("Game ends! ","" + object1.winner()+ object2.winner());

In Game class:

public void winner() {
    if(this.points!=0) {
        Log.i("Winner is", this.objectName);
    }
}

Error is: '+ operator can no be applied to java.lang.String,void

As per the expertise of the given answers users I have done this.

public String winner() {
    if(this.points!=0) {
        return this.name;
    }
}

error: missing return statement

TylerH
  • 20,799
  • 66
  • 75
  • 101
Shad
  • 1,185
  • 1
  • 12
  • 27
  • The solution is fairly simple. Do **not** log anything in the `gettermethod()`, instead return what should be printed, and use that return value like `Log.i("Number stored in a given object","" + object.gettermethod());` – Tim Jan 05 '17 at 13:39
  • You said, **But then I need to return something! and whatever I return (0/ etc) it is also getting printed in Log catalog which I don't want. What to do?** If you don't want to print anything then just don't concat it to log. – Jagruttam Panchal Jan 05 '17 at 13:44
  • or simply create getter method with return type String and return "";(**Empty String**) – Jagruttam Panchal Jan 05 '17 at 13:46
  • 2
    ^ That would be silly – Tim Jan 05 '17 at 13:51
  • Are you perhaps unaware that you can just have `object.gettermethod() ;` as a statement to invoke a method when you don't care about its return? The method can then happily be declared as returning `void` (and, log its own messages to logcat if needed). – TripeHound Jan 05 '17 at 13:57
  • @TimCastelijns I edited and showing what I am trying to do – Shad Jan 05 '17 at 14:10
  • @user6177394 my first comment still applies – Tim Jan 05 '17 at 14:14
  • @Why is it saying missing return statement? & If this is a very noob question should I delete it? – Shad Jan 05 '17 at 14:48
  • @it is solved now, I didn't put 'else' statement as the other answerer pointed out. Thank your for your time. ( I know I shouldn't be thanking ) but after getting -6 it's worth it. – Shad Jan 05 '17 at 15:44

1 Answers1

0

The void keyword is not an object or a variable, all it does is specify that a method does not return anything. So the following line means that the function will return void (i.e. nothing):

public void doSomething() {}

Therefore, using the method Log.i() to print nothing is not possible. This method expects two String objects, which it can print to the logcat. The only reason you can use an int is because of the way Java handles String concatenation; it automatically converts any primitive type (boolean, int, float, etc.) to a String when using the + operator.


The reason the Log methods expects two String objects is simply to maintain legibility in logcat. The first parameter is meant to be a TAG variable, which is usually the name of the Class that is calling the method. This is usually done by creating a static value in each class:

private static final String TAG = MyActivity.class.getSimpleName();

In this way, you can use the Log methods as follows:

Log.i(TAG, "Log information…");

Which will result in a more uniform message written to logcat.


So, with this information, the question remains; what are you trying to output to logcat?

If you just want to output that the method was called, than simply call the Log statement before (or after) the method call:

Log.i(TAG, "doSomething() called");
doSomething();

Otherwise, if you actually expect your method to return a printable result, then you must return something other than void. Either a String, a primitive type, or an Object that can be printed using the toString() method.

Bryan
  • 14,756
  • 10
  • 70
  • 125
  • I have edited the question to show what I was really trying to do. – Shad Jan 05 '17 at 14:13
  • @user6177394, your edit has no impact on my answer. The error message you are seeing is stating that you cannot concatenate `void`; your methods must `return` *something* if you expect them to print something. – Bryan Jan 05 '17 at 14:19
  • did what you say. Missing return statement when using String. – Shad Jan 05 '17 at 14:25
  • why is it saying missing return statement? – Shad Jan 05 '17 at 14:47
  • @user6177394 I am assuming you changed the `void` to another type (`int`, `String`, etc.); in this case you need to call `return` [`int`, `String`, etc.] to tell the method what to return when called. I recommend you review some [Java training](https://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html) in order to get a handle on how functions work. – Bryan Jan 05 '17 at 14:56
  • I am trying to return object.name which is of String type. Why I am having return statement error – Shad Jan 05 '17 at 15:16
  • Ah, your `winner()` function also includes an `if` statement. If the function has a type other than `void` (in this case `String`), it *must* return something. If your `if` statement evaluates to `false` the `return` statement is skipped, resulting in an error. Therefore, you should add an `else` statement, in which case you can either `return` some other `String`, an empty `String` (`""`) or `null`. – Bryan Jan 05 '17 at 15:22
  • OMG! yes!. it was this 'if' statement causing all the trouble. I know i shouldn't be thanking in comments. But thank you so much! :) – Shad Jan 05 '17 at 15:42