This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Array search and print Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Array search and print" Watch "Java Array search and print" New topic
Author

Java Array search and print

nate langley
Greenhorn

Joined: Aug 02, 2011
Posts: 3
I have three .java files collocated in the same folder. Student, Roster, and RosterTester. assignment is to use an array to create a class roster. should be easy enough and i think i have most of this correct. my biggest issues that i can tell are the listRoster or print function and getting the data to add to the roster from a scanner in RosterTester. I have tried and tried and tried to figure this out, but i don't even know where i am going wrong. Any help is appreciated. Student.java is correct for sure!


Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
The student roster isn't of fixed size (or am I wrong?) therefore it would benefit you to use ArrayList instead

The reason the listRoster() isn't working I assume is the fact that it is empty, or contains empty strings, take a look at the body of the createAndAddStudent() method and see why.

Another problem I notice is the isEnrolled() method, you need to compare the contents of each index in the array with the input, students.equals(Name) will always return false

The Scanner class method nextLine() reads the next line of input and stores it in a String, that string should be input as parameter to the createAndAddStudent() method
nate langley
Greenhorn

Joined: Aug 02, 2011
Posts: 3
Unnar Björnsson wrote:The student roster isn't of fixed size (or am I wrong?) therefore it would benefit you to use ArrayList instead

You have the createAndAddStudent() method in place but it's body is incomplete, what it does now is erase the roster instead of adding a new student, first of all fix that.

The Scanner class has a method nextLine() that reads the next line of input and stores it in a String, so you could print out the text: "New student name: " and have the Scanner read the next line of input which would be the name of the student and insert that string into the createAndAddStudent()


the student roster is a fixed size and the instructions for the assignment were to use an array, I agree the array list would make more sense and i understand it a little better.

how do i fix the createAndAddStudent() so that it adds a student and goes to the next open slot in the array? that is what i thought i did with the for loop.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
how do i fix the createAndAddStudent() so that it adds a student and goes to the next open slot in the array? that is what i thought i did with the for loop.


I depends on whether a student can be removed from the list, if so you would need to be aware of gaps in your array therefore the safest way would be to scan the whole array (for loop) and insert the name at the first empty spot. You also need to handle the case that the list might be full, therefore it might be a good idea to have the method return something (boolean) to indicate whether the student was actually added or not.
nate langley
Greenhorn

Joined: Aug 02, 2011
Posts: 3
Unnar Björnsson wrote:
how do i fix the createAndAddStudent() so that it adds a student and goes to the next open slot in the array? that is what i thought i did with the for loop.


I depends on whether a student can be removed from the list, if so you would need to be aware of gaps in your array therefore the safest way would be to scan the whole array (for loop) and insert the name at the first empty spot. You also need to handle the case that the list might be full, therefore it might be a good idea to have the method return something (boolean) to indicate whether the student was actually added or not.


can you give me a demonstration of the for loop that i should be using.

Also is it possible to incorporate a if "" value entered .close() so that if you just hit enter instead of typing a string first, it closes the scanner and does the list.Roster.

As far as removing slots, that is the bonus. and i am not there yet since i can not even get the data in to the array.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
can you give me a demonstration of the for loop that i should be using.

I can't give you an exact code, the code shouldn't be a problem if you understand what needs to be done.
You need a loop, while loop would be appropriate here. You'll want to loop through each value of the array which are 25 (according to your code).
During each iteration you must check if the slot is free by comparing it to null (null meaning it doesn't contain a reference yet), if it's null it means that the slot is free and you can add a new student here. So you'll need two stopping conditions: one that stops when the end of the list is reached (if that happens it means the list is full, no slots available) and another that stops if an empty slot is found.

Also is it possible to incorporate a if "" value entered .close() so that if you just hit enter instead of typing a string first, it closes the scanner and does the list.Roster.

Yes, by putting the call to nextLine() in a loop which will repeat the request for a new line until some exit command is given, in each iteration you have to evaluate the input (ifs), if the input is "" then print the roster else do something else


Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Unnar Björnsson wrote:

Or, if line can be null,


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java Array search and print
 
Similar Threads
Compile error
Arrays: Calling input from other methods and classes
using Objects and Classes to Add Students to Courses
Passing Objects Created By User to Array in Different Class
Re: Urgent with this program, Pls help