• Post Reply Bookmark Topic Watch Topic
  • New Topic

why is there an error in the below code?  RSS feed

 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


here i am tryin to actually store the last bit of a number in a character array and then compare it with a character variable ,so actually I just want to compare the LSB of the binary numbwr which is stored as a string with the string "0",so how to proceed?
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is where your problem is. Should you be able to call equals on this array element? Why or why not? If not, what operator should you use?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are lots of errors. I am surprised you get it to compile.

If you want us to help, things like presenting unindented code with incorrect formats for class names simply make things difficult and will discourage anybody from helping. Start by telling us what error you are experiencing. Look in that link for the how to ask questions page, too.
 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sir,please help me out I think it is used only with objects ,so then how to do the comparsion because I am restricted to use any comparison operator except for that I can use a function only ,so what to do now?
 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting these 2 errors
rd5.java:12: incompatible types
found : char
required: java.lang.String
arr[0]=s.charAt(s.length()-1);
^
1 error

C:\java>javac rd5.java
rd5.java:13: char cannot be dereferenced
if(arr[0].equals('0'))
^
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
radha gogia wrote:sir,please help me out I think it is used only with objects ,so then how to do the comparsion because I am restricted to use any comparison operator except for that I can use a function only ,so what to do now?


The array is an object but the elements are not.



What is the type of the array element? What operator do you use for equal in this case?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first of those two errors does not happen with the code you posted above. You must have changed the code.

Knute is already giving you hints about the second error.
 
radha gogia
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think compareTo can be used but then again I will have to use comparsion operator for checking its value ,which I don't want to do ,so basically i want to check the LSB of the String value with the 0 character ,so what to do now and no operator has to be used.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How on earth do you get a String in line 12? I tried your code and did not get that error.
I did get the error in line 13 however.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another hint: You cannot call methods such as equals() on primitive types.

What is the type of the elements of the array?
Is it a primitive type or not?
What should you use to check if two primitive type values are equal?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!