Android: How can I print a variable on eclipse console?

AndroidLogcat

Android Problem Overview


I wanted to print the value of a variable on the console for my debugging purpose, but System.out.println doesn't work.

Android Solutions


Solution 1 - Android

System.out.println and Log.d both go to LogCat, not the Console.

Solution 2 - Android

Window->Show View->Other…->Android->LogCat

Solution 3 - Android

Writing the followin code to print anything on LogCat works perfectly fine!!

int score=0;
score++;
System.out.println(score);

prints score on LogCat.Try this

Solution 4 - Android

I'm new to Android development and I do this:

  1. Create a class:

import android.util.Log;

public final class Debug{ private Debug (){}

public static void out (Object msg){
    Log.i ("info", msg.toString ());
}

}

When you finish the project delete the class.

  1. To print a message to the LogCat write:

Debug.out ("something");

  1. Create a filter in the LogCat and write "info" in the input "by Log Tag". All your messages will be written here. :)

Tip: Create another filter to filter all errors to debug easily.

Solution 5 - Android

I think the toast maybe a good method to show the value of a variable!

Solution 6 - Android

Ok, Toast is no complex but it need a context object to work, it could be MyActivity.this, then you can write:

Toast.maketext(MyActivity.this, "Toast text to show", Toast.LENGTH_SHORT).show();

Although Toast is a UI resource, then using it in another thread different to ui thread, will send an error or simply not work If you want to print a variable, put the variable name.toString() and concat that with text you want in the maketext String parameter ;)

Solution 7 - Android

toast is a bad idea, it's far too "complex" to print the value of a variable. use log or s.o.p, and as drawnonward already said, their output goes to logcat. it only makes sense if you want to expose this information to the end-user...

Solution 8 - Android

If the code you're testing is relatively simple then you can just create a regular Java project in the Package Explorer and copy the code across, run it and fix it there, then copy it back into your Android project.

The fact that System.out is redirected is pretty annoying for quickly testing simple methods, but that's the easiest solution I've found, rather than having to run the device emulator just to see if a regular expression works.

Solution 9 - Android

By the way, in case you dont know what is the exact location of your JSONObject inside your JSONArray i suggest using the following code: (I assumed that "jsonArray" is your main variable with all the data, and i'm searching the exact object inside the array with equals function)

    JSONArray list = new JSONArray(); 
	if (jsonArray != null){
		int len = jsonArray.length();
		for (int i=0;i<len;i++)
	    { 
			boolean flag;
			try {
				flag = jsonArray.get(i).toString().equals(obj.toString());
				//Excluding the item at position
			    if (!flag) 
			    {
			        list.put(jsonArray.get(i));
			    }
			} catch (JSONException e) {
				e.printStackTrace();
			}  
	    } 
	}
	jsonArray = list;
	

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuperCopView Question on Stackoverflow
Solution 1 - AndroiddrawnonwardView Answer on Stackoverflow
Solution 2 - AndroiddoemscheView Answer on Stackoverflow
Solution 3 - Androidpoojan9118View Answer on Stackoverflow
Solution 4 - AndroidGabriel LlamasView Answer on Stackoverflow
Solution 5 - AndroiddaisyView Answer on Stackoverflow
Solution 6 - AndroidSebasuView Answer on Stackoverflow
Solution 7 - AndroidMJBView Answer on Stackoverflow
Solution 8 - AndroidHaravikkView Answer on Stackoverflow
Solution 9 - AndroidAvi LevinView Answer on Stackoverflow