arrays - java arrayoutofbounds exception error when program together -
i doing assignment , have several simple string manipulations. think have last 1 figured out, , works itself, not work when put other string manipulations, giving me arrayoutofbounds exception error. advice? here small code made
public static void main (strings[] args) { scanner sc = new scanner(system.in); string thesentence = sc.nextline(); string [] thewords = thesentence.split(" "); arrays.sort(thewords); system.out.println(thewords[1]); system.out.println(arrays.tostring(thewords)); }
this not work when put rest of code though works itself. reference, code supposed take in small sentence , give me smallest word lexicographically. ex: input: "4 wait this"
output "is"
the code assuming thewords
have @ least 2 elements in it. if sentence provided user not have spaces, thewords
never element in position 1 , program crash on system.out.println(thewords[1]);
Comments
Post a Comment