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 Problem with indexing of an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with indexing of an array" Watch "Problem with indexing of an array" New topic
Author

Problem with indexing of an array

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi all,

I have an array


i am creating the array with this



Everything in my program is working now except, when i print out the Array it is printing this



the id is not being indexed, and comming up as "0" for each box now.Is there any way i can fix that ?? possibly in the print statement maybe?I am having a difficult time with that part anyway as far as where to put things to show up in the right place when printed out , as i would like to have [box:1, height:2,width:3,length:4 ] Something resemeling that anyways.


Thanks for your help
Mike
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

Well, I see a few problems. First, let me ask, are you trying to get user input through a GUI, but put output into the console window? That's what I see with the combination of JOptionPanes and System.out.println(). Also, there's no way the output you show, is coming from that System.out.println(), so where is it coming from?

You have a number of loops with empty bodies. At best, they do nothing, but at worst, they cause endless loops. For example:


and


Why are those there?

You also have a boolean called weiter, which I don't understand at all.

I think you should break down your problem into smaller pieces. Get one small part working, then add features one by one until you have want you want.>
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Also you've a do-while loop which must programmers don't use. The JavaRanch nitpicking code style guide forbids it. And you're using recursion which isn't really necessary and only complicates the method.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi greg,

Thanks for your answer.I have the System.out.println(); Just for me to see what is going on and won't be there for the user to see.The "for " loop was an attempt to fix my problem at the moment,
it probably doesn't do anything was just the last attempt.

is so the loop continues until the user inputs a number larger than zero as zero isn't allowed

and i am printing the array when the user picks the choice "show all boxes"

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2851
    
  11

The problem with that while loop is that there isn't any body to it. The body is where you would tell the user that 0 isn't allowed, and prompt him to enter another value. There's also nothing that changes the value boxes[id][1], so if it starts at 0, you're program will stay in the loop doing nothing forever. That's what I meant by an endless loop.

Many beginning programmers have a mindset that they should just keep throwing stuff into the pot, and see if something works. To some extent experimentation is a good thing, but in this case I think you need to build from the ground up. Try to get simple input and output working, then try to read enough input for a single box, then try to extend your ideas to multiple boxes if that's what you want to do. I've been a professional programmer for 15 years now, and I still develop that way.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

mike ryan wrote:the id is not being indexed, and comming up as "0" for each box now.Is there any way i can fix that ?? possibly in the print statement maybe?I am having a difficult time with that part anyway as far as where to put things to show up in the right place when printed out , as i would like to have [box:1, height:2,width:3,length:4 ] Something resemeling that anyways.


Where do you set the boxes[id][0] = <some_id> it your code ?? isn't the the index field ??


Regards, Rene Larsen
Dropbox Invite
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Greg,

Thanks for the advice, and i will work on the flow of that Method,it works basically except when the user inputs a zero the loop starts at the beginning and i would like it to repeat the last JOptionPane input.But as far as getting the inpout and saving it , it worked with proper input and saved it. The problem with the printing i think is because the id is [id] <---- like so , so i thought just writing id= boxes[id][0], would have taken care of it but i t doesn't, so i think instead of using id=IntegerParseInt(JOptionPane.showInputDialog() , i need to use "boxes[id][0]". Looking around the net i see lots of different ways of doing a while loop or a do while loop, which i guess is considered bad practice on this forum, or maybe in General? or is there a time and a place for that too? Saying do{ whatever -------.-----joption do something else while(!something)else do next?
as you said about experimenting , i did do allot of that and i actually did learn allot doing it as well as also getting myself confused to a poin where i had to re-do everything to get it working again. I have in every Method ATM System.out.println(), to show me how and what is being saved and what isn't and what is happening in General which was sometimes pretty interesting and not at all like i thought it would print sometimes.
I will get the while statements fixed if i can and show what i came up with and see if it is a better way or the proper way or not...



Mike
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

while i work on the flow of the method,I would like to get back to my original question,if it is possible to print the first number "the box id number" <--- see original post.

I would like to have that working as well as getting the method loops working properly.


Thanks
Mike
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

As i ask you about in my post: "Where do you set the boxes[id][0] = <some_id> it your code ??"

Please tell ...
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi rene,

i tried it all over now, in the main, in the show all method where i print the array, and also in the creat box method.I had boxes[id][0]=id, but didn'T work or do you mean something totally different?

Thanks
Mike


PS; Sorry i overlooked your post, i have messed with this for umpteen hours and am a bit frustrated to say the least lol.
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

What do you mean with line 4 ?? what do you expect ??

You get the ID in line 15 - which you then need to set in the array at [id][0]
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Rene Larsen wrote:What do you mean with line 4 ?? what do you expect ??

You get the ID in line 15 - which you then need to set in the array at [id][0]


oh my lord do i feel like a complete idiot I actually tried to change the

to


And tons of other things , but not adding that after it was called in that method!?! Sorry for the stupid question once again !!! I will get a handle on this yet.... i hope!!!

Thanks Rene!!

Mike
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

Just remember that you still need the ID for the other positions in the array...

One solution could be:

Why do you need the id as input parameter to the method ??

What does 'weiter' (continue) do - where do you use it ??

Why do you call the method recursive if a NumberFormatException occurs ??
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi,

weiter is a boolean that was missplaced at the time i copied that part.I have it set as boolean weiter=false; "weiter" is german for continue.

then i had wanted to use it to make continue either true or false which i suppose would be better used as



I don't really know what recursive means to be honest, i was trying to catch any possible false inputs and to keep the program running to continue on with the rest of the methods.I am still a real newb
and have tons to learn(if that wasn't already painfully obvious). And doing lots of googling and reading and trying to get this program to run properly.
I am now working on getting that part of the code corrected to work and look better.I thanks you for your tipp, and i have to say i knew that i needed to set the id to boxes[id][0], but had it in every spot except the one that counted ;) which by the way almost drove me insane!!!So thanks for saving my sanity fo´r another day ;)

I am doing allot of hit and miss at the moment and sometimes it has gotten nerve racking in the process, but in the end i have learned allot about the java language already that i won't soon forget, so that isn't a bad thing.I think i don't "think " like a programmer, and that is probably something i need to learn (i hope it is possible to learn for me).I am taking a correspodence course in German , and i am american--> living in Germany.So i think some of the explanations in the book aren't comming through as well as they would in English.But the fight goes on ;)


Thanks
Mike
Rene Larsen
Ranch Hand

Joined: Oct 12, 2001
Posts: 1179

I don't really know what recursive means to be honest, i was trying to catch any possible false inputs and to keep the program running to continue on with the rest of the methods.I am still a real newb and have tons to learn(if that wasn't already painfully obvious). And doing lots of googling and reading and trying to get this program to run properly.
To do a recursive call means that you are calling the method you are in (see line 73), and when this/these new call(s) finishes, you'll step backwards again, and return to the first calling method. In some situations a recursive call make sense - but not in your case.

I am taking a correspodence course in German , and i am american--> living in Germany.So i think some of the explanations in the book aren't comming through as well as they would in English.But the fight goes on
Yeah, the German language can be a bit hard to learn - but when living in Germany you should have the best opportunities of learning it I'm from Denmark my self, but living in Sweden.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with indexing of an array