• Post Reply Bookmark Topic Watch Topic
  • New Topic

"Identifier expected" error  RSS feed

 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, I used to write java pretty well then I stopped and I'm starting again so I can get better than I used to be. I am currently working on a VERY basic calculator that should have realistically taken 10 minutes at the most to write, but I have been stuck on one error for the past 2 days: "identifier expected". Here is the code and I labeled the line where it is expected. Any help would be greatly appreciated.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why is there a dot after new?
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, strings are compared using the equals method, not using the "==" operator.

And this kind of shortcut does not work in Java: (op == "x" || "*"). You'll have to write both checks in full.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, strings are compared using the equals method, not using the "==" operator.

Thats what I thought too but when I wrote it out in NetBeans it gave me an error. I'll go back and double check it though
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for helping guys, I copied the code over to NetBeans and fixed all the incorrect syntax. It works now but I thought you might want to see it to know what I did so here:
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 20 and 26 could be combined like this:

if (op.equals("x") || op.equals("*")) {
 
Paweł Baczyński
Bartender
Posts: 2085
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also write else if instead of if on every check except first.
Or you could use switch statement do do the job (if you are using Java 7).
You can move System.out.println(ans) outside of if statements because they are repeated code and that line will be executed in every case.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use a switch before Java7 by using '+' (a char) instead of "+" (a String).
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:You could also write else if instead of if on every check except first.
Or you could use switch statement do do the job (if you are using Java 7).
You can move System.out.println(ans) outside of if statements because they are repeated code and that line will be executed in every case.

I just tried moving System.out.println(ans); outside the if statements and its throwing me an error, "Variable might not have been initialized"
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't declare three variables in the same line. Give them each their own declaration. Initialise them if you can think of a sensible initial value: double ans = 0.0; will probably work!
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, thanks for all the help guys, I appreciate it. I finished that calculator and I will post the code below. I am also starting a new slightly more advanced calculator that I am going to make another topic on, just to see how else some of you might approach the app.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume the fall‑through in line 23 is intended? If not, you need a few more break;s.
 
Jorgen Bech
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I presume the fall‑through in line 23 is intended? If not, you need a few more break;s.

I left it like that because I saw no reason to add breaks when the program accomplishes the break without putting the keyword in
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jorgen Bech wrote: . . . the program accomplishes the break without putting the keyword in
Really? It didn't when I tried it.
java Test
Enter the first number:
123.45
Enter the operation:
+
Enter the second number:
654.32
777.7700000000001
-530.87
80775.804
80775.804
0.188669152708155
You can read about the 000001 here (No 20).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!