• Post Reply Bookmark Topic Watch Topic
  • New Topic

twodim arrays  RSS feed

 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem with this logic I am getting NumberFormat exception
while doing TotAmt. Also suppose if i need to handle the empty array how would i handle it.

Could please any one help me in the below case

String [][] list = new String[3][3];
list[0][0] = "7.00"; for empty string list[0][0]="";
list[0][1]= "56.00";
list[0][2]= "eeeee";
list[1][0]="8.00";
list[1][1]="66.00";
list[2][2]="nnnnn";
float f = 0.0f;
for(int i=0; i<list.length; i++)
{
for(int j=0; j<list[i].length; j++)
{

if(i<2 && j<2)
System.out.println(list[i][j]);
f += Float.parseFloat(list[j][j]);
System.out.println("fl:" +f);
}
}


}
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no way you can get "eeeee" or "nnnnn" into a decimal number. those entries can be relied upon to throw a NumberFormatException.

As for iterating through an "empty" (better called zero-length) array, you do that exactly the same way you iterate through an array with members. Use a for loop or a for-each (=enhanced for) loop, and as long as you set up the loop correctly, it will do exactly . . .















. . . nothing.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and if you wanted to get the bit about adding "eeeee" out, you thought you had it in the "if" block. You haven't, not unless you add another pair of {}.
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you show me with example please? I need convert the string array to float and calcualte the total. what is the best way?
to do
 
phani kon
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for the aboove example. How would i show the correct amount records and uncorrect amt records
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by lakshmi manepally:
Can you show me with example please?
No.

We do not simply hand out that sort of answer here on the Ranch. Have you followed my suggestion about going through the Float class? Have you worked out what the scope of the "if" statement is? Why didn't you post on beginners', since this appears to be a beginner's question?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!