Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Arrays again :-( Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays again :-(" Watch "Arrays again :-(" New topic
Author

Arrays again :-(

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
I'm trying to write a programme that'll create an Array, fill it up with elements, and then tell you....

1. the elements that were entered.
2. how many times each element is contained in the Array.

the user is prompted to enter the numbers one by one....

I've been going round in circles, and to be honest i dont know where to start. This is the code so far (i know it's a bit nonsensical, but this is all i've been able to come up with so far...) wo can help me here???

PS: "IM" is a class (contained in my source directory) which contains methods that enable you to read in integers, strings, chars etc from the command line....

who can help please? I'd really appreciate it!
thanks!
Stephen Foy
Ranch Hand

Joined: Oct 17, 2005
Posts: 143
Fairly simple to achieve what your doing...



Stephen Foy - Microsoft Application Development Consultant
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> I'm trying to write a programme that'll create an Array,

do this only.

here's your code stripped of everything else



there are a couple of ways to create the array, but you've put them both
together so they won't work.
1)
public static String[] elements = new String[99];
creates an array, with 99 null String elements.
you should now have a method loadArray(), to iterate the elements putting
data into each element.
2)
public static String[] createArray(){
returns a String array, created within the method, but you haven't used the
return value for anything, just
createArray();
normally it would be
elements = createArray();
but if you do this, the structure of the createArrays() method needs to
change i.e. you have a String[] inside the method, you load the
'temp' String[], then you return the 'temp' String[]
so it looks something like this


some things to note from the above
1)
String[100] is 100 elements, 0-99 are their element numbers
2)
i < temp.length
is clearer than
i <= elements.length-1
3)
return temp;
returns the name of the variable with a type that matches the method signature String[]
return elements[];
will generate an error because [] is part of the data type

suggest, for testing purposes, you reduce the elements 5.
It won't do much at preset, but when you get it to compile and run without error
you add another method to printout the elements to confirm they match what
you input (and 5 elements to check is better than 100)

when all OK, proceed onto the next step of the requirements.
Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
oh, thank you so much, for the corrections!.
i still have one more question :

if i want the elements to be read in by the programme until the letters "stop" are entered shouldnt it look something like this?....



would it be correct to use this "if statement" like this?
and if not,...what would you recommend?

thanks again in advance....
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> would it be correct to use this "if statement" like this?

technically you can break out like that, but you leave all the other elements
at null, which will probably bite you later - perhaps using an ArrayList()
would suit you better.

elements[i] = IM.readString();
if(enteredElement == "STOP"){

has several problems though:
1) what if element[] is a non-string array?
2) comparing an entered string is done via .equals(), not ==
3) you may want to consider stop,STOP,Stop etc as all being the same
4) if you enter stop, you want to break before making element[x] = "STOP",
otherwise "STOP" might be included in your sort routine

perhaps something like this might work a little better

Wolfgang Obi
Ranch Hand

Joined: Dec 05, 2005
Posts: 134
great, thanks for the help Michael!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays again :-(
 
Similar Threads
I need Help
Howto generate unique random id
How to get a method to call a method
[Easy] Shift String
applying ArrayLists