• 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

Removing the middle 2 characters when string length is even

 
Greenhorn
Posts: 24
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Given a string, return a a string with either the middle character removed, when the original string contains an odd number of characters, or the middle two characters removed when the original string contains an even number of characters. Assume there will be at least one character in the original string. Note that you can do this without an if statement.






this works for a word that has an odd length but how can i make it so it can also remove the 2 middle characters if the word length is even??
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

how can i make it so it can also remove the 2 middle characters if the word length is even??


First detect an even length.
Have you tried with paper and pencil to get the indexes and lengths for the parts of the String that need to be extracted and put together again to build a new String with the middle 2 characters removed?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hint is important: you can do this without using an if-statement.

This means that the formula for determining whether the length is odd or even will be part of the calculation of how many characters you need to remove. It's actually a pretty clever twist to the solution.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and Welcome to the Ranch!
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, and yes that's the part i'm stuck because with the if i might be able to do it but i'm trying to do it without the if statement.
 
Norm Radder
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

i'm trying to do it without the if statement.


How do you plan to make the decisions regarding even or odd length?
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i have no idea where to begin
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
length of stringstart of leftend of leftstart of rightend of right
1
2
3
4
5
6
70347
8

Fill in the rest of the table and see if you recognize a pattern.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Google is your friend. Look up checking for odd or even with Java remainder operator or check out this article: http://www.javaranch.com/drive/modulo.html
 
Stephan van Hulst
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't need a modulo operator. In fact, you don't need to determine at all whether the length is even or odd.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need to determine the length. The left and right parts-to-be-kept are equal in length. Now, suppose the original string has length 6, then the left part will be 2 characters long. If the string is 7 chars, we need to keep the first 3 chars. So, we need a function f with f(6) = 2, f(7) = f(8) = 3, et  cetera. Can you think of such a function?
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome again

If I were doing this, I would use a StringBuilder object.
 
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you think in this instance, the ternary operator counts as an 'if'? ;)

L.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You don't need a modulo operator. In fact, you don't need to determine at all whether the length is even or odd.


Ok, I see how you can solve this without using the remainder operator.

Piet Souris wrote:You do not need to determine the length.


Not sure what you mean by that because length() is central to the solution, at least it is with the one I got. Are you saying you don't need length() at all or did you mean to say that you don't need to determine whether the length is odd or even?
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

L Hasan wrote:Do you think in this instance, the ternary operator counts as an 'if'? ;)

L.


Yes, it counts as an if

It's a bit of a brain-teaser, I must admit.

Hint: First cut off the right side to the correct length, then you can use that piece to measure how much of the left side you need to take.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming the method you write is named noMid(), here's some code you can use to test your implementation:
 
Alvaroo Hernandez
Greenhorn
Posts: 24
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



All i have is this and this only works for any word that has an even amount of characters. i do apologize i am new to this site and barely learning java. i appreciate all the tips and help.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you insert some System.out.println() statements so that you can see what your start and end parts look like, you'll see that your end part is always correct but your start part will only be correct when string.length() is even.  So, if your end part is always correct, then you can use its length to measure out how much of the string you need for start.
 
L Hasan
Greenhorn
Posts: 24
4
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Assuming the method you write is named noMid(), here's some code you can use to test your implementation:



This might be a good, albeit in-depth, opportunity to read up on Test-Driven Development (TDD). This way of testing the implementation fits perfectly into that.

L.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In such exercise pencil and paper are the best tools.

Alvaroo, write down on a piece of paper:

Write indices above each character. Write strings lengths on the right of each string. Once you workout right hand side string start and end indices, think how you can calculate left hand side string start and end.

All is simpler thank you might think. Just visualise all exercise on a paper.

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic