2016-04-18 112 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]; 我認爲每一件事情都會好