GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes beginning code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "beginning code" Watch "beginning code" New topic
Author

beginning code

Ryan Bell
Greenhorn

Joined: Oct 30, 2012
Posts: 2
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


Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 30, 2012
Posts: 2
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
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
Alex Petsche
Ranch Hand

Joined: Oct 29, 2012
Posts: 42

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
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11357
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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 !=.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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

Joined: Oct 29, 2012
Posts: 42

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
Bartender

Joined: Oct 02, 2003
Posts: 11357
    
  16

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 29, 2012
Posts: 42

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
GeeCON Prague 2014
 
subject: beginning code