Results 1 to 4 of 4

Thread: Creating Arrays

  1. #1
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Creating Arrays

    Hi im writing a program where i ask the user how many students took a test and then ask them to enter the students name and the students answer to each individual question(true/false) and the answers will be contained in an array. I can do this fine for 1 or 2 users using a created array String[] studentAnswers and then String[] studentAnswers2. My problem is if the user enters 50 students is there some sort of loop i can write to make String[] studentAnswersX. So far from searching i have not found that i cannot create an array in a loop, is there anyway to do this or can you think of any other way around this problem ?

    Thanks Steve

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Creating Arrays

    you want each student to have an array signed to them containing answers?

    so 50 students, 1 answer = 1 array so 50 students + 50 answers = 100 arrays?

    is this what you are saying?

    couldnt you just create Student Class and assign an array[] variable encapsulate it, then when you enter a students name make that student Object.then for the answers just add to thier array?

    or have i miss understood the problem?

  3. #3
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Creating Arrays

    Thanks for the quick reply i was hoping to ask for the

    number of students - 50 ------- student id Array size = 50
    student ID - A (all the way to how ever many students) ------------- student id Array - {A,......}
    answers for student id index[0] ---------- {so essentially answers for studentId"A"}

    store these next answers in an array
    String[] studentAnswers"A"

    but because i dont know the ammount of students that are going to be entered i dont know how many arrays to create, my best guess was a for loop that would create String[] studentAnswers"X" but i no longer think that is possible. Maybe your solution is better but i do not fully understand in still fairly new to java. Here is the part of my code in question.
    Java Code:
    p("Hello Ms .Jones Welcome to your Test Program");
    		p("Please enter the number of Questions in todays test");
    		numQuestionsAnswers = ifint();// Checks user input for int data type and then saves in to the variable declared
    		p("Thank you now please enter the number of students who participated in the test");
    		numStudents = ifint();// Checks user input for int data type and then saves in to the variable declared
    		questions = new String[numQuestionsAnswers];// Here i am setting the array questions a length
    		answers = new String[numQuestionsAnswers];// Here i am setting the array answers a length
    		studentId = new String[numStudents];// Here i am setting the array studentId a length
    		p("Thank you can you now enter the questions for the test and their answers");
    		for (i=1 ; i<=numQuestionsAnswers ; i++){
    			p("Question" + (i) + ":" );
    			questions[i - 1] = keyboard.nextLine();// This will save the users input into the array index i
    			// check if there was information inputed
    			questions[i - 1] = emptys(questions[i - 1]);
    			p("Answer" + (i) + ":" );
    			answers[i - 1] = keyboard.nextLine();// This will save the users input into the array index i
    			// check if there was information inputed
    			answers[i -1] = emptys(answers[i -1]);
    		}
    		p("Thank you can you now enter Students Id Numbers and their Answers");
    		for (i=1 ; i<=numStudents ; i++){
    			p("Student" + (i) + " Id Number" + ":" );
    			studentId[i - 1] = keyboard.nextLine();// This will save the users input into the array index i
    			// check if there was information inputed
    			studentId[i - 1] = emptys(studentId[i - 1]);
    FROM HERE DOWN IS MY PROBLEM
    
    			for (j=1 ; j<=numQuestionsAnswers ; j++){
    			p("Answer" + (j) + ":" );
    			studentAnswers[j - 1] = keyboard.nextLine();// This will save the users input into the array index i
    			// check if there was information inputed
    			studentAnswers[j - 1] = emptys(studentAnswers[j - 1]);
    			}
    Last edited by steveday77; 04-30-2013 at 10:55 PM.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Creating Arrays

    There are several ways to do this. It appears you want to allocate an array based on input. Assuming you input "n" number of students and "q" number of questions, you could allocate the array like this:

    Java Code:
    boolean[][] students = new boolean[n][q];// for T/F questions
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Creating Stack with Arrays
    By ale626 in forum New To Java
    Replies: 11
    Last Post: 06-01-2012, 07:12 AM
  2. Help with creating a HashMap from Arrays
    By lannie1980 in forum New To Java
    Replies: 9
    Last Post: 04-24-2012, 10:39 PM
  3. Problem creating arrays
    By JohnPringle83 in forum New To Java
    Replies: 7
    Last Post: 05-06-2011, 11:01 PM
  4. Creating generic arrays
    By stijn1989 in forum Advanced Java
    Replies: 6
    Last Post: 11-18-2009, 01:26 PM
  5. [SOLVED] Creating an Array of Arrays?
    By xcallmejudasx in forum Advanced Java
    Replies: 5
    Last Post: 11-04-2008, 06:01 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •