File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes using while and if`s Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using while and if`s" Watch "using while and if`s" New topic
Author

using while and if`s

James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
I am trying to fill an string array 0f five elements with new string objects
using an array of defined ints as index`s at where to place the next String object.I have a for loop to iterate through the array of ints and a while loop for the purpose of placing the the object in the correct position. I find it difficult sometimes to understand how to implement while loops and if statements in the correct logical way.perhaps someone could help me understand better.here`s the code,it compiles and runs as the numbers in the int array stand at the moment, but the string array is left with empty elements


thankyou for your input in advance
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
just to add I have changed the class names so a i =new a (), should read
method i = new method();
Niki Nono
Ranch Hand

Joined: Mar 20, 2005
Posts: 256
i believe you dont need two loops.
why dont you just use a single for loop to iterate through the string array



if this is what you are looking for then great.
else please let me know otherwise.


Life called,so here I am.<br />Cheers<br />Niki.:-)
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
One thing that helps me is to start by writing a description of what I'm trying to do. I keep the Java syntax in the back of my head, but I don't concentrate on it too much. Often I write sentences in English (my native language) in order to better understand the steps I need to take to solve the problem. I strongly suggest that you develop some similar system to help you break up the problem at hand. What I'm saying is that you should write down the steps to take in your native language before you even start writing any code.

Then when you are trying to code it look at your written description. Pay close attention to words that indicate the different control structures in Java. For example, the word "if" will indicate that you need an if statement. The words "until" and "repeat" indicate that you need some kind of loop.

HTH

Layne
[ April 30, 2005: Message edited by: Layne Lund ]

Java API Documentation
The Java Tutorial
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
hi nikki and lynne thanks for the help,I may have missled you on the question I know it really doesn`t matter which element the object is placed in as each object will be identical.What I am trying to do though is place eachobject in the correct elememnt partaining to the int array numbers ie if the numbers in the int array were - {2,4,2,5,4} the first object needs to placed in element 2 of the String array then 4 in element 4 next number is 2 , second element is taken so add one place string object in element three fourth is five -is = to array size so 5=0 place object in 0 if empty and so on I have a pseudo code written for the exercise, but putting the above program to work leaves some of the elements empty.I can`t figure out why these elements don`t get filled
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Hi -

Layne's suggestions about the "while" and "if" is excellent advice.

Here's a modification to your original code that may (or may not ;-)) help clarify things for you (you'll notice I'm *not* doing the number mappings you suggest):


Note, too, that the loop "for (i=0 ; i < n ; i++) { ... }" is equivalent to:


'Hope that helps .. PSM

PS:
Here's the sample output:


You'll note that each element of array "strings[]" is successfully initialized to a different string value (which, I presume, is what you wanted). You'll also notice that I'm not bothering to check for "null": either I've initialized the value, or I haven't.

PPS:
Don't forget that "element #2" of array "strings" is actually "strings[1]", and that element #4 is "strings[3]", etc.
[ May 01, 2005: Message edited by: Paul Santa Maria ]

Paul M. Santa Maria, SCJP
James .D.Johnstone
Greenhorn

Joined: Dec 08, 2004
Posts: 20
thanks guys for the help. The problem was staring me in the face.In the code I have posted the elements in the String array were being filled correctly. I just had the wrong println statment outside the while loop.I was printing a single line of the String array using the outer for loop as a counter which was printing the empty elements and not the elements that were being incremented by "current" if I printed Array[current] then I get the elements being filled and if I use a for loop in the same position using the outer loop as a counter then it fills as well.
I have written pseudo code and planned programs with single classes but find it difficult to adapt them when multiple classes are being used. Would someone be kind enough to explain how to go about planning a program with multiple classes
Paul Santa Maria
Ranch Hand

Joined: Feb 24, 2004
Posts: 236
Actually, the first question you need to feel completely comfortable answering is "what is 'a class'?" Once you're at ease with that, the question of what to put into that class and what to leave out and put in a different class, more or less falls into place.

Strong suggestion: please consider purchasing a copy of this book:

Thinking in Java, 3rd Edition, Bruce Eckel

It's an excellent buy. The complete text is also available as a free download on Mr. Eckel's web site:

http://www.mindview.net/Books/TIJ/

Good luck!

.. PSM
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by James .D.Johnstone:
hi nikki and lynne thanks for the help,I may have missled you on the question I know it really doesn`t matter which element the object is placed in as each object will be identical.What I am trying to do though is place eachobject in the correct elememnt partaining to the int array numbers ie if the numbers in the int array were - {2,4,2,5,4} the first object needs to placed in element 2 of the String array then 4 in element 4 next number is 2 , second element is taken so add one place string object in element three fourth is five -is = to array size so 5=0 place object in 0 if empty and so on I have a pseudo code written for the exercise, but putting the above program to work leaves some of the elements empty.I can`t figure out why these elements don`t get filled


First of all, my name is Layne.

Second of all, my previous post was a long way of saying that you should write out some pseudocode before typing in the program. Since you have some, my guess is that you have one of two problems: either the pseudocode is incomplete or you are strugling with translating it into Java. Either way, you should post the pseudocode and we can help from there.

Layne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using while and if`s