Results 1 to 13 of 13
- 02-18-2009, 04:43 PM #1
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
Converting a String into a array.
Hi,
I'm working on a program right now where I need to convert a String into an array. I thought I got it working, but I keep having runtime errors.
This is the coding I have so far:
String phrase;
phrase = keyboard.next();
String[] userPhrase = new String[80];
userPhrase = phrase.split("");
If the "user" enters something without spaces (for ex. ABA) it works fine, but if it has spaces I keep getting errors.
I'm assuming I don't have the correct parameters when calling the split method? But I'm having a hard time figuring out what I need to put in place of the quotation marks. If someone could point me in the right direction I would appreciate it.
- 02-18-2009, 04:50 PM #2
First, don't create an array when you define userPhrase. That array gets thrown away immediately. Use "null" instead.
The expression used in split("") must be a regular expression. If you want to split on blanks, use " +".
Here's a good regular expression site.
- 02-20-2009, 06:07 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
I'm still a little confused. I realized what I was trying to do wouldn't even make logical sense if it had worked.
Let me explain what I'm trying to do better. We're working on a palindrome program that checks to see if a word if the same frontwards as backwards. But, it's required we use at least one array to check. What I need to do is take a string the user enters and turn it into an array of characters (that includes spaces as characters as well.)
Am I even on the right track with the string method or is there something else I should be looking into?
-Thanks.Last edited by taraxgoesxboom; 02-20-2009 at 06:10 PM.
- 02-20-2009, 06:31 PM #4
First, use String.toCharArray().
Second, think about how you would practically define a palindrome. Using only plain English words, describe the steps you would follow to manually test an entry. Turn those steps into code.
Here's a thought about an "elegant" solution. An array can be accessed from both ends by using two indexes.
- 02-20-2009, 07:18 PM #5
... and adding to the above what Steve said, it can all be done with one "while" loop !!!
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-20-2009, 08:58 PM #6
We should have a Java Challenge to see who can cram all the code into a single "for" statement while using as few letters as possible. Or is that a C Challenge? ;-)
- 02-20-2009, 11:42 PM #7
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
it can be done in java, but i always imagine c being used for the impractical challenges.
- 02-21-2009, 01:30 AM #8
Playing in my bit box ...
yeah... sounds fun ... did it with "for" loop, two variables and 4 lines of code (made it as a method).
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 02-21-2009, 03:25 AM #9
ah ha! i've done it in 108 characters. not very readable though...
Java Code:// "as few letters as possible." public boolean p(char[] c){ for([B]/*...*/[/B])if([B]/*...*/[/B])return false; return true; }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-21-2009, 03:32 AM #10
Senior Member
- Join Date
- Sep 2008
- Posts
- 564
- Rep Power
- 5
after trying it out, if this were c, you would be able to cut more corners, since booleans are just ints. and it would be a lot easier to do in c if you actually require something that'll compile
- 02-22-2009, 02:42 AM #11
less effective, but i wrote a palindrome method using StringBuffer.reverse() and String.compareTo(). max is 93 chars and no for loop.
USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
- 02-22-2009, 03:12 AM #12
I like the StringBuffer solution a lot better...
- 02-22-2009, 05:29 AM #13
Its better in readibility, but not performance. I ran it on a list of palindromes for 100,000 loops.
Java Code:String[] sArray = {"aibohphobia","alula","cammac","civic","deified","deleveled","detartrated","devoved","dewed","evitative","Hannah","kayak","kinnikinnik","lemel","level","madam","Malayalam","minim","murdrum","peeweep","racecar","radar","redder","refer","reifier","repaper","reviver","rotator","rotavator","rotor","sagas","solos","sexes","stats","tenet","terret","testset" };Java Code://String.toCharArray() solution Time: 1.181 secs //StringBuffer.reverse() solution Time: 4.366 secs
and the timing for String.toCharArray() solution would be even less if it's a mixed list.Java Code:for(int i=0; i<100000; i++) for(String s : sArray){ Word.isPalindrome(s); }USE CODE TAGS--> [CODE]...[/CODE]
Get NotePad++ (free)
Similar Threads
-
NullPointerException converting String to double
By infaddict in forum New To JavaReplies: 3Last Post: 07-19-2008, 06:01 PM -
Converting object to string
By Preethi in forum New To JavaReplies: 4Last Post: 06-14-2008, 03:29 AM -
Converting an Array to a Vector
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:44 PM -
Converting String to Double
By srini in forum New To JavaReplies: 1Last Post: 12-24-2007, 08:03 PM -
Converting Epoch to string Date
By amyedwards in forum New To JavaReplies: 3Last Post: 12-14-2007, 10:33 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks