• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Setting a Boolean flag value

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to set a boolean flag to check if 17 appears in a random array of 1000000.
Can someone tell me how close or far off I am, please.
 
Saloon Keeper
Posts: 21312
140
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

The line

is both syntactically and semantically invalid and will not compile. You are assigning an array to a scalar.
[code]

Also, "if (x == true)" and "if (x == false)" are bad practice. First, because they are redundant code (simply say "if  (x)" and "if (!x)". Secondly because if you mis-type an expression like this: "if (x=true)" then you introduce a very subtle bug into your program.
 
Saloon Keeper
Posts: 2806
368
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, typos/errors in these lines:
 
Joshua Rowland
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you both. I am trying to learn and prep for a test. We're kind of left to our own devices to "figure it out."
 
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use better names that make the code read like it's telling a story.

Take this, for example:

versus

Then this:

becomes this instead

Which version makes more sense? I hope you agree that it's the version that uses found instead of check17.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you learned about the enhanced for-loop yet? If so, then that's a more appropriate kind of loop to use for this kind of task.

The pseudo-code for finding 17 in an array of ints would be:

found = false
for each element n in the array:
   if (n equals 17):
       found = true
       terminate the loop

if found:
   display found message
else:
   display not found message

I already showed how the "if found" part would be implemented in Java. You just need to figure out how to write the for-loop part.

See https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
 
Joshua Rowland
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,

It does. I've not worked much with setting boolean values. I am studying old exams for mine tomorrow, and the instructor told me that I needed to set a boolean flag, test within my FOR loop, and then create another IF statement to print what was asked. Thank you for taking the time to look and give good information.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joshua Rowland wrote:I've not worked much with setting boolean values.



It's not difficult. It's basically this:


In this snippet, line 1 shows how you'd set the boolean variable isPositive to either false or true. These are the only two boolean literal values.

Line 4 shows that you can set a boolean variable to any boolean expression. A boolean expression is an expression that will evaluate to either true or false.

There are a number of ways you can write a boolean expression. See this tutorial page: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/op2.html
 
Joshua Rowland
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu,

We've started talking about them, but I've yet to use a for-each loop. For the FOR loop, it would be FOR(int i = 0; i <array.length; i++)

What I currently have for my pesudocode is

Boolean found ← false
FOR(int i = 0; i <array.length; i++)
IF (array[i] == 17)
found ← true
END FOR
END IF

IF (found == true) THEN
PRINTLINE ("Che burtto!")
ELSE
PRINTLINE ("Buona fortuna!")
END IF
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, that looks about right. But again, don't do this:


Do this instead:

Writing "if (found == true)" is basically saying "if (true == true)" or "if (false == true)" depending on what the value of found is. Either way, it's redundant.
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joshua Rowland wrote:
Boolean found ← false
FOR(int i = 0; i <array.length; i++)
IF (array[i] == 17)
found ← true
END FOR
END IF


Important note: you have the order of END FOR and END IF switched here. If you indented this, you'd see the problem more clearly:

Boolean found ← false
FOR(int i = 0; i <array.length; i++)
  IF (array[i] == 17)
     found ← true
  END FOR
END IF

Notice how the indentation shows the mismatch between the start and end of each statement.
 
Marshal
Posts: 66637
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . .. . .

I would prefer to consider the ?: operator:-
 
Tim Holloway
Saloon Keeper
Posts: 21312
140
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I would prefer to consider the ?: operator:-



So would I, for production. We're talking beginner with a side order of potentially "the instructor wanted 2 if statements", so best to worry about other things.

Anyway, I'm a format freak:


When I'm in a really snarfy mood, I may even drop the ending parenthesis down on a new line and column-align it with the "System.out".
 
Junilu Lacar
Marshal
Posts: 14530
242
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Holloway wrote:When I'm in a really snarfy mood, I may even drop the ending parenthesis down on a new line and column-align it with the "System.out".


Well, color me snarfy with a permanent marker then.  

Edit: Ok, maybe just tattoo me snarfy. With some henna. On my shoulder.
 
Tim Holloway
Saloon Keeper
Posts: 21312
140
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry, we're flat out of henna, right now. Best we can offer is Peppa Pig washable tattoos.
 
Joshua Rowland
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for the help you've provided; I learned a lot with some other advanced techniques. I'll try those some additional time.
 
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!