• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

beginning code

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi I have been coding for about a week and i started to make a more complicated calculator i dont know if this is the best way to do it but my error is on line 2 the error list is.Multiple markers at this line
- Syntax error, insert "EnumBody" to complete
EnumDeclaration
- Syntax error, insert ")" to complete SingleMemberAnnotation
- Syntax error, insert "]" to complete ArrayAccess
- Syntax error on token "void", @ expected
- Syntax error on token "]", invalid (
- Syntax error, insert "enum Identifier" to complete
EnumHeaderName
- Syntax error on token "void", @ expected



and here is my code


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than trying to debug a huge wad of code at once, you should strip out pretty much everything, and compile after ever few lines (< 5) you add. Otherwise you're just begging for a helling time trying to sort out all the error messages.

Start with this:


Use a capital letter for the beginning of class names, and use a more meaningful name than H.

Compile. Don't add anything else until it compiles successfully.

Then add this:


Compile. Don't add anything else until it compiles successfully.

Make sure you add it in the right place.

And so on.

Keep in mind that all methods and constructors must be inside a class body, and all statements (anything other than a member variable declaration and optional initialization as a single step) must be inside a method body or constructor body (or initializer body, but you don't need to worry about that yet).
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the future, please UseCodeTags(←click) so that your code will be readable. I've added them to your original post for you.

Also, use a reasonable indentation. 2 to 4 spaces (and make sure you're using spaces, not tabs), and don't increase the indent level for consecutive lines in the same block.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, when you wrote this:


You really meant this:


1. Lowercase letter to begin variable names.
2. == for comparison, not =
3. No semicolon after the while condition, else while has an empty body, and will loop either forever or never, and if it does loop, it won't do anything but evaluate the loop condition over and over.

Except that also, you should never use == or != with true or false. It's just redundant and adds clutter.

 
Ryan Bell
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thank you i will do as you said and start building my code up from the bottom again with all of the changes that you put in.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good! I know it seems like a pain now, but believe me, it will be less painful than trying to manhandle that mess you've got now.

Good luck! And post again if you get stuck.
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote: . . . never use == or != with true or false. It's just redundant and adds clutter. . . .

And, as you saw, it is error‑prone because you might write = by mistake.

And welcome to the Ranch
 
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just a quick question to clear up a few things. Does default to true? If so the same is true that defaults to false? It appears so from your post, but I wasn't sure as no one's ever explained this to me.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Petsche wrote:Just a quick question to clear up a few things. Does default to true? If so the same is true that defaults to false? It appears so from your post, but I wasn't sure as no one's ever explained this to me.


not sure what you mean by "default".

x would have to be a boolean type, or the compiler will complain. Somewhere, you would have set it to either 'true' or 'false'. When you get to the line "if (x)", you can replace the 'x' with whatever value it holds. If it resolves to 'true', you enter the if block. if it resolved to false, you don't.

when you have "if (!x)", again, you replace the 'x' with its value. the '!' negates it, so a 'false' becomes 'true', and vice versa. so if you have


you will enter the 'if' block when x is false, and the 'else' block when x is true.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Alex Petsche wrote:Just a quick question to clear up a few things. Does default to true?



No. My "x" there is a placeholder for any boolean expression. What's inside the if's parens must be an expression of type boolean, such as a boolean variable, a literal true/false, or a call to a method that returns boolean.

There is no "default." If the value of x is true, the body of the if statement will be evaluated. If it's false, it won't be.

Given this:


all of the following are equivalent:



And they are all equivalent regardless of whether X's value is true or false.

That is, the boolean expression X is equivalent to the boolean expression X == true, X == true == true and so on up to infinitely many repetitions of "== true". This is always the case, regardless of whether X is true oor false. If X is true, then "X == true" is true, and if X is false, then "X == true" is false. And so on.

And if X is not a boolean expression, then X == true is not valid anyway.

Given that, there's no point in using "== true", and the same logic can be applied wither it's true or false and whether it's == or !=.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One other small comment on that: when you start using descriptive variable names, instead of calling your variables obscure and meaningless things like "x", things become a lot clearer:



versus


 
Alex Petsche
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok that makes sense I guess I was wondering if it worked for int, double, String, etc

Does this work or would I need to do this?



I also understand I can use an "else" statement if the first if doesn't work, but I wanted to show for either way if you decided to check.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you have this:


ask yourself if x resolves to a boolean type - i.e. "true" or "false".

It's pretty clear here the answer is "No". The compiler will complain. Note that I am only talking about Java here. C/C++ behaves differently.

Honestly, the easiest way to tell is to try it. I can't tell you the number of 10-line programs I've written to experiment and see what would happen. It's usually faster than posting to a forum and waiting for a reply.

also, this:

could be written as


You know that these two conditions are mutually exclusive, so you can wrap them into the same if/else block. I would also suggest you ALWAYS use curly braces, even if they are not needed. You will save yourself a world of hurt if you don't, and someday try to add a "System.out.println()" in there to see what's going on, and things REALLY start acting strange...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:One other small comment on that: when you start using descriptive variable names, instead of calling your variables obscure and meaningless things like "x", things become a lot clearer:



That was me, actually. I started using "x" as a placeholder for any boolean expression, and the ensuing discussion continued that.
 
Alex Petsche
Ranch Hand
Posts: 42
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the info Fred. That's the way I thought it would work, but it wasn't clicking in my head exactly. I did try it, but I wasn't sure if I was missing something or if, as I said, it wasn't clicking. Thanks for the help though!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

fred rosenberger wrote:
You know that these two conditions are mutually exclusive, so you can wrap them into the same if/else block.



And that goes beyond convenience and ease of reading. The two idioms have different semantics, and produce different behavior when the conditions are not mutually exclusive. And even if they are mutually exclusive, if they're complements are not mutually exclusive (that is, if it's possible for neither condition to be true) then you also get different behavior.

Separate if (X) / if (Y) blocks are only appropriate when X and Y are two independent conditions, and the actions you're taking in those respective cases are independent of each other. For example: "If the order is larger than 100 units, apply a 5% discount. If the customer lives in this state, add sales tax."


I would also suggest you ALWAYS use curly braces, even if they are not needed.



PlusPlus to that! It also makes it clear to anybody reading the code later (yourself included) exactly what was and was not intended to be part of the if block. There have been several times when I was debugging some code, came across a brace-less if, and had to waste time figuring out whether it was intentional, or if the coder forgot the braces and that might be part of the problem.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic