0

I want to add all of the user input I just received and put it all into one array position.

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

Sorry for the entire main method. I'm not very good at Java so I don't really know how I could describe my situation.

4
  • I'm trying to add all of the input into the the citationIndex[]. Commented Sep 20, 2013 at 23:10
  • 1
    Can you describe the problem you're having in a bit more detail? Commented Sep 20, 2013 at 23:12
  • What exactly is your question? Obviously nobody will read your entire code without knowing what exactly to look for. Commented Sep 20, 2013 at 23:15
  • 1
    Could you please provide an SSCE ?! It will help us to answer your question, thus helping you having a correct answer Commented Sep 20, 2013 at 23:18

1 Answer 1

0

Use an ArrayList instead of an array and then add multiple items to it via addAll:

If the items you want to add to it are not already in a collection, put them in one first:

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

Or you can add them one at a time:

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

DISCLAIMER: I only browsed your code because I didn't want to read it. I'm mostly answering based on your question's title.

5
  • We are not allowed to use ArrayList for this assignment. Commented Sep 20, 2013 at 23:28
  • What the assignment does is it takes user input (name of a book, publisher, publish date, keywords, ID#, etc.) and after we get all of the input we are supposed to put it into an array that contains all of the citation data Commented Sep 20, 2013 at 23:29
  • takes two arrays in the process of gathering the information, keywords [] and authors []. Commented Sep 20, 2013 at 23:30
  • Then state all such things in your question, not here. Commented Sep 20, 2013 at 23:54
  • SO is not a place where you ask people to do your homework... You should show us what is going wrong in your program, not just copy paste your entire code. Where are you stuck ? @user2800960 Commented Sep 21, 2013 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.