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:
Code :names[i] != null) n = names[i];
Printable View
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:
Code :names[i] != null) n = names[i];
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 getOnCode Java:if (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?
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.Quote:
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?Quote:
This method would end up returning the name array
Leave off the []s.Quote:
something like return names[];
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.
Fill in the ...sQuote:
The 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
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.Quote:
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.Quote:
-1, would that be using an if statement
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;
Code Java: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.Quote:
if (names[x] != -1)
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?Quote:
the x value I pulled from findFirstEmpty was equal to -1
What you coded was something different.
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.Code Java: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.
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.
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;
Code Java: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. }
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?
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
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?