I have a strange problem in my application
Some users send me the error:
java.lang.ArrayIndexOutOfBoundsException
i , myself , have never experienced that error. Here is the code:
for(int i =0; i<length*2; i+=2) {
if(charsLeft[i/2]==1)
temp[i]=word[i/2];//IT HAPPENS HERE
....
length = word.length;
charsLeft = new int[length];
temp = new String[length*2];
When a users goes back to home screen , the application saves and later loads the data this way:
public Bundle saveState(Bundle bundle) {
synchronized (surfaceHolder) {
if (bundle != null) {
bundle.putStringArray("word",game.word.word);
bundle.putIntArray("charsLeft",game.word.charsLeft);
bundle.putInt("length",game.word.word().length());
bundle.putStringArray("temp",game.word.temp);
bundle.putCharArray("characters", game.word.characters);
...
}
}
return bundle;
}
public synchronized void restoreState(Bundle bundle) {
synchronized (surfaceHolder) {
mPaused = true;
if (bundle != null) {
game.word.word = bundle.getStringArray("word");
game.word.length = bundle.getInt("length");
game.word.init();
game.word.charsLeft = bundle.getIntArray("charsLeft");
game.word.temp = bundle.getStringArray("temp");
game.word.characters = bundle.getCharArray("characters");
...
}
}
}
Does anyone see where it goe
EDIT
full loop
for(int i =0; i<(length)*2; i+=2) {
if(charsLeft[i/2]==1)
temp[i]=word[i/2];
else
temp[i]="_";
temp[i+1]=" ";
}
Example word[0] = a word[1] = n .....
->"answer"
the loop diveds the word in spaces and the letter or if the letter isnt guessed yet by a _
ex. a n s _ e _
EDIT:
I think I found the error :
A thread was calling the loop while an other thread could reassign word to another value , and if the loop was called before the value of length was changed then you have an error.
I changed the loop to this
temp = new String[word.length*2];
for(int i =0; i<(word.length*2); i+=2) {
if(charsLeft[i/2]==1)
temp[i]=word[i/2];
Now lets hope its fixed
Forget your loop if all you want to do is divide the word by spaces or blanks. Just using split:
String[] words = word.split( "\s|_" );
That will spit the word into an array of words on either a space or an underscore.
Related
This is my code.
I wanna receive two values in Arduino from android app.
I can receive one value but I can't two values.
Here is my code.
I wanna send two values with Bluetooth.send().
How can I do that?
I use bluetoothSPP in android studio.
I wanna receive Go ,Back, Right, Left and seconds in arduino.
so If i receive Go and 2sec, my arduino rc car go for 2 sec.
//Arduino code
#include<SoftwareSerial.h>
#include <Servo.h>
SoftwareSerial BT(blueTx,blueRx);
Servo servo;
char input1;
char input2;
void loop() {
if(BT.available()){
if(Serial.available()){
Serial.println("------");
Serial.println(BT.read());
}
input1 = BT.read();
input2 = BT.read();
}
}
// Android code , i use bluetoothSPP and send method
blockStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!list.isEmpty()) {
delayHandler.postDelayed(new Runnable() {
#Override
public void run() {
while(!list.isEmpty()) {
String blockName = list.get(0);
num1 = inputNum.getText().toString();
list.remove(0);
adapter.notifyDataSetChanged();
if (blockName == "GO") {
Bluetooth.send("G", true);
Bluetooth.send(num1, true);
} else if (blockName == "BACK") {
Bluetooth.send("K", true);
Bluetooth.send(num1, true);
} else if (blockName == "LEFT") {
Bluetooth.send("L", true);
Bluetooth.send(num1, true);
} else if (blockName == "RIGHT") {
Bluetooth.send("R", true);
Bluetooth.send(num1, true);
}
// inputNum.setText(null);
}
}
}, 10);
}
I had similar problem in the problem , though I did'nt use bluetooth spp , but give this a shot , try to send byte array in send
I designed my input like a,b and convert this to byte array , in your case send it like G/K/L/R,num1 see the comma added , to convert this to byte array use getBytes() , in aurdino do the following :
void loop() {
if(Serial.available()){
int available = Serial.available();
for(int i=0; i< available; i++){
char a = Serial.read();
if(i < 1){
input1 = a;
}
if(i > 1){
int c =(int)a - 48;
input2 *= 10;
input2 += c;
}
}
}
Explanation skip comma which is second character(1) , first character is normal so add it to input1 , second one is number so extract ascii values starts from 48 and get the number from it.Input2 is of type int with initial value of 0 , Enjoy!
I am an android noobie. What I am trying to do is to make this String an ArrayList. This is done. When i Print it On (with tv.setText) , the result is what i need but in this if i have right below i cannot find the "1".
The result i want to have is to store the text between the noumbers inside another ArrayList but to go there i have to be able to read the strings from the ArrayList.
public class MainActivity extends AppCompatActivity {
String text = "1Hello12People22Paul22Jackie21Anna12Fofo2";
TextView tv;
List<String> chars = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.tv);
PrinThemNow();
}
public void PrinThemNow(){
chars = Arrays.asList(text.split(""));
tv.setText(toString().valueOf(chars));
for(int i=0;i<chars.size();i++){
if(toString().valueOf(chars.get(i)) == " 1"){
Toast.makeText(this,"I found One",Toast.LENGTH_LONG).show();
//This if is not working while the TV's text shows " 1"
}
}
}
}
First, just a tip, from string to char[] you can use
char[] chars = myString.toCharArray();
because it has no sense to save a char array as a string ArrayList
but now the problem. you have your string and you wanna print the text between the numbers.
It's not really clear what is your goal but lets try.
I will suppose you used the char[] because it's 10 times better and easier
case 1) you wanna print text betweens "1"s
//lets loop the chars
bool firstOneFound = false;
int firstOccurrence = -1;
int secondOccurrence = -1;
int i = 0;
for(char c : chars){
//is it equals to 1?
if(c.equals('1')){
//check if we are already after the first 1
if(firstOneFound){
//if yes, we found the final one
secondOccurrence = i;
break;
}
else{
//this is the first occurrence
firstOccurrence = i;
firstOneFound = true;
}
}
i++;
}
if(firstOccurrence != -1 && secondOccurrence != -1){
String myFinalString = myString.subString(firstOccurrence, secondOccurrence);
}
case 2) you wanna print all text except numbers (maybe with a space instead)
for(char c : chars){
//check if it's a number
if(c >= '0' && c <= '9'){
//replace the number with anything else
c = ' '; //if you wanna have it as a space
}
}
//print the final string
String myFinalString = new String(chars);
NOTE:
You can also use ArrayList of string, just replace ' with "
hope it helps
First of all, here is my code:
startRandomizing.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> values = new ArrayList<>();
int idList[] = new int[]{R.id.textBox1,R.id.textBox2,R.id.textBox3,
R.id.textBox4,R.id.textBox5,R.id.textBox6};
for(int id : idList){
if (findViewById(id) != null) {
values.add(((EditText) findViewById(id)).getText().toString());
}
}
String[] myItems = values.toArray(new String[values.size()]);
What I want to do is get rid of all the null values so that the length (myItems.length) of the array will depend on the value inside the text boxes 1 - 6.
(E.x - I have a string "Hello" in textBox1 and "World" in textBox2, and the rest empty. My desired output for myItems.length should be 2 since the remaining textBoxes do not have a value.)
This code outputs 6 (counts all the text boxes). Where have I gone wrong?
Change your code in the for loop as follows:
String s = ((EditText) findViewById(id)).getText().toString();
if(!TextUtils.isEmpty(s)){
values.add(s);
}
class oddevens{
public static void main(String[] args){
String A, Even, Odd;
int a,x,y;
x=0;
y=1;
A="sample text here";
a=A.length();
Even="";
Odd="";
for(int c=0;c==a;c++) {
if(c%2==0){
Even=Even+A.substring(x,y);
x=x+1;
y=y+1;
}else{
Odd=Odd+A.substring(x,y);
x=x+1;
y=y+1;
}
}
System.out.println("Even: "+Even+", Odd: "+Odd);
}
}
With this code, I try to divide String A by taking the 'even and odd' letters, so the output should look like this:
Even:sml ethr, Odd: apetx ee
But is not showing anything.
In your for loop, you have put c==a as the condition. Since a for loop runs for as long as that condition is met, your loop does not do anything (since c starts of as 0 and a starts of as the length of A.
It should be for(int c =0; c<a; c++)
A shorter solution to your problem would be something like this:
for(int c = 0; c < A.length(); c++){
if(c % 2 == 0{
Even += A.charAt(c);
}else{
Odd += A.charAt(c);
}
}
Your code will never run
for(int c=0;c==a;c++) {
.....
}
should be
for(int c=0;c < a;c++) {
.....
}
Anyway, you did not even tell us your problem.
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.