• Post Reply Bookmark Topic Watch Topic
  • New Topic

right syntax with !equals in Java  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have some syntax issues with "not equals" in line 8, in particular where should I put the "!" ? I've tried several combinations. Compiler says "char cannot be dereferenced".
Thanks in advance.

 
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
The method charAt() returns a char, which is a primitive type, not an object. You can't call the equals() method on a char because it's a primitive type.

Use != to check if two chars are not equal:

But even if you do this, it's not going to do what you expect. You are letting i go from 0 to the length of the source string, and then you take the i'th character of 'source' and compare it to the i'th character of 'separateurs'.

You'll get a StringIndexOutOfBoundsException because 'separateurs' has less characters than 'source'.

You probably wanted to do something else than comparing the i'th character of 'source' to the i'th character of 'separateurs'. Can you explain what you actually wanted to do?
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the answer, I had forgotten about the primitive type.
It was just a snippet here, but what I would do is to put each members of the String source (numbers here) separated by the delimitors ("/" or " " or ":") in an array. At the end, it should yield
I will try do it...
 
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
You can use the split() method of class String for that, no need to write a loop yourself.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's kind of a homework and that's forbidden unfortunately
 
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
Ok. To get an idea of how to do this, first you'll have to have it exactly clear for yourself what the program needs to do. This means, in your own native language (I guess that's French for you) write down exactly what steps the program should do. When the plan is clear, you can start thinking about how to put it in Java code.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came up with the following code, which does not give the desired output (it yields "444"). I had the feeling something was wrong but don't know exactly what...I tried my best first with pencil and paper !


 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, to start off the 'i' and 'j' loops should be swapped. But that won't make the program work. Renaming your 'j' variable to separatorPos and 'i' to sourcePos might make things clearer for you. You'll also need another variable to keep track of the position in the result array that you are working on; 'j' is your separatorPos, not your resultPos.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!