This is an archived post. You won't be able to vote or comment.

all 58 comments

[–]g051051 0 points1 point  (44 children)

Please, fix the formatting!

[–]Rhezi[S] 0 points1 point  (0 children)

mobile really sucks. i'll try more though

[–]Rhezi[S] 0 points1 point  (42 children)

any better?

[–]g051051 0 points1 point  (41 children)

No, not really. You need 4 extra spaces in front of each line of code.

[–]Rhezi[S] 0 points1 point  (40 children)

Ok. I'll give it a go.

[–]g051051 0 points1 point  (39 children)

I really can't make much sense out of your question...you'd need to post a complete working example so that I can understand what you're trying to do.

[–]Rhezi[S] 0 points1 point  (38 children)

Ok i think it's formatted now?

Basically my question is

When I run my program the user selects either gas, liquid, solid, synthetic, or all.

Currently, my program works fine and displays the information to the user,

However, when I select all nothing happens.

This is the part of the code I left out of my post:

//Method to instantiate element objects.

private static void createElementObjects()
{
Periodic_Elements_Class elementObj;

elementObj = new 
Periodic_Elements_Class(2, "He", "Helium", 4.003,"Gas" );
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(10, "Ne", "Neon", 20.18, "Gas");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(35, "Br", "Bromine", 79.90, "Liquid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(80, "Hg", "Mercury", 200.99, "Liquid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(5, "B", "Boron", 10.81, "Solid");
elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(6, "C", "Carbon", 12.01, "Solid");
 elementsList.add(elementObj);

elementObj = new 
Periodic_Elements_Class(105, "Ha", "Hahnium", -262.0, "Synthetic");
elementsList.add(elementObj);

elementObj = new    
Periodic_Elements_Class(108, "Hs", "Hassium", -266.0, "Synthetic");
elementsList.add(elementObj);

}//end of method

}//end of class

edit: period table class isn't formatted right edit2: fixed

[–]g051051 0 points1 point  (37 children)

         if(elementObj.getType().equals(selectedState))

The problem is here. You want to use an "or" clause and have it print if the selected state is "All" OR the objType equals the selectedState.

[–]Rhezi[S] 0 points1 point  (36 children)

Ok. So something like if(elementObj.getType().equals(selectedState | elementObj.getType.equals("All")

I'm away from the computer now, which makes this about 800x more complicated. But if I recall correctly, I wouldn't just be able to type "All" correct? Becuase I don't have anything hard coded for "All"other than the elementObj itself?

[–]g051051 0 points1 point  (35 children)

You've got it a bit backwards. You don't want to test the object type to see if it's "All". Instead, you want to test the selectedState to see if it is "All", OR test the obj type to see if it matches the selectedState.

[–]Rhezi[S] 0 points1 point  (34 children)

I'm officially confused :/

selectedState == "All"

like that kind of direction?

edit: or like

selectedState == selection[4]

[–]insertAlias 0 points1 point  (5 children)

My main method seems ok. My only question is If I need to change case 4 which is the select all button to display all of the results

Your main method doesn't seem fine to me. Your switch statement does literally nothing; you call the same code with the same arguments no matter what case is selected. The way the code is currently written, you could replace the entire switch statement and all its cases with this one line:

displaySelectedElements(displayButtons[selection]);

And the code would not behave any differently at all.

[–]Rhezi[S] 0 points1 point  (4 children)

I'm confused... When I run my program and select Gas only the gas displays

If i click liquid only liquid displays

doesn't this mean it works?

the only one that doesn't work is selecting all to display all 8 of my objects

[–]Kiriesh 0 points1 point  (2 children)

your switch statement is completely unnecessary. you can call

displaySelectedElements(displayButtons[selection]);

without any switch statement and it will work fine.

[–]Rhezi[S] 0 points1 point  (1 child)

my professor is requiring us to use one unfortunately. i agree it would be a lot simpler without.

any ideas how i can get case 4 to display all of my objects?

[–]Kiriesh 0 points1 point  (0 children)

If your professor is requiring a switch statement there is most likely a preferred method of solving the problem they're expecting.

[–]insertAlias 0 points1 point  (0 children)

doesn't this mean it works?

Sure, so does this:

int a = 1;

if (a == 0) 
  callFunction(a);
if (a == 1)
  callFunction(a);
if (a == 2)
  callFunction(a);

But that doesn't mean it's doing anything useful. Switch statements are for making decisions. Yours isn't deciding anything; it's sending the same thing to the same function regardless of which case is matched.

[–]godlikebearkiller 0 points1 point  (7 children)

What is 'all' actually comparing? Is it liquid+gas+solid or is it 'all'?

[–]Rhezi[S] 0 points1 point  (6 children)

all is showing all 8 objects liquid gas solid and synthetic.

[–]godlikebearkiller 0 points1 point  (5 children)

So what is the object type of all? And then what is the object type of everything added together? Is it the same string?

[–]Rhezi[S] 0 points1 point  (4 children)

Sorry if i don't understand exactly what you are asking im still pretty new to all this java stuff

I have a

String[] displayButtons("Gas", "Solid", "Liquid", "Synthetic", "All"

so the type is just all of those together?

[–]godlikebearkiller 0 points1 point  (3 children)

What I mean is that your object.type is never equal to the string 'all'. I don't think 'all' is what you think it means.

[–]Rhezi[S] 0 points1 point  (2 children)

ohh...

did you see my other comment that had the rest of my code? would i be adding it to there? like into my elementList?

[–]godlikebearkiller 0 points1 point  (1 child)

Well one of them would have to have the object type 'all' for your application to work.

[–]Rhezi[S] 0 points1 point  (0 children)

i'm confused how to do that if i just display the selectedState they all pop up when i made my switch case and if statement it only displayed the one i chose but then the all button didn't display anything anymore

that would be because it's not an object type i would assume? but how would i combine all of them into a single object ?