• Post Reply Bookmark Topic Watch Topic
  • New Topic

getting cannot find symbol when compiling  RSS feed

 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

Please see my code.



When I compile getting

CQ1v1.java:12: error: cannot find symbol
x = in.nextLine ();
^
symbol: variable x
location: class CQ1v1
CQ1v1.java:21: error: cannot find symbol
System.out.println("You have made choice "+x);
^
symbol: variable x
location: class CQ1v1
2 error

any help appreciated.
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yepp - you never declared / defined x (i.e.: the compiler does not know what type x is).
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks i got it now.

I placed this in the code

String x ="";
 
Steve Fahlbusch
Bartender
Posts: 612
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good work -- glad you got it.

-steve
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I cannot figure out why it would not compile.



error message :

CQ2.java:25: error: cannot find symbol
if (x=A) {
^
symbol: variable A
location: class CQ2
CQ2.java:25: error: incompatible types
if (x=A) {
^
required: boolean
found: String
CQ2.java:28: error: cannot find symbol
else if(x=B){
^
symbol: variable B
location: class CQ2
CQ2.java:28: error: incompatible types
else if(x=B){
^
required: boolean
found: String
CQ2.java:31: error: cannot find symbol
else if (x=C){
^
symbol: variable C
location: class CQ2
CQ2.java:31: error: incompatible types
else if (x=C){
^
required: boolean
found: String
6 errors
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to check if the variable x has a value of the String "B" the you need to use



if(x=B) is wrong because

1.) It's using = which is an assignment operator (tries to assign B to x) not a comparison operator for equality which would have been ==
2.) It's using the symbol B which the compile will assume is variable. If you want the String value "B" then you must wrap in in quotes like I did above.
3.) It's trying to use an operator to compare Strings. you should always use the equals method when comparing Strings (or any reference types).

You should also consider changing x from String type to char type because its possible values are only one character long. Generally you should pick a type that best matches the range of values you are trying to represent.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it to work.
thanks
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

E Armitage always provides helpful answers, and you have got a helpful answer. You should avoid the == operator, and that link will tell you a lot more. It might have compiled had you declared B and written if (x == B)... but that would probably have given wrong results at runtime. Wrong results is a much more serious error than failing to compile.
You will doubtless succumb to the temptation to write == true and == false, but never write that sort of thing. If b is a boolean, never write
if (b == true)... nor if (b == false)... nor anything similar.
It is:-
if (b)... or if (!b)... or similar.

I suggest you change the names of your variables; people don't usually call Strings x. Look at these suggestions. They might be old but the haven't changed.
Also, your main method is much too long. You should move 95% of that code into different methods.
Did you know you can use switch‑case statements with Strings? It only works in Java7.
 
Carolin Sha
Ranch Hand
Posts: 68
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying the switch and i changed the x, see my code, got some compiler errors.



errors

CQ3v1.java:32: error: incompatible types
case 1: letterString = "Your prize is a pet aardvark!";
^
required: String
found: int
CQ3v1.java:34: error: incompatible types
case 2: letterString = "Your prize is a pet bear!";
^
required: String
found: int
CQ3v1.java:36: error: incompatible types
case 3: letterString = "Your prize is a pet cat!";
^
required: String
found: int
CQ3v1.java:38: error: incompatible types
case 4: letterString = "Your prize is a pet dog!";
^
required: String
found: int
CQ3v1.java:40: error: incompatible types
case 5: letterString = "Your prize is a pet eel!";
^
required: String
found: int
CQ3v1.java:42: error: incompatible types
case 6: letterString = "Your prize is a pet fish!";
^
required: String
found: int
6 errors
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As far as I know java does not support String in switch case.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin, your letter variable is String.
In your switch you compare it to integers.
You should really read compiler error messages.
Try etc...


This line (and similiar) will cause you real problems if user typed enything that is not integer.

Swastik Dey wrote:As far as I know java does not support String in switch case.

It does beginning with version 1.7 (read about it here).
 
Swastik Dey
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Pawel.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you prompt an user for a letter A to F, and then you compare it against numbers 1-6.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carolin Sha wrote:I am trying the switch...

I think Pawel has covered the errors, but I have a small additional point for you:

Try to avoid repeating code when you write programs.

In your switch statement, you have a bunch of Strings, all of which start with "Your prize is a pet ", so you could have saved yourself quite a bit of typing with something like:(and there are many other alternatives)

In this case, it's a very trivial thing (and probably not worth worrying about); but when it comes to logic, repetition - ie, copied code - can be disastrous. So I urge you to always be on the lookout for places where you have written the same code twice.

HIH

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!