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

Reverse the pattern in optimize way in java  RSS feed

 
Mahesh Suryawanshi
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Folks,

I have a string with a pattern for eg 10 23 40 00, so i want to reverse this pattern like 00 40 23 10 in a optimize way.

the pattern will consist of space between 2 numbers & all numbers will have 2 digit.

Thank you
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
their are many methods to do this you should see this
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Suryawanshi wrote:Dear Folks,

I have a string with a pattern for eg 10 23 40 00, so i want to reverse this pattern like 00 40 23 10 in a optimize way.

the pattern will consist of space between 2 numbers & all numbers will have 2 digit.

Thank you


So what have you tried so far and where are you stuck?

1st step to solve a problem is to get a piece of paper and write down how you as a human would go about reversing it.
Once you have this logic in place, it is easy to convert it into code
 
Mahesh Suryawanshi
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen kumaar wrote:their are many methods to do this you should see this


Thanks for reply

if the input is --> Be in present then output be like --->present in Be

How can achieve this, below code reverse everything what changes i do to achieve the above output

 
Mahesh Suryawanshi
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . 1st step to solve a problem is . . .




Above Program Output
input -> 10 23 40 00
output -> 00 04 32 01

What i expected is 00 40 23 10

Can you help me to solve this ?

 
Liutauras Vilda
Marshal
Posts: 4642
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

1. First important thing - always use code tags when you post your code. Tags are in the editing panel where you write your text, right above the text you write basically.
2. After you add code tags, make sure you code is well indented, otherwise code gets very difficult to read.
3. Class names suppose to start with an upper case.
4. There should be a space before you add '{'. It is missing right after the 'for' loop.

Please accomplish those steps and re-post your code. Thank you.
 
Dave Tolls
Rancher
Posts: 2914
36
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Suryawanshi wrote:
Can you help me to solve this ?



First step then is to turn your String into an array of the words in the String.
String has a method that will let you do that.

Then you can use that same algorithm for reversing the array of characters and instead reverse the array of Strings.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:. . . 1st step to solve a problem is . . .
And what Maneesh recommended is exactly what you didn't do. That is why your code didn't work, because you didn't plan it.
When you do get round to planning, don't use byte[]s, which are intended for sending across a network. You need something different.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mahesh Suryawanshi wrote:I have a string with a pattern for eg 10 23 40 00, so i want to reverse this pattern like 00 40 23 10 in a optimize way.
Welcome to JavaRanch, Mahesh.

That question is actually TWO questions:
1. "[How] to reverse this pattern..."
2. "...in a optimize way."

and of the two, #1 is much more important than #2; because if you don't get #1 right, then it really doesn't matter how "optimized" your code is, does it?

So, my advice:
Whenever you get tasks like this, forget about efficiency until you've solved the problem.   And even then, it's probably not worth bothering about unless you can prove that it's worth the time and effort.

As a very famous computer scientist
Donald Knuth wrote:Premature optimization is the root of all evil.

Winston
 
Mahesh Suryawanshi
Greenhorn
Posts: 22
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys for your comments

i found the solutions below is the code for same



 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not bad (), but it could be a lot better.
Why are you using equals("")? there is a simpler method in the String documentation.
Why are you looking for empty Strings in the first place? If you search for regular expression for any number of whitespace characters, you shou‍ld find something useful. In fact the most useful thing I noticed was on a website not a million miles from here. If you use a regex which matches multiple whiespace, you will not find any empty Strings in the middle of your text. You can also use a String method to remove leading and trailing spaces, which shou‍ld obviate all empty Strings. The search I showed you will find some promising tutorials about regular expressions.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using the substring method?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!