• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginner help using arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I've decided that I wanted to take a java programming class and I've run into an exercise I can't google my way out of.

For an assignment I have utilized a loop to:

- ask the user to input the name of salesman1
- ask the user to input the sales for acct1, acct2, acct3

Then i repeat the loop for other salesman

The objective is to calculate the total for each salesman and then the total for all salesman.

My question is:

1. Is there some way I can build an array to store salesman names? (salesman1, salesman2, etc)

----------------0-------------1---------------2
..........+--------------------------------------------+
Array | salesman1 | salesman2 | salesman3 |
..........+--------------------------------------------+


2. Use that array of salesman to each have their own array to store the sales for acct1, acct2, acct3?

------------------------0------------------1------------------2
.................+---------------------------------------------------------+
Salesman1 | sales for acct1 | sales for acct2 | sales for acct3 |
.................+---------------------------------------------------------+


------------------------0------------------1------------------2
.................+---------------------------------------------------------+
Salesman2 | sales for acct1 | sales for acct2 | sales for acct3 |
.................+---------------------------------------------------------+

and so on...

I am super new with java, and if I am going about this the wrong way let me know. Even if I should scrap all this and start new.
Sorry for the wall of text and I thank anyone that can offer help. I'm new to forums as well, so I don't understand why the formatting on my tables eliminated all the spaces... that is why my tables look so funny.

The assignment is already turned in, so I am looking for help with the concepts on how to accomplish this instead of just getting someone to do it for me.

The following is my test class code:



 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
matt leifi,

1) Why do you want to go for array when you have done it with arraylist. Continue with arraylist.

2) I guess the sales for acct1, sales for acct2, sales for acct3 are of the same object type. If my guess is right, store it in an arraylist and use Map to map it with the sales arraylist.
 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think you are going about things the wrong way. Start with the Salesman class, with suitable fields like name, sales. If you are supposed to have accounts, then you need an Account class, and the sales field might be of type Account. You can give the Salesman class methods; this is one possibility:-Be sure to provide the class with a toString method. Then you can test it like this:-You might do well to test it with incorrect figures e.g. -999. If you have an Account class do the same for that.

Forget about arrays, Lists, etc until you have the Salesman and Account classes working nicely. Then you can simply add them to an array, List, or whatever, later. Do this sort of thing in stages and it is much easier. I see you have had a Map suggested. Do you know anything about Maps? What you do depends on what the relation is between salespeople and accounts.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In java, an array can (and really should) only store one kind of thing. That could be names (as Strings), sales amounts (as Integers), or really, any other single object type you can think of.

And in this case, you could have a "Salesman" Object (that you have defined). That Salesman object type could have a name and an array of sales amounts.
 
matt leifi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you guys for responding.

Partheban Udayakumar, the arraylist that I wrote in my code above was me just trying to figure out the syntax. I really have no idea what I did there. If I make an arraylist for salesman, and then another one for sales (acct1, acct2, acct3). How would i go about retrieving the salesman from my salesman arraylist and the sales for the same salesman from the sales arraylist? You refer to something called a "map"... does a "map" associate a value from one arraylist to a value from another arraylist?

Campbell Ritchie. The idea of making a salesman class didn't occur to me since the instructor only specified that I have only a class for the parent "account" class and 3 child classes that extend the parent class (paperAccount, serviceAccount, suppliesAccount).

I'm not quite sure what a map is, after a google search on it, I think I am even more confused.

fred, I'll take your advice about arrays being all one type of object. Is it like that because that is a functional restriction, or is it good practice to not mix types in one array?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read all about Lists in the Java™ Tutorials. I have given a link to the whole“trail” so you can see how many different things there are in the Collections Framework.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matt leifi wrote:. . . all one type of object. Is it like that because that is a functional restriction, or is it good practice to not mix types in one array?

Both. If you have an array of Foos you can only put Foos into it. If you try, it is likely the compiler will notice and not let you. If you manage to sneak it past the compiler, you will suffer an Exception at runtime.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But let's say you ARE allowed to put different things into an array (in java you are not, so this is just a theoretical exercise). Also, something that many new people don't realize is that in a large software house, you will be using code you never wrote. In fact, you may never see the code, you are just told "this is what will happen".

So, you get an array. It is not uncommon for you to get it when you call a method like:

now, you want to look at everything in the array:

When I get to the "do something" line...can I print a person's name? Can I compute a comission bassed off the number? If anything can be in there, maybe I have a file handle. Or a socket connection. My code would have to have a bunch of


On the other hand, if everything in the array is a Salesman, then I know EXACTLY what I can do with ANYTHING I pull out of the array.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matt leifi wrote:The idea of making a salesman class didn't occur to me since the instructor only specified that I have only a class for the parent "account" class and 3 child classes that extend the parent class (paperAccount, serviceAccount, suppliesAccount).


Don't worry; it takes time to get used to. However, it's worth remembering that Java is an Object-Oriented language - although 'Class-oriented' might be better because the language allows you to define classes. And they're VERY useful, so don't be afraid to add one it if you think you need to.

the instructor only specified that I have only a class for the parent "account" class and 3 child classes that extend the parent class (paperAccount, serviceAccount, suppliesAccount).


Well that sounds wrong. Class names should always start with a CAPITAL letter, so they should be called Account, PaperAccount, ServiceAccount and SuppliesAccount.

And on that basis, a Salesman class might look something like this:I'll leave the rest to you, but hopefully it gives you an idea of what we're suggesting.

And then you can use either a Salesman[] or an ArrayList<Salesman>, whichever makes sense.

If you haven't tackled Maps yet and you find them confusing, don't worry about them for the moment. I'm sure they'll turn up in your course in a few weeks time.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Afraid I shall go into pedantic mode and tell you not to call subclasses child classes, even though lots of people do.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic