Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-10-2004, 05:22 PM   PM User | #1
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
reverse string in java

for my java class i need to write a method that would take a string the user inputs and output the characters in reverse order. Any ideas on how to do this cause i have no clue. Thanks
cwl157 is offline   Reply With Quote
Old 11-10-2004, 06:10 PM   PM User | #2
whackaxe
Senior Coder

 
Join Date: Jun 2002
Location: paris, france
Posts: 1,216
Thanks: 0
Thanked 0 Times in 0 Posts
whackaxe is an unknown quantity at this point
i don't know java, but the baisc process woulmd be this

-make an array the size of the length of your string
-make a for loop iterating up(++) to the strings length (lets call it i)
-take originalString[i] and place it in newString[originalString-i]

that should do it
__________________
photoshop too expensive? use the GIMP! www.gimp.org
whackaxe is offline   Reply With Quote
Old 11-10-2004, 06:40 PM   PM User | #3
Antoniohawk
Senior Coder

 
Join Date: Aug 2002
Location: Kansas City, Kansas
Posts: 1,518
Thanks: 0
Thanked 2 Times in 2 Posts
Antoniohawk will become famous soon enough
Yup, do what Whackaxe suggested, or you could always just use charAt() and make a String instead of an array. Haven't tried it, but it should work.
Antoniohawk is offline   Reply With Quote
Old 11-10-2004, 08:14 PM   PM User | #4
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
You could do it without the array, i'd write it like this in javascript, would be damn similar in java:

Code:
    var result;
    result = '';
    for (var position = 0; position < aString.length; position = position + 1)
    {
        result = aString.charAt(position) + result
    };
    return result
KeZZeR is offline   Reply With Quote
Old 11-10-2004, 11:45 PM   PM User | #5
Spookster
Supreme Overlord


 
Spookster's Avatar
 
Join Date: May 2002
Location: Marion, IA USA
Posts: 6,220
Thanks: 4
Thanked 80 Times in 79 Posts
Spookster will become famous soon enough
Quote:
Originally Posted by KeZZeR
You could do it without the array, i'd write it like this in javascript, would be damn similar in java:

Code:
    var result;
    result = '';
    for (var position = 0; position < aString.length; position = position + 1)
    {
        result = aString.charAt(position) + result
    };
    return result
You realize you just did his homework for him right?
__________________
Spookster
CodingForums Supreme Overlord
All Hail Spookster
Who gave you that Ugging infraction? Yeah that's right it was me!
Spookster is offline   Reply With Quote
Old 11-11-2004, 12:52 AM   PM User | #6
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas?

Last edited by cwl157; 11-11-2004 at 01:08 AM..
cwl157 is offline   Reply With Quote
Old 11-11-2004, 02:00 AM   PM User | #7
turbowrx
New Coder

 
Join Date: Nov 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
turbowrx is an unknown quantity at this point
Couple ways to do this, but I would recommend using the stringbuffer. These objects are made for modifying strings. Just create an array of vowel characters and make your comparisons. Loop through the stringbuffer and check to see if the character matches any index in the vowel array, if yes, delete it, if no, move on. Later.
turbowrx is offline   Reply With Quote
Old 11-11-2004, 11:40 AM   PM User | #8
squirellplaying
Regular Coder

 
Join Date: Jan 2004
Location: Maryland
Posts: 468
Thanks: 0
Thanked 0 Times in 0 Posts
squirellplaying is an unknown quantity at this point
Loop through the string changing each character to an int. Then check using the dec. value it returns. It's easier if you convert it to all lower case. If the character does not fall into one of the ranges you set, add it to a new string.

That's probably more of the way they want it done. If you want it done quick and easy look through here.

Last edited by squirellplaying; 11-11-2004 at 11:43 AM..
squirellplaying is offline   Reply With Quote
Old 11-11-2004, 12:57 PM   PM User | #9
KeZZeR
Regular Coder

 
Join Date: Oct 2004
Location: England
Posts: 282
Thanks: 0
Thanked 0 Times in 0 Posts
KeZZeR is an unknown quantity at this point
Quote:
Originally Posted by cwl157
Thanks a lot for the help ive been working on this all week and i couldnt figure it out. So now my next problem is i have to make a method that takes a string and removes the vowels then returns the string. Any Ideas?
lol, i had to do the exact same thing back in my old course. What/where are you doing this for study?
KeZZeR is offline   Reply With Quote
Old 11-11-2004, 07:16 PM   PM User | #10
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
thos 2 solutions sound a little complicated for my current level?
cwl157 is offline   Reply With Quote
Old 11-11-2004, 09:02 PM   PM User | #11
squirellplaying
Regular Coder

 
Join Date: Jan 2004
Location: Maryland
Posts: 468
Thanks: 0
Thanked 0 Times in 0 Posts
squirellplaying is an unknown quantity at this point
It doesn't get any easier then that. Read the second link I gave you. That is the similest that it gets.
squirellplaying is offline   Reply With Quote
Old 11-11-2004, 09:47 PM   PM User | #12
cwl157
New Coder

 
Join Date: Jun 2003
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
cwl157 is an unknown quantity at this point
i figured out the program and everything works to remove the vowels i just used an if statement that compared each char in the string to every different vowel and if they were not equal then it added that char to the final string.
cwl157 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 08:29 AM.


Advertisement
Log in to turn off these ads.