0

I can't figure out the proper way to output an int the logcat and the api document doesn't make sense to me.

I feel like this should do it:

package com.example.conflip;

import java.util.Random;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flip();
    }

    public int flip() {
        Random randomNumber = new Random();
        int outcome = randomNumber.nextInt(2);
        Log.d(outcome);
        return outcome;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

However I only get the error The method d(String, String) in the type Log is not applicable for the arguments (int)

Do I need to cast the int to a string? And if so, how?

Update:

Though all the solutions below do work, LogCat would not display output until I selected my hardware device in DDMS.

nipponese
  • 2,813
  • 6
  • 35
  • 51

6 Answers6

2

use Integer.toString(outcome) as you need String as parameter in Log

so overall Log.d(tag_name, Integer.toString(outcome));

here you can see details of Log.

stinepike
  • 54,068
  • 14
  • 92
  • 112
2

Add this line before onCreate method

private static final String TAG = "your activity name";

and now you in flip

Log.d(TAG, "outcome = " + outcome);
Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
  • I can't get around this error `Illegal modifier for parameter TAG; only final is permitted`. An explanation would be appreciated. – nipponese Mar 15 '13 at 06:24
  • I don't think the problem is a spelling error because I get the same error. – nipponese Mar 15 '13 at 06:27
  • TAG should be declare as a class member. If it is local take out private. – Hoan Nguyen Mar 15 '13 at 06:30
  • You do not call flip() anywhere in your code. maybe you mean to put flip() after setContentView – Hoan Nguyen Mar 15 '13 at 06:53
  • You're right. But also it turns out I didn't have a device selected for logging. http://stackoverflow.com/questions/2250112/why-doesnt-logcat-show-anything-in-my-android – nipponese Mar 15 '13 at 07:00
1

Use Log.d(String, String). The first string is a tag that will appear in the logcat- an easy identifier you can search for. The second is the message printed to log. To get the string for an int, use Integer.toString(value).

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
1

Use this:

public int flip() {
    Random randomNumber = new Random();
    int outcome = randomNumber.nextInt(2);
   Log.d("This is the output", outcome.toString());
    return outcome;
}
Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26
1
public class MainActivity extends Activity {

private String TAG = "MainActivity"; //-------------Include this-----------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        flip();   //----You miss this out perhaps-----
    }
public int flip() {
    Random randomNumber = new Random();
    int outcome = randomNumber.nextInt(2);
    Log.d(TAG, "Checking Outcome Value:" +outcome); //----Include this--------
    return outcome;
}

You can also change Log.d into Log.i (Information) , Log.w (Warning), Log.e (Error)

That depends on what type of message you want to display (Mainly different in color).

IssacZH.
  • 1,457
  • 3
  • 24
  • 54
  • As @Hoan Nguyen said, You did not initialize `flip();` in your onCreate. Therefore, it did not go through the Log.d process. Check my edited answer – IssacZH. Mar 15 '13 at 06:58
0

you should use string.valueof(integer) to get the output in log cat for eg.

int outcome = randomNumber.nextInt(2);
        Log.d("urtag",String.valueOf(outcome));
krishna
  • 142
  • 7
  • 12