0

I am making a random character generate app that generates letters A-Z randomly. The application will create a customized service (extend from service) to keep generating a random character every one second from A to Z using a new thread (As instructed by my teacher we aren't supposed to use application main thread). The service can be bound to the MainActivity of the application, so that the randomly generated characters can be displayed on the screen. I managed to get the app working and I expected it to generate random letters, but it is generating random numbers instead. Layout: app layout image Code:

package com.example.RandomCharGen;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.util.Log;

import java.util.Random;


public class RandomCharacterService extends Service
{
private int myRandomCharacter;
private boolean isRandomGeneratorOn;

private final int MIN = 65;
private final int MAX = 90;

private final String TAG = "Random Char Service: ";

private final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

class RandomCharacterServiceBinder extends Binder{
    public RandomCharacterService getService()
    {
        return RandomCharacterService.this;
    }
}

private IBinder myBinder = new RandomCharacterServiceBinder();

@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    Log.i(TAG, "In OnStartCommand Thread ID is "+Thread.currentThread().getId());
    isRandomGeneratorOn = true;

    new Thread(new Runnable()
    {
        @Override
        public void run()
        {
            startRandomGenerator();
        }
    }
    ).start();

    return START_STICKY;
}

private void startRandomGenerator()
{
    while(isRandomGeneratorOn)
    {
        char alphabet = 'A';
        for (int i = 65; i < 90; i++)
        {
            try
            {
                Thread.sleep(1000);
                if(isRandomGeneratorOn)
                {
                    alphabet++;
                    myRandomCharacter = new Random().nextInt(MAX)+MIN;
                    Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+myRandomCharacter);
                }
            }
            catch(InterruptedException e)
            {
                Log.i(TAG, "Thread Interrupted.");
            }

        }

    }

}

private void stopRandomGenerator()
{
    isRandomGeneratorOn = false;
}

public int getRandomCharacter()
{
    return myRandomCharacter;
}

@Override
public void onDestroy()
{
    super.onDestroy();
    stopRandomGenerator();
    Log.i(TAG, "Service Destroyed.");
}

@Nullable
@Override
public IBinder onBind(Intent intent)
{
    Log.i(TAG, "In onBind ...");
    return myBinder;
}

}

Amma
  • 17
  • 1
  • 9

5 Answers5

4

You can do this to generate a random

Random rnd = new Random();    
Char randomChar = alphabet.charAt(rnd.nextInt(alphabet.length()));

It will store in randomChar variable a Char, so now you can print it.

 Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+randomChar);

YOUR PROBLEM

The problem is that your getRandomCharacter() returns an int, so that's why it prints a number.

public int getRandomCharacter()
{
    return myRandomCharacter; //This is an int not a char
}

If you have to follow this way, the only thing that you can do is to parse to char this int.

char randomChar = (char)getRandomCharacter();

And you can do the Log as explained before.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
1

This would have to be worked into your code....

But..

final String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// convert the string to a char array
char[] alphabetArr = alphabet.toCharArray();

This is doing the same thing as...['a','b','c' ... ]

// create an instance of random.
Random random = new Random();
//set the max number for the int to be the length of the string.
int randomInt = random.nextInt(alphabet.length());
//print it
System.out.println(randomInt + " :: " + alphabetArr[randomInt]);

I ran it and got this..

10 :: K

The way it works is it gets a random number from 0 - 25 (1-26) Then I take that random number and get the value at the index in my array.

You just need to now convert this to run every second....

letsCode
  • 2,774
  • 1
  • 13
  • 37
0

change the type of your

Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+myRandomCharacter);

to

 Log.i(TAG, "Thread ID is "+Thread.currentThread().getId() + ", Random character is "+(char)myRandomCharacter);

if your myRandomCharacter is type int, you must cast it to char when using it. this question has more info: Java - char, int conversions

Shawn
  • 403
  • 8
  • 38
0

You just need to cast it?

 int j = 65;
 char c = (char)j;
 System.out.println(c);

Prints A.

Joey
  • 70
  • 7
0

You can use in java Programming

import java.util.UUID;

id = UUID.randomUUID().toString();

This can You in Android Studio

public static Random RANDOM = new Random();

public static String randomString(int len) {
    StringBuilder sb = new StringBuilder(len);

    for (int i = 0; i < len; i++) {
        sb.append(DATA.charAt(RANDOM.nextInt(DATA.length())));
    }

    return sb.toString();
}