• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to split a string int two equal halves?  RSS feed

 
PrabhuRaam Yogaraj
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Example:
Java
After spliting:
Ja
va
please help with this problem
Thank you for your patience
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

We don't give out complete solutions, so please tell us what you think. Show us your reasoning, not your code.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How you split this string by hand in equal halves: Ranch ?
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hetram Lakhera please look what it says on this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
Please don't be annoyed with me, but I have pulled rank and removed the complete solution. The OP will learn much better by working out the solution for themselves.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A place to start would be to read the API doc for the String class to see what methods it has that might be useful.
The API doc is at:  http://docs.oracle.com/javase/8/docs/api/index.html
 
fred rosenberger
lowercase baba
Bartender
Posts: 12564
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, an example is NOT a specification.  What exactly is your criteria for deciding what "half" a string is?  If you are a typesetter, you may care more about how wide each character is...so a string like:

iiiiiiiiw

is going to be broken something like

iiiiii
iiiw

(or something like that).  If you are into numerology, then perhaps you assign numeric values to each character, and you want to divide the string where the sum of the values of each digit in the halves are closest to being equal.

Yes, these are rather contrived examples, but the point is that if you are going to work with software developers, it's critical you start thinking about how you communicate, and what you are asking.  Nothing drives me more insane than getting some vague request that can be interpreted about 20 different ways.
 
Pete Letkeman
Ranch Foreman
Posts: 911
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:What exactly is your criteria for deciding what "half" a string is?

This is a big hurdle to answering the initial post.

Not only that, but there are different ways to do something like this, depending on what exactly are you comfortable doing and what you experience level is.
You could make this an exercise in Lambda's, considered advanced, or using many of the built in String methods and loops.

Aside from that, is this a problem that you have thought of or something that you got from a book or a web site?
If so, which web site or book?
If we can see the question in context on a different web site then maybe we can put the solution into the correct context.
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pete Letkeman wrote:. . . Lambda's, considered advanced . . .
Are λs really an advanced topic? Or are people simply scared of them?
 
Pete Letkeman
Ranch Foreman
Posts: 911
26
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Are λs really an advanced topic? Or are people simply scared of them?

I'm not too sure, but one thing that I think most people can agree upon is that the syntax for lambdas does take some getting used to.

I do believe that many people struggle with lambdas in how they could be presented on the Java OCA 1Z0-808 exam when preparing for said exam.

And from what I've seen thus far (I'm somewhat of a beginner with regards to Java), I think that, many times when you get into lambdas you get into collections. However that is my experience only.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!