This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Arrays again :-(

 
Wolfgang Obi
Ranch Hand
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 143
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fairly simple to achieve what your doing...

 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 134
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
great, thanks for the help Michael!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic