好日子给大家,我只是想问一下这个怎么办.. 我想创建程序,我可以显示所有日期输入没有分隔符“/”,所以我用分裂方法来做到这一点。更清楚这就是我想做的事:java数组变量的初始化
Input
Enter Date:10/11/1994
Enter Date:11/10/2008
Enter Date:12/12/2010
Enter Date:08/12/1999
Enter Date:09/10/2005
Output:
10 11 1994
11 10 2008
12 12 2010
08 12 1999
09 10 2005
的问题是我有一个错误 在System.out.println(comp[ctr1]);
它说,我必须初始化变量comp
,其实我不初始化我会用什么。我尝试使用String[] comp=new String[date]
和String[] comp=new String[5]
但它仍然是一个错误。在此先感谢..
String[] date=new String[5];
String[] comp;
int mm, dd, yyyy;
for(int ctr=0;ctr<date.length;ctr++){
System.out.print("Enter Date: ");
date[ctr]=input.nextLine();
comp=date[ctr].split("/");
mm=Integer.parseInt(comp[0]);
dd=Integer.parseInt(comp[1]);
yyyy=Integer.parseInt(comp[2]);
}
for(int ctr1=0;ctr1<date.length;ctr1++){
System.out.println(comp[ctr1]);
}
,什么是与'的String [] =比较新的String [5]'的错误? – Tom
由于String [] comp = new String [5]'有效并且OP尝试了它(?),所以投票关闭为打印错误/无法重现。 – Tunaki
它说在运行期间ArrayIndexOutOfBoundsException –