Results 1 to 5 of 5
  1. #1
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default counting in an array

    Guys I have an issue here that I cant seem to understand..... Basically I have school system for registering students on courses, but what I want to do is keep count on how many students choose one module e.g if student 1 picks English then the is 1 and if student 2 picks english the count goes to and so. The tricky part is that it finds the users choice in array and thats why i cant seem to figure out the counter layout. here is the code to give you folk an idea of what i am trying to say.
    Java Code:
    boolean isValidId;
    						String moduleCode;
    						do {
    							System.out.println("Student number:");
    							while (kb.hasNextInt()) {
    								kb.next();
    								System.out
    										.println("Please enter student number: ");
    							}
    							studentNumber = kb.next();
    						} while (studentNumber.charAt(0) != 'R'
    								&& !Character.isDigit(studentNumber.charAt(1)));
    
    						isValidId = isStudentID(kb, userId, studentNumber);
    						if (isValidId) {
    							do {
    								System.out.println("Module Code:");
    								// Validate the user choice for string
    								while (kb.hasNextInt()) {
    								}
    								moduleCode = kb.next();
    							} while ( moduleCode.length() != 8);
    
    							int pos = addStudentChoice(kb, userId,
    									studentNumber);
    							if (pos >= 0) {
    
    								if (moduleCode1[pos] == null) {
    									moduleCode1[pos] = moduleCode;
    									 currentAmount[pos]++;// i set counters here to try do it, but doesnt do what i want.
    								} else {
    									moduleCode2[pos] = moduleCode;
    									currentAmount[pos]++;// i set counters here to try do it, but doesnt do what i want.
    								}
    							}
    							System.out.println("$$$$$$$$$$$$"+currentAmount[pos]);
    						}
    						displayMainMenu(userId);

  2. #2
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: counting in an array

    Sorry ..just need a bit more explanation .....

  3. #3
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: counting in an array

    Ok no problem. I have a menu like this:
    1 - Add Student
    2 - List Students
    3- Add Module
    4- List Modules
    5- Add Student Choice

    So I add a student, by student number. I store them in array of up to 200. I then add a module by code and title. I then enter option 5 (where my problem is)
    Firstly I am prompted to enter student number (i search through my add student array and if the student number is there it will move on and ask for course code) if the code is there its shown in the list students options along with the students full name and student number. The problem is that if i have more than 1 student that chose the same course. I want to keep a count of it, so I can limit the mount per course.

  4. #4
    downWithJava is offline Member
    Join Date
    Mar 2013
    Posts
    17
    Rep Power
    0

    Default Re: counting in an array

    Ok after a bit of research it seems, hashmaps may help. I have no clue on these so if some is viewing and knows if I can use hash maps with two string arrays, could you let me know. Like I have two arrays that will be storing the same values. e.g. you chose two modules, so i have two arrays Module1 and Module2. I want to be able to check through both of these to see the number of times that the same module is chosen.

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: counting in an array

    I think ....Hashmaps search the array with respect to its index .....don't know exactly...

Similar Threads

  1. Counting duplicates in an array
    By mar1 in forum New To Java
    Replies: 7
    Last Post: 04-16-2014, 05:06 PM
  2. Array for counting from 1 to 100
    By Magic8ballfollower in forum New To Java
    Replies: 5
    Last Post: 09-22-2011, 05:55 AM
  3. Need help with counting strings in an array.
    By dalonehunter in forum New To Java
    Replies: 1
    Last Post: 10-02-2009, 09:47 AM
  4. Array help counting # of grades
    By speaknspell in forum New To Java
    Replies: 4
    Last Post: 04-16-2009, 11:09 PM
  5. Counting Duplicate Variables in an Array
    By Npcomplete in forum New To Java
    Replies: 2
    Last Post: 10-24-2008, 08:33 PM

Posting Permissions

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