Convert each character of string into integer - android

I have hard coded 'a to z' integers with their respective integer/number values.
like
int a=0, b=1, c=2, d=3, e=4, f=5, g=6;
Now i have an edit text which can take input like abcdefg. I want to convert each character of string into int so that i can get
0123456 in return of abcdefg
Is that possible? please help. Thanks in advance.

For this its better to use hashmap
delecare values in hash map as like below
HashMap<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 0);
map.put("b", 1);
map.put("c", 2);
map.put("d", 3);
//....
map.put("z",25);
String s1 ="abcdefg";//String from edit text
char[] sa = s1.toCharArray();//converting to character array.
String str ="";
for(int i=0;i<sa.length;i++)
{
str = str+(map.get(Character.toString(sa[i])));
}
System.out.println(str);//Here str show the exact result what you required.

Its very simple . First you have to get string from edittext then convert it to string array and in for loop compare it.
String convertedText;
String str = editText.getText().toString();;
char[] ch = str.toCharArray();
for (char c : ch)
{
System.out.println(c);
if(c == 'a')
convertedText = convertedText + "1";
// Coding to compare each character
}

this can be done like this
ASCI value of a is 97 to get the value 0 subtract 97 so..
IN C
.......
char *insert = "abcdefg";
for (int i = 0; i < 7; ++i)
int value = ((digit_to_int(insert[i]))-97)+1
printf("%d", value );
.........
int digit_to_int(char d)
{
char str[2];
str[0] = tolower(d);
str[1] = '\0';
return (int) strtol(str, NULL, 10);
}
you can attain this

try using this
String text = "hitesh";
char[] ch = text.toCharArray();
StringBuilder sb = new StringBuilder();
for(char cd :ch)
{
int n = cd-'a';
sb.append(n);
}
System.out.println(sb.toString());

if you intend to modify the input in the edit text, i.e. when the user inters 'a' he see '1' instead..Try overriding addTextChangedListener use onTextChanged (if you want to modify it while the user is writing) or afterTextChanged (if you want to modify it after the user is done):
editText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count){
}
});

Related

How to capitalize letters in editText?

i have simple Edittext and when I'm going to change input letters in im setting listener new textWatcher and it's onTextChanged() method like:
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("qwer", "onTextChanged: " + s + " " + start + " " + before + " " + count);
String originalText = s.toString();
int originalTextLength = originalText.length();
int currentSelection = textHeading.getSelectionStart();
StringBuilder sb = new StringBuilder();
boolean hasChanged = false;
for (int i = 0; i < originalTextLength; i++) {
char currentChar = originalText.charAt(i);
if (isAllowed(currentChar) && i < 21) {
sb.append(currentChar);
} else {
hasChanged = true;
textHeading.setError("Please insert current letters");
}
}
if (hasChanged) {
String newText = sb.toString();
textHeading.setText(capitalize(newText));
textHeading.setSelection(currentSelection);
}
}
endless cycle begins when i'm setting validated data back to the edittext becouse it calls method ontextCahnged() again. so my goal is dynamically change input letters and i have to capitalize it. I know there is more the easiest way to do it. but i need to do by this way.
You problem is with TextWatcher not with the logic what you are writing. Below Code block is causing the issue (endless cycle begins when i'm setting validated data back to the edittext becouse it calls method ontextCahnged() again)
if (hasChanged) {
String newText = sb.toString();
textHeading.setText(capitalize(newText)); // <<<<<< This line is culprit which is calling Watcher's method again and again.
textHeading.setSelection(currentSelection);
}
To handle this issue, you need to do below steps
Remove Watcher from EditText
Set text
Add Watcher to EditText.
For more information read How can I change the EditText text without triggering the Text Watcher?
There is many ways to do that:
1- Using common Utils
Library: http://commons.apache.org/proper/commons-lang/
StringUtils.capitalize(..)
2- By Custom method
public static String upperCaseFirst(String value) {
// Convert String to char array.
char[] array = value.toCharArray();
// Modify first element in array.
array[0] = Character.toUpperCase(array[0]);
// Return string.
return new String(array);
}
3- From apache Common
Library: http://commons.apache.org/proper/commons-lang/
WordUtils.capitalize(java.lang.String)
Now you can assign that string to your input box.
You can set the input type (of EditText) to TYPE_CLASS_TEXT | TYPE_TEXT_FLAG_CAP_CHARACTERS.
OR
Set android:inputType="textCapSentences" on your EditText.
You can follow this link https://developer.android.com/reference/android/text/InputFilter.AllCaps
You can use following InputFilter.AllCaps
or this
android:inputType="textCapCharacters"
Why not use a flag ? I've modified your code by adding a boolean setManually flag.
boolean setManually = false;
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Log.d("qwer", "onTextChanged: " + s + " " + start + " " + before + " " + count);
if (setManually) {
setManually = false;
return;
}
String originalText = s.toString();
int originalTextLength = originalText.length();
int currentSelection = textHeading.getSelectionStart();
StringBuilder sb = new StringBuilder();
boolean hasChanged = false;
for (int i = 0; i < originalTextLength; i++) {
char currentChar = originalText.charAt(i);
if (isAllowed(currentChar) && i < 21) {
sb.append(currentChar);
} else {
hasChanged = true;
textHeading.setError("Please insert current letters");
}
}
if (hasChanged) {
String newText = sb.toString();
setManually = true;
textHeading.setText(capitalize(newText));
textHeading.setSelection(currentSelection);
}
}
You can just simply use in XML
android:inputType="textCapCharacters"
no need to write code for capitlize letter.
Try like this:
String originalText = s.toString().toUpperCase();
or
if (hasChanged) {
String newText = sb.toString().toUpperCase();
textHeading.setText(newText);
textHeading.setSelection(currentSelection);
}

How to get text value and show in text

eText.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
int wordvalue = 0;
int numvalue = 0;
for(int i=0; i<s.length(); i++) {
wordvalue += this.calcCharValue(s.charAt(i));
numvalue = this.calcCharValue(s.charAt(i));
String userEntered = nText.getText().toString();
userEntered = userEntered + numvalue;
tv.setText(String.valueOf(wordvalue));
nText.setText(String.valueOf(userEntered));
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
protected int calcCharValue(char a) {
Character ch = new Character(a);
if(ch >= 'A' && ch <= 'Z') { // a=1, b=2, c=3, ..., z=26
Character ca = new Character('A');
return ch.charValue() - ca.charValue() + 1;
}
I am try to create an app that get the text value from the word.
for example
a = 1, b = 2,
I am able to add them like abc = 6, abcd = 10
Now what my problem is I try this value as
abc = 123 and abcde = 12345
but what I get is
abc = 112123 and abcde = 1 12 123 1234 12345
It repeat the every character before and then add next value so how can I handle this repeated problem.

String encryption in editext

I have got an EditText in my Android app where you can type in some text. What I want to do is change only first character to another one (some kind of enciphering). Therefore I first have to read every single character.
How can I do that?
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
String s=ed1.getText().toString();
abc(s);
}
public void abc(String s){
//get your string
String str = s;
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
strChars[1] = 'x';
str = String.valueOf(strChars);
ed1.setText(str);
}
Something like this would work:
//get your string
String str = "whatevertextyouwanthere";
//turn it into an array of chars
char[] strChars = str .toCharArray();
//set array at position 1 to an x
//Check to make sure the length is above -
if (str.length() != 0)
{
//Check that we have a value of some sorts in the char array
if (strChars[0] != ''
{
//replace the char at place 0 with X
strChars[0] = 'x';//this might need to be 1
}
}
//turn the array back into a string
str = String.valueOf(strChars);
Ideally you should have this within a function for ease of use and re-usability. It should return whatever the value of str is.
Post this in your onCreate:
ed1.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
abc(ed1.getText().toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
});
}
The above was just some code I wrote really quickly but should do what you need it to do. You need to have a listener watching for changes to the edit text.

s.replace() not working in Android EditText in TextWatcher

I have an EditText in which the user should input a number including decimals and i want a thousand separator automatically added onto the input number
I tried a couple of other methods but some do not allow floating point numbers
so i came up with this code which works well only that the string input is not being edited in realtime to one with possible thousand separators and the errors seem to stem from the s.replace();
am2 = new TextWatcher(){
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void afterTextChanged(Editable s) {
if (s.toString().equals("")) {
amount.setText("");
value = 0;
}else{
StringBuffer strBuff = new StringBuffer();
char c;
for (int i = 0; i < amount2.getText().toString().length() ; i++) {
c = amount2.getText().toString().charAt(i);
if (Character.isDigit(c)) {
strBuff.append(c);
}
}
value = Double.parseDouble(strBuff.toString());
reverse();
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.#######");
s.replace(0, s.length(), nf2.format(value));
}
}
};
I think you should use setText() of the edittext once the text is changed
do something like this
editext.setText(s); after you replace your string
simple s = s.replace(0, s.length(), nf2.format(value));
That is variable s need to be assigned.

How can I implement digit grouping input mask using InputFilter?

I am using InputFilter class to make a masked EditText supporting digit grouping. For example when the user inserts" 12345" I want to show "12,345" in EditText. How can I implement it?
This is my incomplete code:
InputFilter IF = new InputFilter() {
#Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!Character.isLetterOrDigit(source.charAt(i))) {
return "";
}
}
if (dest.length() > 0 && dest.length() % 3 == 0)
{
return "," + source;
}
return null;
}
};
edtRadius.setFilters(new InputFilter[] { IF });
Is there any other way to implement this kind of input mask?
This an improvement on the response from #vincent. It adds checks on deleting spaces in a number in the format 1234 5678 9190 so when trying to delete a space it just moves the cursor backon character to the digit before the space. It also keeps the cursor in the same relative place even if spaces are inserted.
mTxtCardNumber.addTextChangedListener(new TextWatcher() {
private boolean spaceDeleted;
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// check if a space was deleted
CharSequence charDeleted = s.subSequence(start, start + count);
spaceDeleted = " ".equals(charDeleted.toString());
}
public void afterTextChanged(Editable editable) {
// disable text watcher
mTxtCardNumber.removeTextChangedListener(this);
// record cursor position as setting the text in the textview
// places the cursor at the end
int cursorPosition = mTxtCardNumber.getSelectionStart();
String withSpaces = formatText(editable);
mTxtCardNumber.setText(withSpaces);
// set the cursor at the last position + the spaces added since the
// space are always added before the cursor
mTxtCardNumber.setSelection(cursorPosition + (withSpaces.length() - editable.length()));
// if a space was deleted also deleted just move the cursor
// before the space
if (spaceDeleted) {
mTxtCardNumber.setSelection(mTxtCardNumber.getSelectionStart() - 1);
spaceDeleted = false;
}
// enable text watcher
mTxtCardNumber.addTextChangedListener(this);
}
private String formatText(CharSequence text)
{
StringBuilder formatted = new StringBuilder();
int count = 0;
for (int i = 0; i < text.length(); ++i)
{
if (Character.isDigit(text.charAt(i)))
{
if (count % 4 == 0 && count > 0)
formatted.append(" ");
formatted.append(text.charAt(i));
++count;
}
}
return formatted.toString();
}
});
In case you're still searching, I ran into this problem the last day, and found that using a TextWatcher is the best (still not really good) option. I had to group digits of credit card numbers.
someEditText.addTextChagedListener(new TextWatcher()
{
//According to the developer guide, one shall only edit the EditText's
//content in this function.
#Override
public void afterTextChanged(Editable text)
{
//You somehow need to access the EditText to remove this listener
//for the time of the changes made here. This is one way, but you
//can create a proper TextWatcher class and pass the EditText to
//its constructor, or have the EditText as a member of the class
//this code is running in (in the last case, you simply have to
//delete this line).
EditText someEditText = (EditText) findViewById(R.id.someEditText);
//Remove listener to prevent further call due to the changes we're
//about to make (TextWatcher is recursive, this function will be
//called again for every change you make, and in my experience,
//replace generates multiple ones, so a flag is not enough.
someEditText.removeTextChangedListener(this);
//Replace text with processed the processed string.
//FormatText is a function that takes a CharSequence (yes, you can
//pass the Editable directly), processes it the way you want, then
//returns the result as a String.
text.replace(0, text.length(), FormatText(text));
//Place the listener back
someEditText.addTextChangedListener(this);
}
#Override
public void beforeTextChaged(CharSequence s, int start, int count, int after)
{
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
});
My formatting function for the credit card numbers looked like this:
String FormatText(CharSequence text)
{
StringBuilder formatted = new StringBuilder();
int count = 0;
for (int i = 0; i < text.length(); ++i)
{
if (Character.isDigit(text.charAt(i)))
{
//You have to be careful here, only add extra characters before a
//user-typed character, otherwise the user won't be able to delete
//with backspace, since you put the extra character back immediately.
//However, this way, my solution would put a space at the start of
//the string that I don't want, hence the > check.
if (count % 4 == 0 && count > 0)
formatted.append(' ');
formatted.append(text.charAt(i));
++count;
}
}
return formatted.toString();
}
You might have to mind other issues as well, since this solution actually rewrites the EditText's content every time a change is made. For example, you should avoid processing characters you inserted yourself (that is an additional reason for the isDigit check).
use simple function:
public String digit_grouping(String in_digit){
String res = "";
final int input_len = in_digit.length();
for(int i=0 ; i< input_len ; i++)
{
if( (i % 3 == 0) && i > 0 )
res = "," + res;
res = in_digit.charAt(input_len - i - 1) + res;
}
return res;
}

Categories

Resources