2013-09-20 182 views
0

我想添加我剛收到的所有用戶輸入並將其全部放入一個數組位置。將多個對象添加到數組

Scanner sc = new Scanner(System.in); 
int cit = 0; 

//cit = Integer.parseInt(sc.nextLine()); 

/*if statement to ensure a value greater than 0 and less than or equal to 50 is 
* entered. 
*/ 
//System.out.println(cit); 
System.out.println("Welcome to OswegoNote - your friendly Citation Manager."); 
boolean accepted = false; 
Index citationIndex; 
Citation currentCitation; 
while (!accepted) { 
    System.out.println("How many citations would you like to store today? (between 0 and 50):"); 
    cit = Integer.parseInt(sc.nextLine()); 
    int citcounter = cit; 
    if (cit <= 50) { 

     if (cit > 0) { 
      accepted = true; 
      citationIndex = new Index(cit); 

     } else { 
      System.out.println("Error: please enter a number between 0 and 50."); 
     } 

    } else { 
     System.out.println("Error: please enter a number between 0 and 50."); 
    }//end if <=50 

}//end while loop 



for (int i = 0; i < cit; i++) { 

    currentCitation = new Citation(""); 
    currentCitation.updateId(i); 

    System.out.println("Please enter publication name (or 'quit' to quit):"); 
    String name = sc.nextLine(); 
    currentCitation.setName(name); 


    System.out.println("Please enter the date of publication (in mm/dd/yyyy format) (or 'quit' to quit):"); 
    String pubDate = sc.nextLine(); 
    currentCitation.setDateOfPublication(pubDate); 



    //while (accepted = true) { 
    System.out.println("How many authors are there? (max 3):"); 
    int numAuthors = Integer.parseInt(sc.nextLine()); 
    //currentCitation.numOfAuthors = numAuthors; 
    if (numAuthors <= 3 && numAuthors > 0) { 
     for (int a = 0; a < numAuthors; a++) { 
      System.out.println("Enter the name of author #" + (a+1) + ". (FirstName MI. LastName) (or 'quit' to quit):"); 
      String authorName = sc.nextLine(); 
      currentCitation.addAuthor(authorName); 
     } 
     accepted = false; 
     //break; 
    } else { 
     System.out.println("Error: Please enter a number between 0 and 3."); 
    } //end if 
    //} 



    System.out.println("Where was it published? (or 'quit' to quit):"); 
    String pubPlace = sc.nextLine(); 
    currentCitation.setWherePublisher(pubPlace); 

    System.out.println("What is the publisher's name? (or 'quit' to quit):"); 
    String publisher = sc.nextLine(); 
    currentCitation.setPublisher(publisher); 

    //while (accepted = true) { 
    System.out.println("How many keywords would you like to add?(max 5) (or 'quit' to quit):"); 
    int numKeywords = Integer.parseInt(sc.nextLine()); 
    if (numKeywords <= 5 && numKeywords > 0) { 
     for (int k = 0; k < numKeywords; k++) { 
      System.out.println("Enter keyword #" + (k+1) + ". (or 'quit' to quit)"); 
      String keyW = sc.nextLine(); 

      currentCitation.addKeyword(keyW); 
     } 
     accepted = false; 
     //break; 
    } else { 
     System.out.println("Error: Please enter a number between 0 and 5."); 
    } //end if 
    //} 

    System.out.println("This is your Citation:"); 
    System.out.println("Name: " + currentCitation.getName()); 
    System.out.print("Author(s): "); 

    for (int s = 0; s <= numAuthors - 1; s++) { 
     if ((numAuthors - 1) > s) { 
     System.out.print(currentCitation.authors[s] + ", "); 
    } else { 
      System.out.println(currentCitation.authors[s]); 
     } 
    } 
    System.out.println("Date of Publication: " + currentCitation.getDateOfPublication()); 
    System.out.println("Name of Publisher: " + currentCitation.getPublisher()); 
    System.out.println("Publication Place: " + currentCitation.getWherePublisher()); 

    System.out.print("Keywords: "); 
    for (int s = 0; s <= numKeywords - 1; s++) { 
     if ((numKeywords - 1) > s) { 
      System.out.print(currentCitation.keywords[s] + ", "); 
     } else { 
      System.out.println(currentCitation.keywords[s]); 
     } 

    } 
    System.out.println("Would you like to save this? (yes or no or 'quit' to quit):"); 
    String answer = sc.nextLine(); 
    if (answer.equalsIgnoreCase("no")) { 

    } else { 
     citationIndex[i] = {currentCitation.authors, currentCitation.keywords, currentCitation.ID, currentCitation.dateOfPublication, currentCitation.name, currentCitation.publisher, currentCitation.wherePublisher}; 
    } 

}// end for 

對不起,整個主要方法。我對Java並不擅長,所以我不知道如何描述我的情況。

+0

我試圖將所有的輸入添加到citationIndex []中。 – user2800960

+1

你能更詳細地描述你遇到的問題嗎? – Dukeling

+0

你的問題到底是什麼?顯然,沒有人會在不知道究竟要查找什麼的情況下閱讀你的整個代碼。 –

回答

0

使用ArrayList,而不是一個數組,然後通過addAll多個項目添加到它:

如果你想添加到它的項目是不是已經在集合中,把它們放在一個第一:

mylist.addAll(Arrays.asList(1, 2, 3));

或者你可以一次添加它們一個:

mylist.add(1); 
mylist.add(2); 
mylist.add(3); 

免責聲明:我只BR欠你的代碼,因爲我不想讀它。我主要根據你的問題標題回答。

+0

我們不允許使用ArrayList來完成這個任務。 – user2800960

+0

這個任務做了什麼,它需要用戶輸入(書的名稱,發佈者,發佈日期,關鍵字,ID#等),並且在我們獲得所有輸入之後,我們應該把它放入一個包含所有引文數據 – user2800960

+0

在收集信息,關鍵詞[]和作者[]的過程中需要兩個數組。 – user2800960