lithium_js
lithium_js

Reputation: 151

Password generator on Android

I've just begun creating android applications, and the first idea was like "create random password generator for android just for practice".

I did, but it was like this.

Here comes the layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="ru.hashmap.myapplol.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:text="Your password should be here"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="16dp"
        android:id="@+id/editText"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:singleLine="true" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/progressBar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:indeterminate="true" />

    <Button
        android:text="generate!"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="62dp"
        android:id="@+id/button"
        android:layout_below="@+id/progressBar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="genPass"/>

    <SeekBar
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:max="15"
        android:progress="6"
        android:layout_marginTop="27dp"
        android:id="@+id/seekBar"
        android:layout_below="@+id/progressBar"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

and code here:

package ru.hashmap.myapplol;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.SeekBar;
import android.widget.TextView;

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void genPass(View view)
    {
        SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
        int seekValue = seek.getProgress()+1;
        Log.d("seekValue", seekValue+"");
        String[] dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
        String[] pass = new String[seekValue];
        Random rnd = new Random();
        for(int i = 0; i < seekValue; i++) {
            pass[i] = dict[rnd.nextInt(dict.toString().length())];
        }
        ((TextView) findViewById(R.id.editText)).setText(pass.toString());
        Log.d("pass", pass+"");
    }
}

How to make it work as expected?

Upvotes: 1

Views: 537

Answers (2)

Andrey  Kopeyko
Andrey Kopeyko

Reputation: 1566

You have defined pass as an array, but try to use it in scalar context:

String[] pass = new String[seekValue];
...
((TextView) findViewById(R.id.editText)).setText(pass.toString());

In this case toString() will dump the array, including elements type and start memory address.

You need to address a single element of this array, like

((TextView) findViewById(R.id.editText)).setText(pass[i].toString());

But I can't guess how to determine the correct index value.

Upvotes: 1

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

There is simply no issue , you are just printing the reference address of an array here setText(pass.toString()) which will look like something [Ljava.lang.String;@52e922

You need a StringBuffer or StringBuilder to append the chars and later display it as a string .

        SeekBar seek = (SeekBar) findViewById(R.id.seekBar);
        int seekValue = seek.getProgress()+1;
        Log.d("seekValue", seekValue+"");
        String[] dict = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
        Random rnd = new Random();
        StringBuilder str= new StringBuilder();
        for(int k = 0; k < seekValue; k++) {
            str.append(dict[rnd.nextInt(dict.length)]);
        }
        // don't use this , will display reference of array
       //((TextView) findViewById(R.id.editText)).setText(pass.toString()); 

       ((TextView) findViewById(R.id.editText)).setText(str.toString());
       // use this and avoid array in this case 

dict.toString again give you the reference address who's length will not be similar to [Ljava.lang.String;@52e922 mean this dict.toString().length() always give length around 26 so your password will include char always around this limit so to get actual length of your dict array i.e 62, use dict.length

Upvotes: 3

Related Questions