Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String delimitation program in Java -- off-by-one error  RSS feed

 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
The goal of the following program is to delimitate the source string thanks to the separators "/" " " and ":". The expected output is but it yields omitting the last element and displaying delimitors. Can you spot what is wrong ? Thanks in advance.

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 36 is always executed.
You are missing an if or else statement.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 30 you are trying to handle the end-of-source case. This would be better handled when you drop out of the for(;;) loop. This also does not print the string.
On line 35 you are missing an 'else'.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed it missed one "else"...I have made another correction according to your remarks, but still it yields (with a space at the very beginning) which is not the desired ouput. Any idea ? Thanks.

 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:it yields (...) (with a space at the very beginning) which is not the desired ouput. Any idea ?
You print this space on line 26.

By the way, read this: MainIsAPain.
You should have a method that does the splitting. Do not cram the logic in main

Also, the splitting method should not be concerned with printing anything.
Have another method to print the result.
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, but if I delete the space, then the ouput is , still the last element i.e "34" is missing...This drives me crazy !
Sure I'll put all that stuff in a method.
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Replace withThat way you will have the space at the end which might be more acceptable.

Currently you have splitting and printing mixed together which is not a good idea.

You should have a method like:You can then test whether this method works correctly by:Only when you have your splitting method working correctly, try to write your own printing method.

Also, do you know that there is a split method already defined in the String class?
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Replace withThat way you will have the space at the end which might be more acceptable.

This is exactly what I was thinking about, but the still the last element is missing...Really don't know why

Yes, I know there is a split method, which I would like to use, but it's forbidden for the exercise ;)
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:This is exactly what I was thinking about, but the still the last element is missing...Really don't know why

You only print a part of the string when you encounter a separator. There is no separator at the end of the string which means you do nothing with the last part.

A quick-fix would be to put this just after a for-loop:But remember, this does not solve the main problem which is mixing splitting and printing.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frank Poupard wrote:The goal of the following program is to delimitate the source string thanks to the separators "/" " " and ":".

I presume that, since you're trying to build this from scratch, you're not supposed to use regular expressions or any existing String methods except charAt().

So here's my suggestion:

1. Read Pawel's post about breaking things up into methods again; but first: write down what you need to do in logical terms - and preferably in French, not Java.

2. String has a method called indexOf() that returns the index of the first matching character or string in it.
Why don't you write something similar, eg:
  public static final int nextMatching(String s, int startPosition, String charsToMatch) { ...
that returns the index of the next character in s, starting from startPosition, that matches ANY of the characters in charsToMatch.

I think you'll find it a huge help in solving your problem.

HIH

Winston
 
Frank Poupard
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston, I went through the logic already with pencil and paper, or at least I tried ! Anyway, I've made a logical error since my program could not deal with the last string.
Actually, I'm assigned a method that I have to stick to, which is
So, I have to deal with the last string now
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having this method signature enforced by the assignment doesn't mean you can't create more methods.
 
Carey Brown
Bartender
Posts: 2980
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:On line 30 you are trying to handle the end-of-source case. This would be better handled when you drop out of the for(;;) loop. This also does not print the string.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!