我正在尝试格式化字符串序列“ab c”,其中 ab 和 c 都是整数。我需要将 ab 和 c 添加到 3 个单独的数组中。这就是我所拥有的。当提示输入 str 时,不断出现 NumberFormatException 错误。
我尝试更改一些变量及其声明,并在线查看其他解释。
import java.util.*;
import javax.swing.*;
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.println("How Many times do you want to loop it?"); //Scanner takes in input
int loop = s.nextInt();
//if someone can help me format this better or have a 3d array that would be helpful.
ArrayList<Integer> a1 = new ArrayList<Integer>(); //3 arrays
ArrayList<Integer> b1 = new ArrayList<Integer>();
ArrayList<Integer> c1 = new ArrayList<Integer>();
String a = ""; //Int #1
String b = ""; //Int #2
String c = ""; //Int #3
String strSpc = " "; //String Spaces to compare with actual String
int x = 0;
String str = " ";
//Start of the error
for(int i=0; i<loop;i++) {
str = JOptionPane.showInputDialog(i+1 + ": ");
//checks format
while(str.charAt(x) !=(strSpc.charAt(0))){
a = a + str.charAt(x);
x++;
}
int a2 = Integer.parseInt(a);
a1.add(a2);
while(str.charAt(x+1) !=(strSpc.charAt(0))){
b = b+str.charAt(x);
x++;
}
int b2 = Integer.parseInt(b);
b1.add(b2);
while(str.charAt(x+1) !=(strSpc.charAt(0))){
c = c+str.charAt(x);
x++;
}
int c2 = Integer.parseInt(c);
c1.add(c2);
System.out.print('\n');
}
}
侃侃无极
相关分类