运行时异常-功能尚未实现



当到达

行时,下面的代码块显示jelliot中的错误"Runtime Exception- feature not implemented yet"
char arr [] = w.toCharArray();

在其他编译器中,它不会接受它应该接受的输入数。如果我设置n=4,它只需要2个输入。

    Scanner Sc = new Scanner (System.in);
    int n = Sc.nextInt();
    for(int count = 1;count<=n; count++){
        String w = Sc.nextLine();
        char arr [] = w.toCharArray();
        if(arr.length > 4){
            System.out.print(arr[0]);
            System.out.print(arr.length-2);
            System.out.print(arr[arr.length-1]);
        }
        else{
            System.out.println(w);
        }
        System.out.println();
    }

异常表明String类的这个特定方法(toCharArray())不是为您正在使用的java(可能是非标准的)实现实现的。您可以通过不使用字符数组来解决这个问题,而是使用String方法length()charAt()。试试这个:

Scanner Sc = new Scanner (System.in);
int n = Sc.nextInt();
for(int count = 1;count<=n; count++){
    String w = Sc.nextLine();
    if(w.length() > 4){
        System.out.print(w.charAt(0));
        System.out.print(w.length()-2);
        System.out.print(w.chatAt(w.length()-1));
    }
    else{
        System.out.println(w);
    }
    System.out.println();
}

最新更新