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.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 65

Thread: Stuck and can't get past it!

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    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];

  2. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  3. #27
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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
    if (names[x] = null)
    {
      name = names[x];   
    }
    Using the String name that was provided at the beginning of getOn

    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.

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    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?

  5. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  6. #29
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    That sounds reasonable, except that the code you posted earlier for getOn() showed that it did not return anything(void)

  8. #31
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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[];

  9. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    how do you know when you need to use public void something();
    or public something();
    The first is for a method that does not return anything.
    The second is a constructor for the something class.
    This method would end up returning the name array
    What method?
    something like return names[];
    Leave off the []s.

  10. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  11. #33
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.
     public String getOn(String name)
        {
            //Read the name into the method.
     
     
            //Use the findFirstEmpty to locate the first null seat
            int x =findFirstEmpty();
     
            //Take the empty seat and put the child in it
     
            //Return the seat with child in it.
            name = names[x]; 
            return name;
     
        }
    Last edited by Wonderlandslost; February 12th, 2012 at 05:57 PM.

  12. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    The get one method should be returning the ....
    Fill in the ...s
    It depends on what the design of the program requires.

    BTW You need to do something if findFirstEmpty returns -1

  13. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  14. #35
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.

  15. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    The getOn method should be returning the array
    If you want to return an array, you need to change the definition of the method to show that.
    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.

    -1, would that be using an if statement
    Yes an if would work.

  16. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  17. #37
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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

  18. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    if (names[x] != -1)
    Can you Explain what you are trying to do here.

  19. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  20. #39
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.

  21. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    the x value I pulled from findFirstEmpty was equal to -1
    How would you code that?
    What you coded was something different.

  22. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  23. #41
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    Would it be something along the lines of this;

     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;
    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.

  24. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    Look at your test again.

  25. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  26. #43
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    Okay, I'm not completely sure I'm following, but would it be the fact I have x != -1 instead of x == -1?

  27. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    Yes that would a better way to test it.

  28. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 12th, 2012)

  29. #45
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past 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;

       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.

  30. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    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?

  31. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  32. #47
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.

  33. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    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.

  34. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

  35. #49
    Member
    Join Date
    Feb 2012
    Posts
    43
    My Mood
    Confused
    Thanks
    33
    Thanked 0 Times in 0 Posts

    Default Re: Stuck and can't get past it!

    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.

  36. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Stuck and can't get past it!

    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?

  37. The Following User Says Thank You to Norm For This Useful Post:

    Wonderlandslost (February 13th, 2012)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. [SOLVED] Nothing really wrong but i cant seem to get past a stone wall
    By Lurie1 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 1st, 2012, 12:35 PM
  2. Please help not sure how to get past the Syntax error
    By KnightC in forum Other Programming Languages
    Replies: 2
    Last Post: December 23rd, 2011, 10:09 AM
  3. problems getting past the out of bounds error
    By dbdny in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 05:57 PM
  4. Getting date based on days past year
    By aussiemcgr in forum Java Theory & Questions
    Replies: 3
    Last Post: July 14th, 2010, 04:06 PM
  5. Having Issues Past this
    By baGoGoodies111 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 12th, 2009, 08:19 PM