Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Comparing Arrays

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I am trying to compare two arrays that I have passed through this method. However, I am getting an error that i in the if statement can't be resolved to a variable. I am lost on what I am doing wrong, because I have declared i in the for statement.
 
Rancher
Posts: 2021
15
Android Java ME Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Look at the semicolon, you for loop is getting terminated there.
 
Marshal
Posts: 7178
491
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of whether you need to verify that array1 and array2 are of the same length?
 
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using an IDE that has an autoformat feature can help in these situations, if you're a stickler for proper formatting and indentation.

This is how my IDE (IntelliJ IDEA) formats that code:

My first thought would be: "Why is the { on line 4 instead of at the end of line 3?" Then I'd scan up to the end of line three and see the semicolon. Then I'd be like "Doh! That's why!"
 
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . a stickler for proper formatting and indentation. . . .

Why should somebody who insists on proper indentation be called a stickler for it? That is like calling somebody a stickler for decent handwriting if I give them some paper which I have written on. If you can't read what I “wrote”, isn't that my fault?

[Edit}[Addition] Wouldn't the IDE also give a warning about empty statements?
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only meant that since I myself am a stickler for proper formatting and indentation, it would make me look twice if my IDE, which is configured to format my code in the 1TBS variant of the K&R style formatting, suddenly formatted an opening brace for a for-statement on a separate line, aligned with the for-statement.

Being a "stickler" in this context was not meant to be pejorative.

If these kind of things don't bother you that much because you're not a stickler for consistent formatting and indentation, then you could easily overlook the brace placement and think that the free-standing block you just created with the ; terminating the for-statement above it is still part of the for-statement, that for some reason or another, the IDE just wanted to switch to Allman style braces for this one for-statement.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:[Edit}[Addition] Wouldn't the IDE also give a warning about empty statements?


I suppose some might. I don't think mine did though.
 
Junilu Lacar
Marshal
Posts: 14039
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Why should somebody who insists on proper indentation be called a stickler for it?


Besides, "somebody who insists" is literally the definition of a stickler.  
 
Campbell Ritchie
Marshal
Posts: 65782
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . I suppose some might. . . .

I haven't seen it for some time but Eclipse gives warnings that an empty block should be commented. Maybe it doesn't notice a plain simple ;
 
today's feeble attempt to support the empire
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!