Sponsors: Michael Fertik - Best JAVA Web hosting Company & 30% off


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-20-2010, 10:44 AM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
cysquatch is on a distinguished road
Default Reverse a string?
How can you reverse a string/number without using any loops or existing methods?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 03-20-2010, 10:49 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by cysquatch View Post
How can you reverse a string/number without using any loops or existing methods?
For Strings have a look at the StringBuilder or StringBuffer classes. Numbers are quite a different cup of tea and a simple recursive method can do the job if the number is an integral number.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 03-20-2010, 11:16 AM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
cysquatch is on a distinguished road
Default
No sweat, thanks for that.

Also another required tip...can you explain how to read/put the methods and libraries into usable code at all? I know i'm a noob but seeing something such as this: BufferedReader (Java 2 Platform SE v1.4.2)
I'm a bit unsure of how to put it all into practice.

Thanks & Sorry
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 03-20-2010, 11:20 AM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
cysquatch is on a distinguished road
Default
I have an idea for how I want to solve my problem:

Say for example someone enters a 3 digit number, 123
using the charAt(); function, i'd want to read the above 123 string into a new string in reverse. Is it possible to just say something like:

charAt(2);
append to result;
charAt(1);
append to result;
charAt(0);
append to result;

I'm just a bit new to this coding area.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 03-20-2010, 11:27 AM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by cysquatch View Post
I have an idea for how I want to solve my problem
Why don't you give it a try:

Code:
public class MyClass {
   public static void main(String[] args) {
      StringBuilder sb= new StringBuilder();
      // your code goes here
   }
}
compile and run it:

Code:
javac MyClass.java
java -cp . MyClass
... and see if your idea was correct.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 03-20-2010, 11:40 AM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
cysquatch is on a distinguished road
Default
Not sure if this is right but i think i went a different way.
I assigned each letter in the string to a char using charAt(x); but am wanting to now assign the characters in each of those, append them to a string.
The values have been read into the char's correctly, guess I just need this last part to append all the letters to one string, is that a hard thing to do?
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 03-20-2010, 11:45 AM
Senior Member
 
Join Date: Mar 2010
Posts: 605
Rep Power: 1
gcalvin is on a distinguished road
Default
StringBuilder (Java Platform SE 6)

No, it's not a hard thing to do.

-Gary-
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 03-20-2010, 11:58 AM
Member
 
Join Date: Mar 2010
Posts: 5
Rep Power: 0
cysquatch is on a distinguished road
Default
StringBuilder (Java Platform SE 6)

This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.

What I thought i could do is something like this:

if the numbers/letters are assigned from their character positions, example

a = number.charAt(5);
b = number.charAt(4);
c = number.charAt(3); ...and so on,

I can use printf and say the string backwards is %c%c%c then insert the arguments, a,b,c but i just want to append the above abc to a string like 'result'.

Could you explain how this can be done by using some kind of a concat method or something?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 03-20-2010, 12:18 PM
Senior Member
 
Join Date: Mar 2010
Posts: 605
Rep Power: 1
gcalvin is on a distinguished road
Default
JosAH has given you a skeleton method above. What I sent should have been a link to the append() method of the StringBuilder class (but I see the link doesn't work well in my browser). You invoke it like this:

Code:
    sb.append(c);
-Gary-
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 03-20-2010, 12:35 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by cysquatch View Post
StringBuilder (Java Platform SE 6)

This isn't really helpful to be as since i'm new i don't understand how to put these instructions into a working example, sorry.
Being new to programming doesn't imply you're illiterate does it? Why don't you read what such a StringBuilder can do for you? Spoiler: it can reverse its content (a representation of a String) for you.

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 03-20-2010, 09:01 PM
Senior Member
 
Join Date: Feb 2009
Posts: 661
Rep Power: 2
pbrockway2 is on a distinguished road
Default
If you find it hard to connect the dots from the API documentation to a working example (eg because the syntax of Java is new or whatever) then Google is your friend. The first hit for "StringBuilder reverse" looks pretty good when I tried it.

Of course you have to exercise a certain amount of sense in choosing whether to accept what you read. I was going to say common sense but, to be fair, it isn't common when you're starting. So look for usage examples that are simple, easy to follow and presented on pages with professional layout and language use.

Understand any code ideas you pick up. That will involve your textbook/teacher/whatever. And, having done your own work, ask here (with code attempt) if things aren't clear or don't give the intended results.
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 03-21-2010, 07:39 AM
Senior Member
 
Join Date: Feb 2010
Location: Ljubljana, Slovenia
Posts: 302
Rep Power: 1
m00nchile is on a distinguished road
Default
Now now, let's all take a breather first
Ok, instead of jumping the gun and stare at complicated classes, why don't we try and figure out a simple solution, for such a simple problem? A few lines of recursive code solve this quite nicely, I'll just describe the solution, so the OP can figure out for himself how to put it into code.
So, to reverse a string, you need to take the first letter, and make it last, and so on and so on. One letter doesn't need to be reversed, so that can be our base case. In short, get the reversed substring without the first character, and append the first character to the back of it. Only substring() and charAt() methods are used and the whole program consists of 3 lines (if you're not counting method headers and main ).
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 03-22-2010, 10:48 AM
programmer_007's Avatar
Senior Member
 
Join Date: Aug 2009
Posts: 125
Rep Power: 0
programmer_007 is on a distinguished road
Default
Quote:
without using any loops or existing methods?
y r we forgetting this.. rite in the first post.
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 03-22-2010, 01:07 PM
Senior Member
 
Join Date: Feb 2010
Location: Ljubljana, Slovenia
Posts: 302
Rep Power: 1
m00nchile is on a distinguished road
Default
I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 03-22-2010, 01:30 PM
Senior Member
 
Join Date: Sep 2008
Location: Voorschoten, the Netherlands
Posts: 3,322
Rep Power: 5
JosAH is on a distinguished road
Default
Originally Posted by m00nchile View Post
I was proposing a recursive method, so no loops, and no existing methods probably means the reverse() method from the string class.
The String class doesn't have a reverse() method and you'd have to at least use one other method in your recursive solution so it is not allowed ;-)

kind regards,

Jos
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 03-23-2010, 02:31 AM
Member
 
Join Date: Mar 2010
Posts: 21
Rep Power: 0
HackerOfDoom is on a distinguished road
Default
May I ask why you need not to use loops and such? That would be by far the easiest way to reverse a string.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
reverse string split Fittersman Advanced Java 4 03-09-2010 12:29 AM
reverse a string with a while loop... OptimusPrime New To Java 9 12-28-2009 11:06 PM
Reverse a string not using the substring method kathyla18 New To Java 17 04-08-2009 04:08 AM
Reverse and Replace a String in Linear Time colin.cruise New To Java 5 07-01-2008 09:02 PM
How to reverse two dimensional masaka New To Java 4 05-19-2008 10:02 AM


Java Forums is supported by the best jsp hosting.

All times are GMT +2. The time now is 09:17 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org