Sunday, 25 August 2013

ArrayIndexOutOfBounds in android app

ArrayIndexOutOfBounds in android app

i am developing an android app as our school project. I personally
contructed this code to the best of my ability but still i get
ArrayIndexOutOfBounds error from the logcat, and the app also forces to
stop. i have revised that code for days to no avail. please help. here
comes my program code...
package com.example.flames2;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText name1, name2, out;
int diffCount=0;
public String output="",holder="";
public int length1, length2, total;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mai...
name1 = (EditText)findViewById(R.id.editText1);
name2 = (EditText)findViewById(R.id.editText2);
Button go = (Button)findViewById(R.id.button1);
out = (EditText)findViewById(R.id.editText3);
go.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
String input1 = name1.getText().toString().toUpperCase()...
String input2 = name2.getText().toString().toUpperCase()...
length1 = name1.length();
length2 = name2.length();
total = length1+length2;
theMethod(input1, input2);
out.setText(input1+" "+input2+" are "+output);
input1="";
input2=""; //to clear the names
name1.setText(input1);
name2.setText(input2);
}
});
}
public void theMethod(String a, String b){
char letter;
char flames[] = { 'F','L','A','M','E','S' };
char input1[] = {},input2[] = {};
//Log.i("a.len",a.length()+" O");
the error at the logcat points here at these for loops...
for(int z=0;z<=length1;z++){
input1[z]=a.charAt(z);
}
for(int y=0;y<=length2;y++){
input2[y]=b.charAt(y);
}
if(length1>length2) {
for( int i = 0; i <= length1; i++ ) {
for( int j = 0; j <= length2; j++ ) {
letter = input1[i];
if( letter == input2[j] ) {
total--;
break;
}
}
}
}
else if(length1<length2){
for( int i = 0; i <= length2; i++ ) {
for( int j = 0; j <= length1; j++ ) {
letter = input2[i];
if( letter == input1[j] ) {
total--;
break;
}
}
}
}
else if(length1==length2){
for( int i = 0; i <= length1; i++ ) {
for( int j = 0; j <= length2; j++ ) {
letter = input1[i];
if( letter == input2[j] ) {
total--;
//break;
}
}
}
}
output += flames[total];
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.mai... menu);
return true;
}
}

No comments:

Post a Comment