import java.util.Arrays;
import java.util.Scanner;
public class Sort {
public static void main(String[] args) {
String temp = "";
String a ="";
String[] New;
Scanner keyboard = new Scanner(System.in);
//String s1 = new String(keyboard.nextLine());
System.out.println("Please write your elements.");
while(keyboard.hasNext()){
String currentString = keyboard.next();
if(currentString.equals(" ")){
System.out.println("Bye");
}
a += (currentString.charAt(0) + "").toUpperCase() + currentString.substring(1).toLowerCase()+",";
}
a = a.substring(0, a.length()-1);
New = a.split(",");
System.out.println("Your elements are" + Arrays.toString(New) + ".");
for (int x = 1; x < New.length; x++) {
for (int y = 0; y < New.length - x; y++) {
if (New[y].compareTo(New[y + 1]) > 0) {
temp = New[y];
New[y] = New[y + 1];
New[y + 1] = temp;
}
}
} for(int i=0;i<New.length;i++){
System.out.println(New[i]);
}
/*System.out.println("In alphabethical order form is:");
System.out.println(Arrays.toString(New));*/
}
}
hasnext in infinite loop ı have already search stackowerflow but answers cant fix the error.please help me.Here my codesPlease write your elements.
ıf ı press space and enter ı have a problem .
Bye Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at aa.Sort.main(Sort.java:38)