0

My problem seems easy, I am trying to encrypt a string in android.

What the program should do

The objective of the program is, put a word or a letter in textbox1, click on button encrypt and textbox2 should to show the symbol in the same position in second array of the letter that you put in.

For example I write letter A (array 1 [0]) must show symbol $ (array 2 [0]) if a click on ENCRYPT button, if I put a symbol and click on DECRYPT must show the letter which is equivalent in position of array

Need help. Sorry for grammar and edit question, first time and no English speaker, i tried to make it more simple.

Arreglo Class, just for declare 2 arrays.

package PaqueteArreglo;

public class Arreglo {
public String [] encrypt = new String[3];
public String [] decrypt = new String[3];
}

Main Activity

package com.example.encrypt;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import PaqueteArreglo.Arreglo;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

//Instance the class with the arrays
PaqueteArreglo.Arreglo ins = new Arreglo();
public Button button1,button2;
public EditText text1, text2;

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

  ins.encrypt[0] = "a";
  ins.encrypt[1] = "b";
  ins.encrypt[2] = "c";

  ins.decrypt[0] = "$";
  ins.decrypt[1] = "%";
  ins.decrypt[2] = "&";

}

private void initialize(){
  text1 = (EditText)findViewById(R.id.txtWord);
  text2 = (EditText)findViewById(R.id.txtResult);

  button1 = (Button)findViewById(R.id.btnEncrypt);
  button1.setOnClickListener(this);

  button2 =(Button)findViewById(R.id.btnDecrypt);
  button2.setOnClickListener(this);

}

@Override
public void onClick(View view) {
  String word = String.valueOf(this.text1.getText());

  if (view.getId() == R.id.btnEncrypt)
  {
     String demo = "";
     int lon = 0;
     lon = word.length();
     for (int i = 0; i < lon; i++ )
     {
        if (ins.encrypt[i].equalsIgnoreCase(String.valueOf(word.charAt(i))))
        {
           demo = demo + ins.decrypt[i];
           text2.setText(demo);
        }
     }
  }
}
}
4
  • You should probably use some kind of map like a HashMap to map your encryption character to it's matching decryption character. Also it is unclear what the specific problem you are having is. Could you clarify the question down a bit? Commented Apr 12, 2016 at 3:41
  • Thank you for answer, i edit the question for make an explanation more simple, and I do not know use (and for this purpose do not need) HashMap, i need resolve the problem with this method, thanks. Commented Apr 12, 2016 at 4:27
  • Please edit to show the Arreglo class Commented Apr 12, 2016 at 4:31
  • @RogerJimenez have you solved the problem? Commented Apr 18, 2016 at 5:23

1 Answer 1

1

There are two solutions to your problem using two different approach:

1- Using your defined structure (which is not optimized by the way) you need to put two nested loop for checking:

for (int i = 0; i < lon; i++ )
 {
    for(int j = 0; j<ins.encrypt.length;j++)
    {
       if (ins.encrypt[j].equalsIgnoreCase(String.valueOf(word.charAt(i))))
       {
          demo = demo + ins.decrypt[j];
       }
    }
 }

text2.setText(demo);

2- Using much better data structure like HashMap:

Change your Arreglo to the following:

public class Arreglo {
   public HashMap<Character, Character> encrypt = new HashMap<>();
   public HashMap<Character, Character> decrypt = new HashMap<>();
}

Now change the way you add your data like:

ins.encrypt.put('a','@');
ins.decrypt.put('@','a');
...

and finally do the encryption like:

for (int i = 0; i < lon; i++ )
{
   demo = demo + ins.encrypt.get(word.charAt(i));
}
text2.setText(demo);

The second implementation is more efficient

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.