• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Storing each item in array to variable fails

Ok so I have this problem where I store info to an internal file using fileOutputStream() and then I start another intent and pull that information using the fileInputStream() method. I then parse the inputStr for indexes of ";" (those are my delimeters). I then do a inputStr.substring(0, index) and store that in a section of an array (infoArray). I loop until all ";" (fields) have been extracted and stored in the array. I then put each item from the array into a variable. This is where the program errors out because I can comment just this section out and the program works flawelessly. Can anyone help? code below.


public
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.game);



String FILENAME = "monster";
String inputStr = "";


String[] infoArray =


new String[4];

String info;
String inputStr1 = "";
int index;



byte[] byteArray = newbyte[102400];
int bytesRead = 0;


try {

FileInputStream fstream = new FileInputStream(FILENAME);
while((bytesRead = fstream.read(byteArray)) != -1) {
inputStr = new String(byteArray,0,bytesRead);

}
inputStr1 = inputStr;

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}




for (int i = 0; i == 4; i++) {


index = inputStr1.indexOf(";");
info = inputStr1.substring(0, index);
inputStr1 = inputStr1.substring(index, inputStr1.lastIndexOf(";"));
infoArray = info;
}



TextView tvMonsterID1 = (TextView) this.findViewById(R.id.tvMonsterID1);
TextView tvMonsterHealth1 = (TextView) this.findViewById(R.id.tvMonsterHealth1);
TextView tvMonsterName1 = (TextView)this.findViewById(R.id.tvMonsterName1);
TextView tvMonsterAtk1 = (TextView)this.findViewById(R.id.tvMonsterAtk1);
TextView tvMonsterDef1 = (TextView)this.findViewById(R.id.tvMonsterDef1);
ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);



if (infoArray[1] == "Shewanadon") {
imgMonsterPic1.setImageResource(R.drawable.two);
} else {
imgMonsterPic1.setImageResource(R.drawable.icon);
}


/**--------This is where the code craps out------------------


String ID = infoArray[0];
String Health = infoArray[1];
String Name = infoArray[2];
String atk = infoArray[3];
String def = infoArray[4];
---------------------------------------------------------*/




tvMonsterID1.setText("ID: " + ID);
tvMonsterHealth1.setText("Health : " + Health);
tvMonsterName1.setText("Name: " + Name);
tvMonsterAtk1.setText("Attack: " + atk);
tvMonsterDef1.setText("Defense: " + def);

 
If you're using Eclipse, you should be able to go to the "Debug" perspective and take a look at the "LogCat" window while your program executes. I gave the same description a few posts back - check it out for a picture of what I'm talking about.

And, if that doesn't work, then try manually putting out an error message in the log:
Code:
[SIZE=2]try {[/SIZE]
[SIZE=2]  String ID = infoArray[0];[/SIZE]
[SIZE=2]  String Health = infoArray[1];[/SIZE]
[SIZE=2]  String Name = infoArray[2];[/SIZE]
[SIZE=2]  String atk = infoArray[3];[/SIZE]
   [SIZE=2]String def = infoArray[4];[/SIZE]
[SIZE=2]} catch (ArrayIndexOutOfBoundsException e) {[/SIZE]
[SIZE=2]   Log.e("<Your Program>", "Yup, an ArrayIndexOutOfBoundsException was thrown. Details: " + e.toString());[/SIZE]
[SIZE=2]} catch (Exception e) {[/SIZE]
[SIZE=2]   Log.e("<Your Program>", "A general Exception was caught. Details: " + e.toString());[/SIZE]
[SIZE=2]}[/SIZE]

Let us know how that goes, and we can proceed from there!

--Boogs
 
  • Like
Reactions: jdizzle1988
Upvote 0
It looks like you're trying to access 5 elements of a 4 element array.

Code:
[I][SIZE=2]String[] infoArray =[/SIZE][/I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[4];
. . .
[/SIZE][LEFT][SIZE=2][COLOR=red]/**--------This is where the code craps out------------------[/COLOR][/SIZE] 
 
[LEFT][SIZE=2]String [U]ID[/U] = infoArray[0];[/SIZE]
[SIZE=2]String Health = infoArray[1];[/SIZE]
[SIZE=2]String Name = infoArray[2];[/SIZE]
[SIZE=2]String atk = infoArray[3];[/SIZE]
[SIZE=2]String def = infoArray[4];[/SIZE]
[SIZE=2][COLOR=red]---------------------------------------------------------*/[/COLOR][/SIZE][/LEFT]
 . . .
 [/LEFT]


You can only use infoArray[0] - infoArray[3].
Using infoArray[4] will make it crash.

Mark

p.s.
for future reference the code will be easier to read if you post it inside CODE tags (The # button in the toolbar), so the indentation is preserved.
Some of your code looks a bit odd, but it would be easier to check if the code was inside CODE tags.


 
  • Like
Reactions: jdizzle1988
Upvote 0
markb++

Nothing makes me want to read your code less than not having code tags around your code.

Even though you know the cause of the problem, it would benefit you to learn how to properly debug your programs using Eclipse. I recommend taking a little tour of the Debug perspective to gain some familiarity.

Cheers,
Boogs
 
Upvote 0
Ok thanks guys Ill remember that code thing I was looking for that when I posted the code but I couldnt find it. I looked at the logcat over the weekend I got this (this is after I changed the array bounds):

Code:
07-06 15:53:20.075: ERROR/AndroidRuntime(10244): Uncaught handler: thread main exiting due to uncaught exception
07-06 15:53:20.115: ERROR/AndroidRuntime(10244): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.monster/com.monster.game}: java.lang.StringIndexOutOfBoundsException

Reposted the changed code Im stumped on this and I know it has to be something easy.

Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Integer [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]size[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = 4;[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]game[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/LEFT]
 
 
[LEFT][SIZE=2]String FILENAME = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"monster"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String inputStr = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/**[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]try {[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f5fbf]FileInputStream [U]fos[/U] = openFileInput(FILENAME);[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f5fbf]fos.read(inputStr.getBytes());[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f5fbf]fos.close();[/COLOR][/SIZE][/LEFT]
[/COLOR][/SIZE]
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]} catch (IOException e) {[/COLOR][/SIZE][/LEFT]
 
[SIZE=2][COLOR=#3f5fbf]// [/COLOR][/SIZE]
[LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf] Auto[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#7f7f9f][SIZE=2][COLOR=#7f7f9f]-[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]generated catch block[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]

[SIZE=2][COLOR=#3f5fbf][LEFT][SIZE=2][COLOR=#3f5fbf]e.printStackTrace();[/COLOR][/SIZE]
[LEFT][SIZE=2][COLOR=#3f5fbf]}[/COLOR][/SIZE]
[SIZE=2][COLOR=#3f5fbf]*/[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[/LEFT]
[SIZE=2][COLOR=#3f5fbf]
[/COLOR][/SIZE]

[LEFT][SIZE=2]String[] infoArray = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String[5];[/SIZE]
[LEFT][SIZE=2]String info;[/SIZE]
[SIZE=2]String [U]inputStr1[/U] = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] index;[/SIZE][/LEFT]
[/LEFT]

 
 
 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][] byteArray = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]byte[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][102400]; [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] bytesRead = 0; [/SIZE][/LEFT]
[/LEFT]

 
 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE][/LEFT]

 

[LEFT][SIZE=2]FileInputStream fstream = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FileInputStream(FILENAME); [/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]((bytesRead = fstream.read(byteArray)) != -1) { [/SIZE]
[SIZE=2]inputStr = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String(byteArray,0,bytesRead); [/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]}[/SIZE]
[LEFT][SIZE=2]inputStr1 = inputStr; [/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (IOException e) {[/SIZE]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block[/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]e.printStackTrace();[/SIZE]
[SIZE=2]} [/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]&#12288;[/SIZE]
[LEFT][SIZE=2]&#12288;[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i = 0; i <= [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]size[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; i++) {[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]index = inputStr.indexOf([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]";"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[LEFT][SIZE=2]info = inputStr.substring(0, index);[/SIZE]
[SIZE=2]inputStr = inputStr.substring(index, inputStr.lastIndexOf([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]";"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]));[/SIZE]
[SIZE=2]infoArray[I] = info;[/I][/SIZE]
[I][SIZE=2]}[/SIZE][/I][/LEFT]
[/LEFT]

 

[LEFT][I][SIZE=2]TextView tvMonsterID1 = (TextView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterID1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[LEFT][I][SIZE=2]TextView tvMonsterHealth1 = (TextView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterHealth1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[I][SIZE=2]TextView tvMonsterName1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterName1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[I][SIZE=2]TextView tvMonsterAtk1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterAtk1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[I][SIZE=2]TextView tvMonsterDef1 = (TextView)[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]tvMonsterDef1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I][/LEFT]
[/LEFT]

 
 
 

[LEFT][I][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (infoArray[1] == [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Shewanadon"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) {[/SIZE][/I]
[LEFT][I][SIZE=2]ImageView imgMonsterPic1 = (ImageView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imgMonsterPic1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[I][SIZE=2]imgMonsterPic1.setImageResource(R.drawable.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]two[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[I][SIZE=2]} [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]else[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE][/I]
[I][SIZE=2]ImageView imgMonsterPic1 = (ImageView) [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].findViewById(R.id.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]imgMonsterPic1[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE][/I]
[i][SIZE=2]imgMonsterPic1.setImageResource(R.drawable.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]icon[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]

 
 

[LEFT][SIZE=2]String ID = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE]
[LEFT][SIZE=2]String Health = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String Name = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String atk = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]
[SIZE=2]String def = [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]&#12288;[/SIZE]
[LEFT][SIZE=2]ID = infoArray[0];[/SIZE]
[SIZE=2]Health = infoArray[1];[/SIZE]
[SIZE=2]Name = infoArray[2];[/SIZE]
[SIZE=2]atk = infoArray[3];[/SIZE]
[SIZE=2]def = infoArray[4];[/SIZE][/LEFT]
[/LEFT]

 
 

[LEFT][SIZE=2]tvMonsterID1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"ID: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + ID); [/SIZE]
[LEFT][SIZE=2]tvMonsterHealth1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Health : "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + Health);[/SIZE]
[SIZE=2]tvMonsterName1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Name: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + Name);[/SIZE]
[SIZE=2]tvMonsterAtk1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Attack: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + atk);[/SIZE]
[SIZE=2]tvMonsterDef1.setText([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Defense: "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + def);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]}[/SIZE]

[SIZE=2]}[/SIZE][/LEFT]
 
Upvote 0
Using code tags doesn't help that much if your original code doesn't use indentation.
It's a good habit to get into, and I'd recommend you give it some serious consideration.

This isn't really an Android specific problem. It's a Java coding problem, and you might be better off in a forum dedicated to people learning Java.

Here are a few observations.

-------

This code probably works, but only if your input file isn't very large.
You read a buffer full each time around the loop, but only use the last buffer read.
If the file isn't large then you'll only read one buffers worth of data, so it will still work.

Code:
FileInputStream fstream = new FileInputStream(FILENAME); 
while((bytesRead = fstream.read(byteArray)) != -1) { 
  inputStr = new String(byteArray,0,bytesRead); 
}
inputStr1 = inputStr;


-------

In this code, if inputStr doesn't contain a ";" character then index will be -1, and the substring call will fail with a string index out of bounds exception.

Code:
for (int i = 0; i <= size; i++) {
  index = inputStr.indexOf(";");
  info = inputStr.substring(0, index);
  inputStr = inputStr.substring(index, inputStr.lastIndexOf(";"));
  infoArray = info;
}


-------

This code almost certainly isn't doing what you intend it to do.
You don't use == to compare strings.

Code:
if (infoArray[1] == "Shewanadon") {
  ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);
  imgMonsterPic1.setImageResource(R.drawable.two);
} else {
  ImageView imgMonsterPic1 = (ImageView) this.findViewById(R.id.imgMonsterPic1);
  [i]imgMonsterPic1.setImageResource(R.drawable.icon);
}

The syntax
imgMonsterPic1
isn't valid Java.

Does your code compile?

As the error you get is
java.lang.StringIndexOutOfBoundsException
you need to look for any code that indexes into a string where the index might be too large or small. From the code you supplied it'll be the call to substring I mention earlier in this post.

But as boogs suggested, it will be worth your while getting to know the development tools, and getting to grips with the debugger.

Mark
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones