You get at the contents of an array by using an index. I see in your code that you have used indexes into arrays before.
Can you explain what the problem is?
Two examples:
names[i] != null) n = names[i];
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
You get at the contents of an array by using an index. I see in your code that you have used indexes into arrays before.
Can you explain what the problem is?
Two examples:
names[i] != null) n = names[i];
Wonderlandslost (February 12th, 2012)
Well with that, I used the i in the findFirstEmpty, so would have to change it to an x, which is the int I'm using in getOn, right? So then I would have something like
Using the String name that was provided at the beginning of getOnif (names[x] = null) { name = names[x]; }
I'm not sure if that's correct. Indexing arrays is confusing to me. I'm not sure how to get the int I take from the findFirstEmpty and apply it to the string array. I'm also not sure how I get the string name from the main method to begin with.
The name of the variable you use for the index into an array is not important. i or x or theNameHere any are ok.
Again you need to decide what the getOn() method is supposed to do BEFORE you write the code for it.
So far you have called a method and gotten an index. What else is getOn supposed to do?
Wonderlandslost (February 12th, 2012)
So it's supposed to call the method, get the index, set the null spot to the string name provided in the main method and return that to the main method in place of null.
As far as I understand at least.
That sounds reasonable, except that the code you posted earlier for getOn() showed that it did not return anything(void)
Okay, so this is a portion that makes no sense to me. When declaring methods, how do you know when you need to use public void something(); or public something(); or any of the other options?
This method would end up returning the name array, correct, with something like return names[];
The first is for a method that does not return anything.how do you know when you need to use public void something();
or public something();
The second is a constructor for the something class.
What method?This method would end up returning the name array
Leave off the []s.something like return names[];
Wonderlandslost (February 12th, 2012)
The get one method should be returning the names array correct? Or would it just be returning string name?
When I changed it to just returning the string name, I didn't get any errors
And okay, that makes a lot more sense, thank you for explaining it to me.
Last edited by Wonderlandslost; February 12th, 2012 at 05:57 PM.
Fill in the ...sThe get one method should be returning the ....
It depends on what the design of the program requires.
BTW You need to do something if findFirstEmpty returns -1
Wonderlandslost (February 12th, 2012)
Well, as far as I understand, the program requires us to push string names, such as anna, into the getOn method through the main method. Since I'm assuming that name is going to be the string that I will use to declare the string "anna", I would think that returning the name would allow for a new item to be entered. And to do something about if it returns the -1, would that be using an if statement, like it did when searching for null in findFirstEmpty, or would it be something along the lines of using a try and catch?
The getOn method should be returning the array with the string names in it.
If you want to return an array, you need to change the definition of the method to show that.The getOn method should be returning the array
BUT there is no need to return the array if the array is defined outside of the method where it is available to any method in the class already.
Yes an if would work.-1, would that be using an if statement
Wonderlandslost (February 12th, 2012)
So, I wouldn't need a return? Or would I need to return something else, like the original string name instead of the array string names?
Something like;
public String getOn(String name) { //Use the findFirstEmpty to locate the first null seat int x =findFirstEmpty(); //Take the empty seat and put the child in it if (names[x] != -1) { //indicates that the array is full System.out.print("The bus is at maximum capacity"); } else { //Indicates that the scan located a null spot in the array name = names[x]; } //Return the seat with child in it. return name; }
However the if statement states that they are incompatible types since one is int and the other isn't
Can you Explain what you are trying to do here.if (names[x] != -1)
Wonderlandslost (February 12th, 2012)
Since I returned the -1 value to indicate that it was a full array. I thought that by stating that the x value I pulled from findFirstEmpty was equal to -1 in the if statement, I could have the portion of code that would indicate what to do when the array was full.
How would you code that?the x value I pulled from findFirstEmpty was equal to -1
What you coded was something different.
Wonderlandslost (February 12th, 2012)
Would it be something along the lines of this;
Where the x is what is indicating whether or not the value is -1 or something else. Instead of referencing the array itself, I would use the value position from findFirstEmpty.int x =findFirstEmpty(); //Take the empty seat and put the child in it if (x != -1) { //indicates that the array is full System.out.print("The bus is at maximum capacity"); } else { //Indicates that the scan located a null spot in the array name = names[x]; } //Return the seat with child in it. return name;
Look at your test again.
Wonderlandslost (February 12th, 2012)
Okay, I'm not completely sure I'm following, but would it be the fact I have x != -1 instead of x == -1?
Yes that would a better way to test it.
Wonderlandslost (February 12th, 2012)
In regards to the getOff function, since it is similar to getOn by way of what it's doing, only reverse, would it be possible to do something like this;
public String getOff(String name) { //Use the findFirstEmpty to set the formerly filled seat to null int x = findFirstEmpty(); //Take the filled seat and remove the child if (x > 0) { //Removes a specific person from the bus array names[x] = null; } else { System.out.println("The bus contained no children."); } //Return the seat with child in it. return name; //Change the child's name to 'empty' to signify their leaving the bus. }
Last edited by Wonderlandslost; February 12th, 2012 at 11:05 PM.
In arrays, 0 is the index to the first element in the array.
What is the method getOff supposed to do? What does it do with the String: name that is passed to it?
What value is it supposed to return?
Wonderlandslost (February 13th, 2012)
getOff is supposed to take the array and find a specific name, it then has to take that and set it back to null, which virtually represents the child getting off of the bus. I think that it is supposed to take the string name initially and remove that portion from the array, by locating where that is in the array, then return the string of null. I'm pretty sure this needs to return null.
Where in the code you posted for getOff() does it look for the name in the array?
Why does the code for the getOff() method return a String?
The code you posted and the description you just posted have very little in common. The method does not do what you have described it is supposed to do.
It looks like you need to do these two steps:
1) find a specific name,
2) set it back to null,
And consider what to do if the name is not found.
Perhaps it could return a boolean value indicating if the removal was successful.
true if found and removed,
false if not found
Last edited by Norm; February 13th, 2012 at 01:44 PM.
Wonderlandslost (February 13th, 2012)
I'm not sure to the first question, I'm not sure how to go about doing that. As for why it returns a string, perhaps it doesn't. I've changed it slightly to have it return null, since I am changing a portion of the array to null.
You have already written a search loop in the findFirstEntry method so you know how to write a loop and compare the contents of the array to a desired value. The loop in getOff will be very similiar.
Why return anything from the method?
The purpose of returning something is to give the caller some results. Look at what findFirstEntry returned. What did the caller of findFirstEntry do with the value was returned?
Wonderlandslost (February 13th, 2012)