0

受信したすべてのユーザー入力を追加し、すべてを 1 つの配列位置に配置したいと考えています。

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

ArrayList配列の代わりに を使用し、次の方法で複数の項目を追加しaddAllます。

追加したいアイテムがまだコレクションにない場合は、最初にコレクションに入れます。

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

または、一度に 1 つずつ追加できます。

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

免責事項:私はあなたのコードを読みたくなかったので、あなたのコードを閲覧しただけです。私は主にあなたの質問のタイトルに基づいて答えています。

于 2013-09-20T23:16:57.037 に答える