Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

changing boolean values for a particular attribute in ArrayList  RSS feed

 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Guys,

I would like to know how to change Boolean values for a particular attribute in an arrayList. I don't now what to do and I am stuck.. I would appreciate any kind of help.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends what you mean by "attribute". That term doesn't have much meaning in this context.

Are you asking about replacing the value at a specific location in the list? If so, look at the javadoc to see if there's already a method that might be helpful.

If you mean something else, you'll have to put a bit more effort into explaining what that is.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to the Ranch!
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its better if I show u.

0->Flask [brand=Argos, includedCups=9, isWatertight=true, capacity=9.0, intendedLiquid=water, hasInsulation=true, netWeight=9.0, isExhibited=false]

The above is my arrayList and I want to change the isExhibited= false into isExhibited=true. I don't know how to do it.




Well, I came up with something. But I don't think its right.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question really has nothing to do with lists. It's just about changing the value of an attribute on an object. Does your Flask class have a setExhibited() method? If so, that would probably be what you're looking for. If not, can you add one?

If the Flask class doesn't give you a way to change the exhibited field, and you can't change the class, THEN this becomes a question about ArrayLists, maybe. At that point, you'd want to create a new Flask with all the same properties as this one, except exhibited set to true, and then use that one to replace the current one in the list.

Two unrelated notes:

1) Don't iterate lists using get(i). Instead use a foreach loop or an explicit Iterator.

2) Never use == or != with true or false. That's reduntant.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Fareedh wrote:Well, I came up with something. But I don't think its right.


Then you should show us what you came up with, and tell us why you think it's wrong.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First things first.

Your array list is empty. So the for loop will never execute.

You should be delcaring the array list with a type. If it contains Flask instances, then it should be ArrayList<Flask>.

And, you're are using the old fashioned version of the for loop. If you have the array list properly declared as shown above, you can just use:

(And, what Jeff said.)
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already posted the code above. In my second reply. I am sorry I used "u" instead of "you"

I have tried setExhibition it says cannot convert from void to Boolean. I am actually a beginner, so I am kind of struggling with the code for this.
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:First things first.

Your array list is empty. So the for loop will never execute.

You should be delcaring the array list with a type. If it contains Flask instances, then it should be ArrayList<Flask>.

And, you're are using the old fashioned version of the for loop. If you have the array list properly declared as shown above, you can just use:

(And, what Jeff said.)


ArrayList booleans = new ArrayList(); .

This is just a temporary ArrayList. My original ArrayList is the "flasks".

Thank you to all of you trying to help me.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the temporary list? What are you really trying to accomplish?

Stop right now and put the keyboard away. Write out (yes, write it out) what it is you are trying to actually do. No code. Then post it here.

If you cannot do that, you're not ready to code it.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Fareedh wrote:I have tried setExhibition it says cannot convert from void to Boolean


You'll get more help if you TellTheDetails(⇐click). Show the actual code, and copy/paste the exact, complete error message.

From what you say, it sounds like you tried to do something like boolean x = setExhibited(true) or something. That error message is telling you that the set method doesn't return anything, so you can't do something = setXxx().

Going along with what Bear suggested, after you're writing out your steps, when you go to convert them to Java, think about exactly what each and every piece of Java code means and how it corresponds to your steps. If you do that, you'll see that trying to get a result from a setter method and use it as a value doesn't make any sense and doesn't correspond to any part of any of your steps.
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, to be honest. I am just not just sure what to do. I know it would involve an 'If satement'.

It would go like this I think :

if(is Exhibited==false)

then change it to true.


Obviously, If I don't now what I am doing. I wont be able to write the code.

Thanks
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said: no code.

Tell us what you are trying to do in a paragraph of text. Until you can do that, yeah, you're not going to know what to do in the code.

This is an essential part of learning how to code: knowing when not to.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Fareedh wrote:Well, to be honest. I am just not just sure what to do. I know it would involve an 'If satement'.

It would go like this I think :

if(is Exhibited==false)


You already have that part, and I already told you not to use == false

then change it to true.


Okay, and how do you think you'd do that? You said you tried something and you got an error.

Did you read and think about and try to understand what I wrote? If you can't understand it, then, again, show us what you tried and provide the exact, complete error message.

 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Mohamed Fareedh wrote:Well, to be honest. I am just not just sure what to do. I know it would involve an 'If satement'.

It would go like this I think :

if(is Exhibited==false)


You already have that part, and I already told you not to use == false

then change it to true.


Okay, and how do you think you'd do that? You said you tried something and you got an error.

Did you read and think about and try to understand what I wrote? If you can't understand it, then, again, show us what you tried and provide the exact, complete error message.



The only think I tried was Boolean c = setExhibited(); and obviously I got that error saying it cannot convert from "void to Boolean" . That was the whole error message.
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:As I said: no code.

Tell us what you are trying to do in a paragraph of text. Until you can do that, yeah, you're not going to know what to do in the code.

This is an essential part of learning how to code: knowing when not to.


I want to be able to change the isExhibited value to true. For that, I would need if statements. It would say if isExhibited is not true then change it to true. Is that what I am meant to be doing?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. First of all, forget about if statements and code. No code.

Back up and explain from a higher level what you need to do. Pretend you're explaining it to your Aunt Mathilda who never even heard of Java.
 
Mohamed Fareedh
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a programme language called java I want to be able to exhibit a flask. if its not exhibited, I want to change it to true so it will be exhibited. In other words, I want to be able to change it from true to false and vice versa.

Is that ok?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I can see you're not going to approach this with an open mind.

You need to answer Jeff's questions about the nature of the Flask class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!