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]));
}
Related
I have an integer two-dimensional array and I want to store it in SharedPreferences. Could someone show me sample code how to do that? I suppose it is possible if I will convert it to string but it works for one-dimensional integer array. Maybe other way is exist to do that? Thanks.
As you said, converting to string that two-dimensional array will work. However if you face the same problem with more complex objects, you can always convert it to string using the GSON library.
Download: Link
Another post: Link
If you want to pass a two-dimensional array to string, you can iterate through it while storing it to a StringBuffer, as this example:
StringBuffer results = new StringBuffer();
String separator = ","
float[][] values = new float[50][50];
// init values
for (int i = 0; i < values.length; ++i)
{
result.append('[');
for (int j = 0; j < values[i].length; ++j)
if (j > 0)
result.append(values[i][j]);
else
result.append(values[i][j]).append(separator);
result.append(']');
}
result.toString(); //<- Save this in your preference
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;
}
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
In my app I have array size 400 elements. My task is Those elements are send to webservice for inserting.But it is not supported.So split the array into pieces and send to webservice.How is it
Something like this, perhaps?
String[] stringArray = new String[400];
//lets assume that the array has objects in it.
int index = 0;
for(int i = 0; i < stringArray.length; i++) {
String[] temp = new String[20];
for(int x = 0; x < 20) {
if(stringArray[index] != null) temp[x] = stringArray[index];
index++;
}
//The temp array is filled with objects from the other array, so send it to the webservice.
sendArrayToWebService(temp);
}
I'm tring to do a simple matrix initialization in android and I get Error: java.lang.ArrayIndexOutOfBoundsException. I'm trying this:
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id] = 0;
}
where id is a value between 1 and 5,sirid.length is a number that reflects number of images from different categorys. For example,I want for category 1 to have something like this :
vot[0][1]=0;
vot[1][1]=0;
vot[2][1]=0;
...etc
Where is my mistache?
try this
Integer id = Integer.valueOf(idcateg);
System.out.println("Id-ul e"+id);
vot = new Integer[sirid.length][id];
for (int i = 0; i < sirid.length; i++) {
vot[i][id-1] = 0;
}
array index start from 0
you set the size of the vot array to sirid.lengh by id but the array start index value from 0 to (size)(size value not included) see the for loop
I think because you initialize the array on id.
After that you call to vot[i][id] which then will alway be 1 too high.
f.e.
if you create new int[3][3]
you can only call positions 0,1 and 2
good luck
That is because id has been considered as Length of the String and you r trying to access the same element... but the last element is vat[i][id-1] but u r trying to get vat[i][id]
So better use this..
for (int i = 0; i < sirid.length; i++) {
for(int j = 0; j<id ; j++){
vot[i][j] = 0;
}
}