我想接受诸如5,6,66,88
这样的用户输入,依此类推。当我使用Scanner对象存储此对象并使用内置的定界符方法时,用户输入进入无限循环。如果我删除了定界符方法,并在下一行接受用户输入,则效果很好。
package demoCollection;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
/**
* @author Aquib
*
*/
public class LargestNumber {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
UserInput ui = new UserInput();
ui.userentry();
}
}
class UserInput {
int input;
List<Integer> intList = new ArrayList<>();
void userentry() {
Scanner sc = new Scanner(System.in).useDelimiter(",");;
System.out.println("Enter list of number. Enter y to complete");
while (sc.hasNextInt()) {
int a = sc.nextInt();
intList.add(a);
}
System.out.println("Entered List is"+intList);
Collections.sort(intList, (a1, a2) -> a1 > a2 ? -1 : a1 < a2 ? 1 : 0);
System.out.println("Sorted List is"+intList);
System.out.println("Largest number is " + intList.get(0));
}
}