Android - moving char array value to same char array - android

how am I going to move the value of a char array to the same char array? Here's a code:
Assuming ctr_r1=1 ,
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
letters[ctr_x] = letters[ctr_x];
}
sb.append(letters);
char[] lettersr1 = sb.toString().toCharArray();
sb1.append(lettersr1);
append the "letters", then convert it to string, then convert it to char array then make it as "lettersr1" value.
what im trying to accomplish is given the word EUCHARIST, i need to take the word HARIST out and place it on another array and call it region 1 (Porter2 stemming algorithm).
The code "ctr_X = (ctr_r1 + 2)" starts with H until T. The problem is I cannot pass the value directly that's why i'm trying to update the existing char array then append it.
I tried doing this:
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++)
{
lettersr1[ctr_x] = letters[ctr_x];
}
sb.append(lettersr1);
but my app crashes when i do that. Any help please. Thanks!

I don't understand what you're trying to do, but I can comment on your code:
letters[ctr_x] = letters[ctr_x];
This is a noop: it sets an array element value to the value it already has.
char[] lettersr1 = null;
for(int ctr_x = (ctr_r1 + 2) ; ctr_x < letters.length - 2 ; ctr_x++) {
lettersr1[ctr_x] = letters[ctr_x];
This obviously causes a NullPointerException, since you're trying to access an array which is null. You must initialize the array before being able to modify it:
char[] lettersr1 = new char[someLength];
Additional note: you should choose better names for your variables. The names should tell what the variable represents, and they should respect the Java naming conventions (no underscores in variable names, camelCase). ctr_x, ctr_r1 and lettersr1 don't mean anything.
EDIT:
I'm still not sure what you want to do, and why you don't simply use substring(), but here's how to transform EUCHARIST to HARIST:
char[] eucharist = "EUCHARIST".toCharArray();
char[] harist = new char[6];
System.arraycopy(eucharist, 3, harist, 0, 6);
String haristAsString = new String(harist);
System.out.println(haristAsString);
// or
char[] harist2 = new char[6];
for (int i = 0; i < 6; i++) {
harist2[i] = eucharist[i + 3];
}
String harist2AsString = new String(harist2);
System.out.println(harist2AsString);
// or
String harist3AsString = "EUCHARIST".substring(3);
char[] harist3 = harist3AsString.toCharArray();
System.out.println(harist3AsString);

May be so:
String str = "EUCHARIST";
str = str.substring(3);
and after toCharArray() or smth another

Related

How to pass edit text values to integer array in Android

I'm a beginner in Android.
I have created a edit text field where can I enter values from 0-9. Now, I have to get these values in an integer array.
example : entered values are 12345.
I need an array containing these values
int[] array = {1,2,3,4,5};
I need to know the way to do this. Kindly help.
Try this :
String str = edittext.getText.toString();
int length = str.length();
int[] arr = new int[length];
for(int i=0;i<length;i++) {
arr[i] = Character.getNumericValue(str.charAt(i));
}
You can use something like this:
int[] array = new int[yourString.length()];
for (int i = 0; i < yourString.length(); i++){
array[i] = Character.getNumericValue(yourString.charAt(i));
}

Is it possible to set array between two limits?

Currently I have a char array
char[] charArray = valueofOutput.toCharArray();
for(int i = 0; i < charArray.length; i+=2){
}
I am trying to make it read up to a certain point, collect up the results and then convert them to an integer using Integer.parseInt(String.valueOf(charArray[i]));
And then i want it to continue from where it left off. How do i do this?
Try this,
char[] charArray = valueofOutput.toCharArray();
for(int i = 0; i < charArray.length; i+=2){
char[i] = Integer.parseInt(String.valueOf(charArray[i]));
}

Android: Fill array up to a certain length

The situation
I have got different char[] arrays with a different amount of items. To avoid the "OutOfBounds"-Error while processing them I want to standardize them.
For Example:
My Array has following items: "5;9" --> What I want it to look like: "0,0,0,0,5,9" (fill up with "0"s to 6 items)
What I tried:
char[] myarray1 = mystring1.toCharArray();
...
for(int i=0; i<6; i++){
myarray1[i] = 0;
if(i<myarray1.length-1){
myarray1[i] = myarray1[i];
}else{
myarray1[i] = 0;
};
};
My code failed, because it evokes exactly that error...
I hope somebody can help me :)
Thanks!
The reason why your solution doesn't work is that you are using the same array for everything.
After char[] myarray1 = mystring1.toCharArray(); the length of myarray1 is 2, so you cannot simply assign entry 2,3,4 and 5 in your loop. Furthermore if you want the character ´0´ to be in the string, you need to surround your zeros with apostrophes.
You can fix your solution like this:
char[] myNewArray = new char[6];
int myarrayIndex = 0;
for(int i=0; i<6; i++)
{
if(i < (myNewArray.length - myarray1.length)) {
myNewArray[i] = '0';
} else {
myNewArray[i] = myarray1[myarrayIndex++];
};
};
System.out.println(myNewArray); //Will print out: 000059
An easier solution could be this:
myarray1 = String.format("%6s", mystring1).replace(' ', '0').toCharArray();
Firstly trying to fill 0's is not going to fix the error.
Secondly your logic is not right(assuming size as 6), change it to myString.length().
And I don't understand the point of myarray1[i] = myarray1[i];.
Anyways, every array with integer size is initialized by Zero's according to Java specs. On the other hand if you want to fill it with any other value, try Arrays.fill().
I think this function will accomplish what you're trying to do.
private static String formatString(String input)
{
String FORMATTED_STRING = "0,0,0,0,0,0";
int difference = FORMATTED_STRING.length() - input.length();
return FORMATTED_STRING.substring(0, difference) + input;
}

Array integer Android

ok so i create an array that has integers. The array displays five number from the min and max. How can i display all five numbers in a textview or edittext ? I tried:
nameofile.setText(al.get(x).toString());
but it only displays one?
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
String myString = TextUtils.join(", ", al);
lottonumbers.setText(myString);
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(0);
al.add(1);
al.add(5);
al.add(4);
al.add(3);
java.util.Collections.sort(al);//for sorting Integer values
String listString = "";
for (int s : al)
{
listString += s + " ";
}
nameofile.setText(listString);
You're currently only printing out one element (the one at index x). If you want to print them all in order, you can just join them using TextUtils.join().
Update: After seeing your edit, I think there's a better way to go about what you're trying to do. Instead of trying to pull the values one at a time, and update the list, why not just shuffle them, then use the above method?
Update 2: Okay, I think I finally understand your problem. Just a simple change, then.
ArrayList<Integer> al = new ArrayList<Integer>();
for (int i = minint; i <= maxint; i++)
al.add(i);
Random ran = new Random();
StringBuilder text = new StringBuilder(); // Create a builder
for (int i = 0; i < 5; i++) {
int x = al.remove(ran.nextInt(al.size()));
if (i > 0)
text.append(", "); // Add a comma if we're not at the start
text.append(x);
}
lottonumbers.setText(text);
al.get(x).toString() will only get the value at index "x". If you want to display all values, you need to combine all of the values from the array into a single string then use setText on that string.
You are only showing one number of your array in the TextView, you must to concat the numbers to see the others results like:
for(Integer integer : al) {
nameofile.setText(nameofile.getText() + " " + al.get(x).toString());
}
Then i think you can show all number in one String.

Array Being Overwritten with Last Index in Loop

I'm working on code that takes two arrays with strings (the strings are just sentences) and allocates them to classes which are held in another array (The Sentence class array shown below in the code).
So here's my problem. When popList() is called, the for loop runs through twice and works fine, putting the first index of addStrings and addTranslation into the first class in the array. However, when the loop indexes up and runs temp.sentence = addStrings[1] again, it OVERRIDES the first class's .sentence also. Then when temp.translations = addTranslations[1] runs again it OVERRIDES the first class's .translation.
So by the end of the loop, all of the arrays are filled with the same thing: the last index of addStrings and addTranslation. Every time it loops it overwrites all the indices before it with the index it's supposed to be putting in.
Anyone know what the problem is here? Thanks!
public class Sentence {
public String sentence;
public String translation;
Sentence() {
sentence = " ";
translation = " ";
}
}
private void popStrings() {
addStrings[0] = "我是你的朋友。"; addTranslations[0] = "I am your friend.";
addStrings[1] = "你可以帮助我吗?"; addTranslations[1] = "Could you help me?";
addStrings[2] = "我不想吃啊!"; addTranslations[2] = "I don't want to eat!";
}
//Fill Sentence array with string and translation arrays
private void popList() {
int i = 0;
Sentence temp = new Sentence();
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
}
You need to create new Sentence() inside the loop:
for(i = 0; i < addStrings.length && i < addTranslations.length ; i++) {
Sentence temp = new Sentence();
temp.sentence = addStrings[i];
temp.translation = addTranslations[i];
sentences[i] = temp;
}
Otherwise you set sentence and translation continuously in the same object.

Categories

Resources