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,所以我真的不知道如何描述我的情况。

4

1 回答 1

0

使用 anArrayList而不是数组,然后通过以下方式向其中添加多个项目addAll

如果您要添加到其中的项目尚未在集合中,请先将它们放入其中:

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

或者您可以一次添加一个:

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

免责声明:我只浏览了您的代码,因为我不想阅读它。我主要根据您的问题的标题来回答。

于 2013-09-20T23:16:57.037 回答