• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

right syntax with !equals in Java

 
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's kind of a homework and that's forbidden unfortunately
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 !


 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Just the other day, I was thinking ... about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic