This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes using Arrays to store info Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using Arrays to store info" Watch "using Arrays to store info" New topic
Author

using Arrays to store info

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi all,

So i have an assignment for my course and i need some help as arrays are giving me real problems!

I have to creat a storagesystem to store boxes using multidimensonal arrays.The height,length and width of each box has to be stored as well as the volume of the box.Each box has to be assigned a number (each number of course has to be different to distinguish them).

the program should be able to allow to add a new box, delete an existing box,change parameters of an existing box,show info on any one box and a listing function to list all current boxes.


I have NO IDEA how to even start....can anyone maybe give me a kickstart? maybe a small code snippet on how to create the arrays needed or maybe a link to a tutorial that may help me out? I basically know how to use the JOptionPane to type the names in and so on, as well as using if loops and Switch statements to add the necesarry options (delete, insert, list)and so on. Basically it is the initial Array and variables i don'T understand well at all...

any help to get me started would be greatly appreciated( am i the only person who doesn't seem to understand Arrays AT ALL? ok i understand some of it but mostly not lol ).

Thanks
Mike
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi Mike,

Is arrays is what you require?
we can store the details of the box in a box object. This way the code will be more managable.

Regards,
Sudipta Kumar Laha
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

HI Sudipta,

It says "Save the Data in a multidimensional array, Create this array locally in the main() method and not to use Class Variables" and the Data should only be saved while the program is running and be able to save up to 100 boxes". I would write my course Teacher a note to ask for a little help but i can't log into the site since last week when they created a new site and the log in is broken;(---> maybe wasn't a good choice for a Programming course after all rofl..
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051


I have to create a storagesystem to store boxes using multidimensonal arrays.The height,length and width of each box has to be stored as well as the volume of the box.

Mulytidimesional array as you mentioned will be solution



Each box has to be assigned a number (each number of course has to be different to distinguish them).

set,Hashset,TreeSet wil solve this problem


the program should be able to allow to add a new box,

This can also be achevied using set.


delete an existing box,change parameters of an existing box,show info on any one box and a listing function to list all current boxes.

But i am afraid to say that how can we achieve this using hashset.because if we change the parameters of box then hashset will be corrupted


SCJP6.0,My blog Ranchers from Delhi
Sudipta Laha
Ranch Hand

Joined: Aug 23, 2010
Posts: 49
Hi Mike,

You can implement a two dimentional array which may be used to store the height,width and length of a box.

In the below code we are creating a array of 100 box each containing some height,width and length.




May be this way you can implement. May this will help you. You can use the [i][0] element to distinguish between different boxed.The delete and search will depend on what specific parameter the delete/search will happen.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

mike ryan wrote:...The height,length and width of each box has to be stored as well as the volume of the box...

These are probably float or double values, right? So if you have a double[] of length 4, that could represent a box (where the fourth element is the product of the first 3 elements).

In Java, multidimensional arrays are just arrays that contain other arrays as elements. So if you create an array of length 100, where each element is a double[] of length 4...

(As far as unique numbers identifying the boxes... That might simply be the index of the 100-element array.)


"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
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

hi,

thanks for the tipps so far everyone;)
at --> shanky sohar

I haven't learned up to this point anything about set/hashcodes or treeSet so i think this isn't what i should be using.Thank you for the answer though!!

at ---> Sudipta Laha,

This looks like a start to what will need to use, at least in some form of that possibly



I don't know... This Test seems a bit complex for the workbook i have just finished really .There were only like 2 pages about Multidimensional arrays in it, or maybe it is due to the fact that i don't fully understand them as i maybe should?This is the book for the 4th month of study, this is my fifth week as i went through the first 3 rather quickly and had no problems answering the tests on those.

i think i will have to use switch statements for the box name and the height, length and width in order to keep the program running and to allow up to 100 entries.But i will firstly need to figure out exactly how i can do the original array,in a way that it can be changed with an input, or a new box added with an input......oh boy this may take a while

at---> marc Weber

I think i can pretty much use whatever length height and so on as i wish, doesn't need to be a double, also it should figure out the volume of the box using the length*height*width(i beleive that is how i would get the volume?).The idea is good and thank you for answering ;)

I don't want anyone to give me the code completely by the way , the snippet from Sudipta was good to give me an idea but i am still not sure if everything i need to do is possible with that start?WOW my brain hurts

note: I am also an american taking the course in German hehe so this may be a possible missunderstanding on my part (but i don't think so...) i think it is possibly not explained properly or missworded if anything,or maybe it is supposed to be this difficult..






marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

mike ryan wrote:...it should figure out the volume of the box using the length*height*width(i beleive that is how i would get the volume?)...

Indeed, the volume of a box is length * width * height.

From your original post, I gather that the volume should be stored as part of the box (as opposed to calculated "on demand"), so you will probably want an additional array element for that.

It looks like you're on the right track. Show us your code when you get it more developed.
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Hi Mike i'm still new to Java so this might not be very helpful but...

Could you not just create your own Box class with instance variables for it's height and width ect, and create get and set methods for each instance variable so you can access them then store the Box objects in the array? Maybe that's what your instructor meant but maybe not, just thought there was no harm in suggesting.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

at---> neil,

I have to use a multi-dimensional array and we haven't got the (Get/Set) yet.Thanks for the input;)

So...

I am still basically totally lost as to how to achieve this after reading a bunch of things about arrays today.

I wonder if i can create an array like


and then a switch statement to be able to change one of the dimensions of the box??But then how do i create a new box? oh lord i am lost and i think i am confusing myself the more i go along now lol.
any ideas?? I really need a kick start here , i am making myself nutz on this



[/code]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
If you are inserting your name, then you won't get it into an int[][]. To alter a number, it would have to be numbers[1][2] = 3; but make sure to use the correct indices, otherwise you will suffer exceptions or incorrect results.
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
OK sorry i wasn't more helpful before. But i've now written a program that i think does what you are after and i think it obays the rules of your instructor. So i'll be back to help later if you are still stuck but right now the final of big brother is about to start! so later...

mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Thanks Campbell,

i am still thinking of using a derivative of this



But of course i can't have it just run up to 100, it will have to go in increments of 1, as one is added.But is it possible to change the name of the box?with JOptionPane input?possibly?

maybe with another variable like



hm, i will have to mess with this idea i think, just have to then check that the name and number haven't been used there somehow too.

Could this possibly work?

Thanks once again everyone, at least i am comming up with some ideas (good or not )
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Hi here is a gif of me using the box program i wrote. If this is the kind of thing you had in mind i can give you some clues as to how to create it without telling you too much. I'm not sure if you can upload gifs here so this is on my myspace photos.

by clicking here EDIT that link no longer works see more recent post for link.

In this version of the program it gives the user full choice of what the box ID number should be, they then enter the height, width ect and it calculates the volume. You can then add more boxes or delete a box. You can enter a box ID to get information about the box. It uses no instance variables and all info is stored in a multi dimensional array.

Let me know if this is the kind of thing you are after. I think this is a better option then using a for loop personally.






mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Niel,

That looks like exactly what i need to do!! BUT , i don't want to see the code for it I want to figure it out myself if i can, it may not be as nice in the end but i really need to figure it out to learn it.
However i like how it works,and thank you for showing that to me although i think i will be upset about mine now LOL.


Thanks
Mike
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Don't worry i wasn't going to show you any code. It's a tricky thing though getting the balance of helping someone without saying too much. I guess the following could loosely be described as sudo code.

Show an input dialog that gives the user the option of either creating a box, deleting a box, or showing information about a box you have already entered. Also give them the user the option to quit. The user enters a number to choose.

If the user enters 1 (the create a box option) ask the user to enter the number of the box ID.

Use the multi dimensional array's Rows as the box ID. So if the user enters that they want the box ID to be 1 you store the box ID number in boxes[1][0] . Because the first [] is for the rows. You store this number so you can display it later.

Then ask the user for the height, width, length and you can use those to work out the volume. You could store each of these values in the columns of that row. for example boxes[1][1] could store the height and boxes [1][2] could store the width ect.


Once you have got the code for storing boxes you can then work on a way to display the values.

All this will need to be in some kind of loop to carry on until the user wants to terminate the progam.

This is obviously very similar to what you already have but you now give the user the chance to input the values. Sorry if it's telling you too much... or too little!
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Niel,

Thanks, and i will get on this as soon as i get home from work;) you didn't tell me too much as i kind of understood it when i saw it running.Now i just have to figure out how exactly to do it.


Mike
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Neil Cartmell wrote:Hi here is a gif of me using the box program i wrote. . . .
That looks good, apart from the interesting unusual creative and original arithmetic it demonstrates.


5 × 5 × 5 = 150
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
mike ryan wrote: . . .
Thanks once again everyone, at least i am comming up with some ideas (good or not )
#

But of course i can't have it just run up to 100, it will have to go in increments of 1, as one is added.But is it possible to change the name of the box?with JOptionPane input?possibly?
. . .
Please avoid plain text and # signs in your quoted code. Please space and indent your code correctly, and avoid long lines because they are difficult to read. I have had to add some new lines to your quoted code. (And those formatting errors will lose you marks.)

That for loop will run exactly 100 times. But you ought not to hard-code the number; you should use the length field of the array.

I have already told you: you can't fit the name of your box into an int[][]. Only numbers.

You realise the way you have written that for loop, box no 20 (which is the 21st box) will have dimensions 21 × 21 × 21? If you always have specificNumber and i equals, why do you need specificNumber at all?
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Campbell Ritchie wrote:
Neil Cartmell wrote:Hi here is a gif of me using the box program i wrote. . . .
That looks good, apart from the interesting unusual creative and original arithmetic it demonstrates.


5 × 5 × 5 = 150


lol Damn it i was hoping no one would notice that. The new version is fixed and works but didn't think there was any need to upload it. That's embarrassing!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Neil Cartmell wrote: . . . lol Damn it i was hoping no one would notice that. The new version is fixed and works but didn't think there was any need to upload it.
That'll teach you to post a link in the hope nobody will look at it.
That's embarrassing!
. . . and recorded on the internet, where it will remain for everybody to see for ever.

We all make mistakes. One of the perks of being a moderator is being able to notice others' mistakes and hide one's own
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
Ok i had to upload another one that works. I know no one cares but i have to do it for myself! I'm sure you understand. lol

This one works i hope!

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Yes, that one uses rather more conventional arithmetic.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Campbel,

Yes sorry about the hash marks there , i had just copied it from a post earlier on in this thread and pasted

I will use a derivative of that code , I will get it started and see how far i get and will post the code and see what the general thoughts from you all are on my decision...
I will of course now have to make it nicer than Neils though lol so it may take me a while
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
If you use the quote button, you can delete the [quote] and [/quote] tags, and the code will reappear ready to amend. No # marks or anything. It appears to be a problem with the code that # marks appear if you copy and paste. As an alternative, use the "view plain" button and copy form the pop-up window.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi ,

Thanks Campbel i will remember that next time;)

ok i have started my program and would like to know if i am on the right track so i will post here what i have so far(no laughing ;))




There are allot of things i still need to figure out of course, like how to check the box number to see if it has been used, and how to create a list of all the boxes.Also how to get a specific box number to change a parameter of it and and and... If anyone has any tipps or suggestions for my current starting code please feel free.I am sure there are a number of mistakes there to find lol.
Neil Cartmell
Ranch Hand

Joined: Feb 13, 2010
Posts: 150
OK before i start let me just say i'm still relatively a noob and i've never had my own code critiqued by anyone so i'm really in no position to critique anyone elses so please take what i say with a pinch of salt. But i'm sure an expert will be happy to point it out if i say something REALLY stupid! lol

But is there really any need for the for loop that is automatically generating the height, width ect for each box? I mean you are going to have to create methods to add those things later anyway as you need to be able to change those values for each box. I think it just confuses things having it in. And from what i've seen of your instructions it doesn't say you need to automatically add the width and height and length.

I did something very similar as you with regards to while loop and having an add box method. But at the moment your add box method doesn't actually add the box to the array. Maybe once the user has chosen to add a box inside the addBox method you could ask the user for the height and width of the box.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Why are all your methods static?

Can you write a switch on eingabe?
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Campbell,

I wrote static cause most of the ones in my book are also written that way, so i figured it is normal = i need to google static in java i think.

Yes i could do a switch.I already have started rewriting the code again as i didn't like a few things and have it well messed up anyway, so i can go with switch yes.

at--> Neil,

Thanks for your input as well and don't worry about hurting my feelings at all, i am here to learn and like any feedback i can get.

I hope this thread won't start annoying people because of the constant questions.I think i need to do some heavy reading up on a few things tomorrow before i continue this though.and getting up at 4:45 isn't doing my thought processes any good either

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
A lot of books cram everything into the main method, which is static. But object-orientation works better with instance fields and methods, which you don't call static.

You need to find a book which teaches object-orientation as well, eg Sierra and Bates (Head First Java) 2nd edition, Deitel and Deitel (6th or more recent edition).
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
mike ryan wrote: . . . getting up at 4:45 . . .
That's normal for computing undergraduates . . . assuming it's the 4.45 just before tea-time.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

ok well...

I think i will just hang myself this weekend ok just a joke but i am still rather lost on how to do this code....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
mike ryan wrote: . . . Create this array locally in the main() method and not to use Class Variables . . .
That sounds like very poor design. You ought to have as little as possible in the main method, and I suspect somebody is confusing Class variables (which are correctly called static fields) with instance fields (also called non-static fields).
Using JOptionPane as your principal input device is a very old-fashioned practice, but I sometimes still do it myself.

You are still starting with BoxNumber being copied from the array. Remember the default contents of the array are 0 0 0 0 etc, so you setting BoxNumber to 0. And change the B to b, etc.
If you have a 100-member array, it's not 1 to 99 but 0 to 99.
As I said before you are setting the size of box no 20 to 21 ×22 × 23. That for loop actually runs 00 times regardless, setting up the next box, and showing your input dialogue. If you pass 1, you get to the break; command which ends the loop. You actually want a while loop comparing the input against 6 for exit. That will repeat until you enter 6 or hang yourself whichever is sooner.

Follow marc's earlier suggestion about how to organise your array and use the index as box number.
Get something working like that, simply entering the numbers of the box, and its sizes. You do not want to pass those values as a parameter to the method. Rather, you want to pass the array. Then you can alter the contents of the array. You can also print what you have entered, and see whether it has worked.
Do it in little bits, and only try one part at a time. Leave the other parts until the first bit is working.
mike ryan
Ranch Hand

Joined: Aug 08, 2010
Posts: 210

Hi Campbell,

I have since changed my code to use a switch. here is what i have together so far.I have been working it out myself and got it to print the input using System.out.println, so i think i can use it like this?



But what i still don't get is how to save this and then add to it up to 100.If i use the

it just runs up to 100 which i don't want.and once i get that saved then how do i save it to the Method Main? The instructions say to save it locally in the Method Main, and not to use Class /Static variables i guess that means. Please let me know if what i have so far can be used for what i need,and maybe give me a hint on saving to the main?


Thanks
Mike (still holding the rope just in case)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
You put those details into the argArrays array in the AddNewBox method. Which should, by the way be spelt a not A. You don't do anything with box in that method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using Arrays to store info
 
Similar Threads
LinkedList class problem
Capturing selected check boxes
Submitting only selected textboxes in a JSP
google competion question...
Project Help