所以我正在解决一个问题,需要对一些字符串进行一些转换,但我甚至无法读取字符串的数量(整数)。这是代码:
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> board = new ArrayList<String>();
int num = scan.nextInt();
int num1 = 0;
String num2 = "";
String word = "";
String word1 = "";
for (int i=0; i<=num; i++) {
word = scan.nextLine();
if (word.length() >10) {
num1 = word.length()-2;
num2 = Integer.toString(num1);
word1 = word.charAt(0) + num2 + word.charAt(word.length()-1);
board.add(word1);
} else {
board.add(word);
}
for (i=0; i<=num; i++) {
System.out.println(board.get(i));
}
}
}
}
我遇到的错误是线程“main”中的异常java.lang.IndexOutOfBoundsException:索引:1,大小:1
答案 0 :(得分:0)
你的意图是做这样的事吗?否则,为什么你有内循环重用相同的变量i?
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> board = new ArrayList<String>();
int num = scan.nextInt();
scan.nextLine();
int num1 = 0;
String num2 = "";
String word = "";
String word1 = "";
for (int i = 0; i <= num; i++) {
word = scan.nextLine();
if (word.length() > 10) {
num1 = word.length() - 2;
num2 = Integer.toString(num1);
word1 = word.charAt(0) + num2 + word.charAt(word.length() - 1);
board.add(word1);
} else {
board.add(word);
}
}
for (int i = 0; i <= num; i++) {
System.out.println(board.get(i));
}
}
}