hi Guys,
Here is a program using StringBuffer class.The problem i came across is :
1. When there is no space between the double quotes [StringBuffer str = new StringBuffer("");],the program throws an error java.lang.StringIndexOutOfBoundsException for "for loop2" after j=0.
2. When there is single space,then "for loo2" throws exception after j=1.
3. And when the space is more than 3,the program runs successfully.
Can anybody plz throw a light on this.
import java.util.*;
public class ForLoop
{
Vector myStockSymbols;
Vector myStockValues;
private static char c[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
public ForLoop()
{
myStockSymbols = new Vector();
myStockValues = new Vector();
for(int i=0;i<10;i++)
{
System.out.println("for loop1 "+i);
StringBuffer str = new StringBuffer("");
for(int j=0;j<4;j++)
{
System.out.println("for loop2 "+j);
str.setCharAt(j,c[(int)(Math.random()*26f)]);
}
myStockSymbols.addElement(str.toString());
myStockValues.addElement(new Float(Math.random()*100f));
}
for(int k=0;k<10;k++)
{
System.out.println("for loop3 "+k);
System.out.println(" "+myStockSymbols.elementAt(k)+" "+myStockValues.elementAt(k));
}
System.out.println("here");
}
public static void main(
String[] args)
{
ForLoop loop = new ForLoop();
}
}
Thanks.
shailendra