java - StringIndexOutOfBoundExceptions error is showing -
java - StringIndexOutOfBoundExceptions error is showing -
i trying create acronym 3 words input user:
import java.util.scanner; public class acronym { public static void main(string[] args) { scanner input = new scanner(system.in); string phrase; system.out.println("please, come in 3 words here>> "); phrase = input.nextline(); char first, second, third; int stringlength; int x = 0; char c; stringlength = phrase.length(); first = phrase.charat(0); sec = phrase.charat(x + 1); 3rd = phrase.charat(x + 1); for( x = 0; x < stringlength; x++); { int = 1; c = phrase.charat(x); if(character.iswhitespace(c)); if( <= 1) sec = phrase.charat(x+1); else if(a <= 2) 3rd = phrase.charat(x++); } system.out.println("the acronym " + first + sec + third); } }
but, whenever seek input stringindexoutofboundexceptions error showing. illustration tried input 3 words "one 2 three" , result:
please, come in words/phrases: 1 2 three
exception in thread "main" java.lang.stringindexoutofboundsexception: string index out of range: 13 @ java.lang.string.charat(string.java:646) @ acronym.main(acronym.java:23)
remove semi-colon
for (x = 0; x < stringlength; x++); ^
which terminating loop. same semi-colon terminating if
statement
if (character.iswhitespace(c)); ^
and please utilize braces surround command blocks
java indexoutofboundsexception
Comments
Post a Comment