This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes I need Help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "I need Help" Watch "I need Help" New topic
Author

I need Help

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
I've been tinkering at this programme for a while now, but i seem not to be making any more progress,....

the programme is supposed to ask for integers from the command line until "stop" is entered,....and then give out which integers were entered and how many of each integer were read in....

whene�ver i incorporate the part containing the Array "elementsCorrected" i always get a number format exception thrown out! why is that?

here are my programme code pieces, where am i going wrong?

thanx!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Your enlargeArray method means there are always more spaces in the array than there are members. So one of the elements will be null. Hence the NumberFormatException.

CR
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Instead of "enlarging" an array (making a new array that's bigger, and then moving each element over), you should really consider an ArrayList. You might think of an ArrayList as an array that automatically re-sizes itself.

I know that you've asked about ArrayLists in another thread, so I'm curious why you're using an array. Are you required to use an array here? Do you have more questions about using an ArrayList?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if you don't want to use an arraylist, here's a way for you to get your input
into an undetermined-length array.

Don't think it's real efficient, but might suit.

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
@Campbell Ritchie...
I took care of this using this(see code):

but thanx anyway.

@marc weber....
thanks for the ArrayList explanation. This is exactly what i was looking for, that notwithstanding i think solving the problem "manually" with Arrays first will help me understand the solution with ArrayLists better(dont u think?)....

@Michael Dunn....
wonderful!...you really helped me there.

I've solved the problem! *GGG*

thanx all!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38057
    
  22
Mark Weber is right about the ArrayList.

I don't think h = i actually sorts out your problem. Using i when number.equals("STOP") still gives you an out-by-one error. Hence the Exception.
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
Originally posted by Campbell Ritchie:
Mark Weber is right about the ArrayList.

I don't think h = i actually sorts out your problem. Using i when number.equals("STOP") still gives you an out-by-one error. Hence the Exception.


hmm,...
okay, lets go through this then:

lets take an array of 5 elements as example....
( e.g.: String[] elements = new String[5]; )

then we begin with the entries.....

1st entry: x1 -> position in Array : "0"
2nd entry: x2 -> position in Array : "1"
3rd entry: "STOP" -> position in Array: "2"

now this is done:
h = i; ....
then: String[] elementsCorrected = new String[h];

then we do:
elementsCorrected[j] = elements[j]
"elements" is now an Array with (h = 2) elements...
---> (for 0 <= J < h ) !!!

in any case it did work for me....however: only when i did this afterwards:

elements = elementsCorrected; break; (and voila! NO NullPointer or number excepions )

as against:

return elementsCorrected;...as commented in my earlier post (still dont know why though :/ )

but it worked *lol*
[ June 01, 2006: Message edited by: Wolfgang Obi ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: I need Help
 
Similar Threads
Resetting the array size
need help
applying ArrayLists
Arrays again :-(
passing a int array[][]