Ciao a tutti!
Io ho questo codice:
codice:
import prog.io.ConsoleInputManager;
import prog.io.ConsoleOutputManager;
public class ConcatenaStringhe{
public static void main(String[] args){
ConsoleOutputManager video = new ConsoleOutputManager();
ConsoleInputManager tastiera = new ConsoleInputManager();
StringBuffer testo1 = new StringBuffer(tastiera.readLine("\nInserire prima Stringa: ") + " ");
StringBuffer testo2 = new StringBuffer(tastiera.readLine("\nInserire seconda Stringa: "));
StringBuffer concat = new StringBuffer(testo1.length() + testo2.length());
char c;
int i;
for(i = 0; i < testo1.length(); i++){
c = testo1.charAt(i);
concat.setCharAt(i,c);
}
for(int j = 0; j < testo2.length(); j++, i++){
c = testo2.charAt(j);
concat.setCharAt(i,c);
}
video.println("\nTesto: "+concat);
}
}
Solo che mi restituisce (all'esecuzione, e non alla compilazione) un errore:
codice:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.StringBuffer.setCharAt(Unknown Source)
at ConcatenaStringe.main(ConcatenaStringe.java:17)
Come mai?
Ho sbagliato a dichiarare lo StringBuffer per caso?