SuperCop
SuperCop

Reputation: 937

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

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

Upvotes: 90

Views: 161441

Answers (9)

Avi Levin
Avi Levin

Reputation: 1868

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;

Upvotes: 0

drawnonward
drawnonward

Reputation: 53669

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

Upvotes: 117

Sebasu
Sebasu

Reputation: 31

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 ;)

Upvotes: 1

poojan9118
poojan9118

Reputation: 2373

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

Upvotes: 6

Haravikk
Haravikk

Reputation: 3290

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.

Upvotes: 0

Gabriel Llamas
Gabriel Llamas

Reputation: 18437

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.

2) To print a message to the LogCat write:

Debug.out ("something");

3) 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.

Upvotes: 6

doemsche
doemsche

Reputation: 922

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

Upvotes: 26

MJVDM
MJVDM

Reputation: 3963

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...

Upvotes: 0

daisy
daisy

Reputation: 66

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

Upvotes: 3

Related Questions