2016-04-18 110 views
1

好日子给大家,我只是想问一下这个怎么办.. 我想创建程序,我可以显示所有日期输入没有分隔符“/”,所以我用分裂方法来做到这一点。更清楚这就是我想做的事: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]); 
} 
+3

,什么是与'的String [] =比较新的String [5]'的错误? – Tom

+1

由于String [] comp = new String [5]'有效并且OP尝试了它(?),所以投票关闭为打印错误/无法重现。 – Tunaki

+0

它说在运行期间ArrayIndexOutOfBoundsException –

回答

0

首先,String[] comp=new String[5]确实有效,它会清除编译器错误,发生后续错误,因为您正在访问超出数组长度的数组的索引。从技术上讲,这不应该发生,因为声明String[] comp=new String[5]将使您的阵列具有与date阵列相同的大小,您将其用作第二个循环条件。但是你的错误发生在第一个循环内部。

您重新初始化comp阵列,如果输入正确,现在将具有3的大小,而不是5的大小。由于你的第二个循环将使用date数组作为条件,它的大小仍然为5,所以你会尝试访问comp数组中的元素34,这些数组超出了刚刚重新分配的数组的长度。这是导致问题comp=date[ctr].split("/");的代码。

使用你的方法,我建议在2d数组中调用comp数组,以便存储日期的数量和日期的部分。

的代码可能会寻找现在这个样子:

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    String[] date=new String[5]; 
    // make comp a 2d Array 
    String[][] comp = new String[5][]; 
    int mm, dd, yyyy; 
    for(int ctr=0;ctr<date.length;ctr++){ 
     System.out.print("Enter Date: "); 
     date[ctr]=input.nextLine(); 
     // At index ctr store the different parts of the date 
     comp[ctr]=date[ctr].split("/"); 
     // This three steps can cause an ArrayOutOfRangeException, 
     // because the indexes 0,1,2 are depending on how many elements the split returned 
     // You might want to add this here, but i´ll leave it out. 
     // But since you never use mm, dd and yyyy you may aswell leave it out 
     mm=Integer.parseInt(comp[ctr][0]); 
     dd=Integer.parseInt(comp[ctr][1]); 
     yyyy=Integer.parseInt(comp[ctr][2]); 
    } 
    for(int ctr1=0;ctr1<date.length;ctr1++){ 
     for(int j = 0;j<comp[ctr1].length;++j) { 
      System.out.println(comp[ctr1][j] + " "); 
     } 
    } 
} 
+0

谢谢先生帮助它现在正在运行,因为我希望它是.. 顺便说一句,如果我想排序这个日期升序或降序? 是否可以使用bubblesort方法对其进行排序?我只在上周了解到这一点,所以我不完全确定如何将其应用于输入,如日期/时间输入。 –

+0

@ NEETRX-78您只需循环遍历'comp'数组,并且您可以简单地比较索引' 0-2'。 – SomeJavaGuy

1

为什么重新发明轮子?家庭作业?为什么不使用java工具来实现你的目标?

DateFormat input = new SimpleDateFormat("MM/dd/yyyy"); 
DateFormat output = new SimpleDateFormat("MM dd yyyy"); 

Date d = input.parse("10/11/1994"); 
System.out.println(output.format(d)); 

OUTPUT:

10 11 1994 
+0

从技术上讲,这将是'MM/dd/yyyy'。 – Tunaki

+0

@Tunaki更新了答案....我也怀疑...它符合西班牙语/欧元格式...'dd/MM/yyyy'和示例数据不能说明XDDD –

+1

那么代码有'mm = Integer.parseInt (comp [0]);'和'dd = Integer.parseInt(comp [1]);'。 – Tunaki

1

你必须先使用 “日/月/年”
字符串[2] = dateFormate日期[I] .split [分裂 “:” ];
让你在dateFormate上处理[1]; 我认为每一件事情都会好