• Post Reply Bookmark Topic Watch Topic
  • New Topic

Program Not working the way it should  RSS feed

 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings, in my program, I have a String Array with a size of 25. What I'm trying to do is create an add method that adds elements to this blank array (by default any object (which is what a String is) that is blank has a null value. When I attempt to add the same string, I create a method that displays the number of times that particular item is listed within the String array (I use a hash map to do this). The program is not working as planned. I'm not sure why. Let's say i add an element called Juice 3 times, It comes up with juice just once and null 24 times....What could I be doing wrong? Thanks!

 
khadar valli
Greenhorn
Posts: 26
Java MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naziru,

"Let's say i add an element called Juice 3 times, It comes up with juice just once and null 24 times....What could I be doing wrong? Thanks! "

PFB the method you had created for adding the flowers to the array.



Here you are initializing the index to zero. Whenever you call this method to add the element in this array, this method will replace the element which is available at that index.

So create such a method which adds the elements to the array based on its size.

Regards,
Khadar.

[edit]Remove incorrect color tags.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naziru Gelajo wrote:. . . by default any object (which is what a String is) that is blank has a null value. . . .
What does that mean?

Don't try [color=xxx] tags inside code; they don't work.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:...
What does that mean?
I think he meant to say or better say-every reference variable of type Object (or say String(IS-A Object)) if not initialized,is a null reference by default.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean that an array is [null, null, null ...] until the elements have been initialised?
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i don't know how you really mean my post.
i am exemplifying my statement(What i meant);

example:

output:

Arrays itself is an Object so first requirement is to initialize an array,though after initialization if the elements are not set they are null(it simply means referencing no any object) and can be viewed by printing the tostring representation of the elements of array(that's what Arrays.toString() does),as done above.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit]For primitive null equivalent is printed.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:. . .
output:
. . . .
Yes. Got it.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
khadar valli wrote:Hi Naziru,

"Let's say i add an element called Juice 3 times, It comes up with juice just once and null 24 times....What could I be doing wrong? Thanks! "

PFB the method you had created for adding the flowers to the array.



Here you are initializing the index to zero. Whenever you call this method to add the element in this array, this method will replace the element which is available at that index.

So create such a method which adds the elements to the array based on its size.

Regards,
Khadar.

[edit]Remove incorrect color tags.


Hello Khadar,

Thanks for your message I'm trying to figure out still, but I know what you mean. I just don't see how this can be done (adding to an array that is) at position starting at index 0 and incrementing by one each time an item is added.

Since arrays are not dynamic, where to next? Any suggestions? Thanks!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since arrays are not dynamic, where to next?

See the ArrayList class. It is dynamic.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Since arrays are not dynamic, where to next?

See the ArrayList class. It is dynamic.


Not allowed to use array lists otherwise I would
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you trying to design code that will "extend" an array when the array gets full?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:Are you trying to design code that will "extend" an array when the array gets full?


Well, no I'm not. The code has a limit of 25 strings. I must populate the array with strings that are input by the user
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I was confused by this:
Since arrays are not dynamic, where to next? 


If there is no need to worry about the array's size or overflowing the array, can you explain the problem?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naziru Gelajo wrote:. . .  I just don't see how this can be done (adding to an array that is) at position starting at index 0 and incrementing by one each time an item is added.  . . .
But that is exactly what you have to do. Start adding at position 0 and increment the position every time something is added. A bit of thought shou‍ld lead you to the solution.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Naziru Gelajo wrote:. . .  I just don't see how this can be done (adding to an array that is) at position starting at index 0 and incrementing by one each time an item is added.  . . .
But that is exactly what you have to do. Start adding at position 0 and increment the position every time something is added. A bit of thought shou‍ld lead you to the solution.


I started doing that and that was my original intention without a doubt. However, I'm running into a conundrum that when I attempt to display the elements within the array, it only displays the element at index 0. The way I had originally wrote the code I guess is the culprit for this. Is there a way in which I structured the code in an incorrect fashion? Thanks!
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it only displays the element at index 0.

Do you mean it only displays one element of the array
or do you mean that only the first element in the array has a value and the other elements are all null?

If the second case is what is happening, look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added. 
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
it only displays the element at index 0.

Do you mean it only displays one element of the array
or do you mean that only the first element in the array has a value and the other elements are all null?

If the second case is what is happening, look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added. 


Yes sir, it only displays one element within the array and I believe that this is attributed to the fact that each time the add method is called, i is initialized to 0 instead of incrementing to the next index index/element within the array.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  it only displays one element within the array

Strange.  How are you displaying the contents of the array?  Try using the Arrays class's toString() method.  It will show all the elements in the array:
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
  it only displays one element within the array

Strange.  How are you displaying the contents of the array?  Try using the Arrays class's toString() method.  It will show all the elements in the array:


I think the problem stems from my add Method Norm, even with your suggestion, the last element added to the array is the only element being displayed whilst the other elements are null.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From an earlier post:
look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added.


Did you check that the index is being incremented every time an element is added and that the index is not being reset to zero between adds?
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:From an earlier post:
look at how the index used for adding elements to the array is kept safe (not reset to zero every time it is used) and incremented after each element is added.


Did you check that the index is being incremented every time an element is added and that the index is not being reset to zero between adds?


I'll give that a whirl. Thanks for the suggestion Norm
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, I finally got the add method to work the way it should be working. However, I am still having problems with the remove method.



Adding break allows me to remove 1 occurrence of an element within an array and not all occurrences. If I remove break, then I get a NullPointerException. Any ideas? Thanks!
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the values of the flowerPack array can be null, then dereferencing an element of the array to call the equals() method will throw a NPE when the value is null
There are a couple of ways to handle that:
1) reverse the order of the variables:  flowerType.equals(flowerPack[i])
2) test for null before calling equals: flowerPack[i] != null  && flowerPack[i].equals(flowerType
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:If the values of the flowerPack array can be null, then dereferencing an element of the array to call the equals() method will throw a NPE when the value is null
There are a couple of ways to handle that:
1) reverse the order of the variables:  flowerType.equals(flowerPack[i])
2) test for null before calling equals: flowerPack[i] != null  && flowerPack[i].equals(flowerType


Thank you very much. Why does something so simple as reversing the order of the variables work?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why does something so simple as reversing the order of the variables work?

It works when the variable used to call the method is sure not to be null.  The argument for equals() can be null.
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
Why does something so simple as reversing the order of the variables work?

It works when the variable used to call the method is sure not to be null.  The argument for equals() can be null.


Got it, thanks so much Norm
 
Naziru Gelajo
Ranch Hand
Posts: 175
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm working on my sorting method now and I can't seem to use either a SelectionSort or InsertionSort because there are null elements within the array. How does one sort an array using those algorithms without getting a NullPointerException?

 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write a helper method that does the compare the way you want. Example:

Then use that in place of your call to compareTo(). Of course your logic will have to deal with nulls in whatever way you feel is appropriate.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A couple of ideas: Make a separate first pass that
1) Either moves all the null values to the end of the array and then  sort the good values in the front of the array
2) Or replace the null values with a constant high value

EDIT:  Carey's idea may be better.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!