所以我试着制作一个程序来计算一个字符串有多少个数字和字母,然后将数字保存在一个整数数组和一个字符数组中的字母中,但它不会抛出跳出错误。
public static void main(String[] args) {
Scanner k = new Scanner(System.in);
System.out.println("Enter a string");
String s = k.nextLine();
int spaceCount = 0;
int[] intarray;
char[] charArray;
int numCount = 0;
int letterCount = 0;
for (int i = 0; i < s.length(); i++) {
if (Character.toString(s.charAt(i)).matches("[0-9]")) {
numCount++;
} else if (Character.toString(s.charAt(i)).matches("[a-zA-Z]")) {
letterCount++;
}else {
continue;
}
}
System.out.println(numCount);
System.out.println(letterCount);
intarray = new int[numCount];
charArray = new char[letterCount];
for (int i = 0; i <s.length(); i++) {
if (Character.toString(s.charAt(i)).matches("[0-9]")) {
intarray[i] = s.charAt(i);
continue;
}else if (Character.toString(s.charAt(i)).matches("[a-zA-Z]")){
charArray[i] = s.charAt(i);
continue;
}
}
for (int i =0;i<intarray.length;i++){
System.out.println(intarray[i]);
}
for (int i=0;i<charArray.length;i++){
System.out.println(charArray[i]);
}
}
}