aspose file tools*
The moose likes Android and the fly likes Array value getter question. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "Array value getter question." Watch "Array value getter question." New topic
Author

Array value getter question.

Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
I have six arrays that will each be a checkbox. Each will have 5-6 values, I want to be able to click two or three checklists and generate a random value the selected checklist box's values. Would creating a new temp array to hold the clicked categories values, and then pull the random variable from that array be the best route to go?

Thank you!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Welcome to the Ranch
Have you got that selection process working at the command line? You will find it much easier to implement a command-line version before trying to add the GUI. I am not sure I understand about the arrays being a check‑box. Draw a diagram of what you want, on paper. Write down in words of one syllable what you plan to do and how to write it in a way the box can do for you.
I don’t know whether a temporary array would help. There might be other ways to do it.
Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
Hi Campbell,

So to give you a better idea of what i'm working on, I am creating an app that allows the user to select up to six checkboxes(Restaraunt Categories) and will have an array for each category that includes values for each restaurant in the array. After pressing a "help me eat" button, I would like each selected array to be added together and the values transfered to a temporary array so I can then pull a randomized value from the new temporary array giving us a choice from the selected categories. I have the code close to what I need but integrating it with android is really messing with my understanding on the matter. My for loop to create the temp array is wrong and can't figure it out since it could be any combination of categories added together.

I've only taken one class in Java, and this project is a bit out of my understanding but i've spent the past few hours looking up more information about it and am unsure of what the best route to take is. So any criticism /help would be great.

Thank you so much!

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

Jeremy Packer wrote:... but integrating it with android ...

Moving this thread to a more appropriate section.


luck, db
There are no new questions, but there may be new answers.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Jeremy Packer wrote:I am creating an app that allows the user to select up to six checkboxes(Restaraunt Categories) and will have an array for each category that includes values for each restaurant in the array. After pressing a "help me eat" button, I would like each selected array to be added together and the values transfered to a temporary array so I can then pull a randomized value from the new temporary array giving us a choice from the selected categories
...
I've only taken one class in Java, and this project is a bit out of my understanding but i've spent the past few hours looking up more information about it and am unsure of what the best route to take is. So any criticism /help would be great.

OK, I'll try.

First off, you need to STOP CODING. You're getting bogged down in the implementation, when you haven't really thought through the problem properly.
Java is an Object-Oriented language, so you want to be using classes and Objects, not arrays and Strings. What you have written could be done just as easily in C or Basic.

Assuming that you have a description of the problem, one technique is to use lexical analysis, which is just a fancy term for going through the description and picking out the key nouns and verbs. Nouns will probably translate into classes or attributes, and verbs into actions or methods.
Of the top of my head, at the very least you have a Restaurant class, which has a name and a "category"; so why not a single array of Restaurant objects, rather than a pile of arrays of Strings? In fact a List (eg an ArrayList) would be even better - indeed, I'd be tempted to wrap it in a Restaurants (note the 's') class that has methods to pull out all the restaurants of a particular category.

BTW, Java has a wonderful type called an enum, which would be perfect for keeping your categories. It's descriptive, easy to create, and type-safe (very important, as you'll discover later; I won't bore you with the details now). The enum for your category might look something like this:and then when you need to use it (eg, for creating a Restaurant), you simply refer to one of the names:(reminds me of 'Demolition Man')

Do you see how much more descriptive that is than simply ploughing through arrays of Strings?

I have the code close to what I need but integrating it with android is really messing with my understanding on the matter. My for loop to create the temp array is wrong and can't figure it out since it could be any combination of categories added together.

Sounds to me like you're tackling too many things at once. Forget Android, and forget the GUI stuff, and get the concept working on the console first. Once you know that everything is working and you can select and combine restaurants the way you want to, THEN tackle the Android/GUI part.
That way you isolate the parts of the problem you're working on.

HIH

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jeremy Packer
Greenhorn

Joined: Jul 28, 2012
Posts: 7
Hi Winston,

So I decided to scrap what I had because I did get boggled down, so this is what I have right now. How would I create an arrayList from the category.Pizza enum?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Jeremy Packer wrote:So I decided to scrap what I had because I did get boggled down, so this is what I have right now. How would I create an arrayList from the category.Pizza enum?

Well, first you need to read the documentation for java.util.ArrayList (←click).
If you haven't covered Lists in your course yet (although what you're doing would appear to be advanced enough to guess that they have) have a look at the Java tutorials. Alternatively, stick to using arrays for the moment, but wrap them inside a class (eg, a 'Restaurants' class, as I mentioned earlier), so that all access to the array goes through class methods. This will make it easier to change the implementation later on if you choose to. The problem with arrays is that they have a fixed size; Lists don't.

2. You need to look up a good reference for Java naming conventions. This site's is here, and the one from Sun/Oracle is here.
Specifically: class, interface and enum names should start with a CAPITAL letter; fields and method names with a lower-case letter. Your program violates this several times, and also isn't consistent. It will still work, because it's not a language requirement; but it really "looks bad" to other Java programmers.

3. Unless you have a very good reason to do otherwise (and you don't), class fields should always be private. If you need to, provide public access to them via 'getter' methods (so called - again by convention - because they usually start with the word 'get').

4. Your field names all have 'place' as a prefix, which seems redundant. If they are private, as suggested above, the only place you will be able to use them is inside the class itself, so why not just 'name' instead of 'placeName'? Also, try and make them as descriptive as possible: If it's a phone number, call it 'phoneNumber'; acronyms like 'resNumber' are only going to be confusing to other people (like whoever has to read your code to grade it ).

5. Providing you initialize all your fields in the constructor, you don't also have to initialize them in place. This allows you to mark them as final, which means "once the Restaurant object is created, this field will never change", and is very useful for minimising changes to an object (a good thing). Example:(BTW, your class contains at least one spelling error. You must be absolutely accurate when you use identifiers and methods, because the compiler is NOT forgiving about mistakes like this)

6. The lines starting:
category c1 = category.PIZZA;
...

are totally redundant. Get rid of them. And get out of the habit of using short names for your fields. They will only make your code difficult to read.
Suppose that 500 lines down in your source code, you found the following lines:
Category someCategory = c1;
Category someOtherCategory = Category.PIZZA;

which would you find easier to understand? Which do you think other people are going to prefer to see?

7. You don't set the category field in your constructor.

8. The line:
public enum Category { PIZZA, SANDWICHES, MEXICAN, BBQ, CHINESE, ITALIAN; }
is a declaration; just like a class declaration (and BTW it should be 'Category', as I mentioned above).
There is absolutely nothing wrong with having it inside the Restaurant class (in fact I was going to suggest it), but you need to understand that what you have done is to declare a type within a type.
Inside the Restaurant class it is fine to just use 'Category', but outside it you will have to use 'Restaurant.Category'.
Again, there's absolutely nothing wrong with this, and it makes it very clear to other people what is being referred to.

Phew. Quite a lot to digest, I suspect; and I didn't even answer your question.

The declaration for your ArrayList will look something like:
List<Restaurant> restaurants = new ArrayList<Restaurant>();
...
restaurants.add( new Restaurant("Taco Bell", 2125551212L, Category.MEXICAN) );
...

but I strongly urge you to read the tutorials before you go any further.

Alternatively, an array declaration might look like:
Restaurant[] restaurants = new Restaurant[] {
   new Restaurant("Taco Bell", 2125551212L, Category.MEXICAN),
   ...
   new Restaurant("Olympic Pizza", 7145551212L, Category.PIZZA)
};


One last thing: You don't need a List for your enums. All enum types have a values() method that returns all of its values as an array. Again, you should read the tutorials for more information.

HIH

Winston
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7652
    
  19

Jeremy Packer wrote:So I decided to scrap what I had because I did get boggled down, so this is what I have right now. How would I create an arrayList from the category.Pizza enum?

And yet another final thing: you haven't stopped coding, you've restarted coding.

It's a common beginners' mistake, because you're just dying to bang out some Java.
GET OUT OF THAT HABIT.

In my experience, a good program is 80% thought and 20% code; and if I'm writing a program of any size or complexity, I would expect to have at least 50 pages of plans, descriptions, diagrams, mindmaps, and all sorts of other scribbles (not to mention several blunt pencils; but I'm old-fashioned), before I write my first line of code.

You can't write a program to solve a problem until you understand it.

Winston
 
jQuery in Action, 2nd edition
 
subject: Array value getter question.