Results 1 to 13 of 13
  1. #1
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default 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.

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

  3. #3
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    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.

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    ... and adding to the above what Steve said, it can all be done with one "while" loop !!!

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    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? ;-)

  7. #7
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    it can be done in java, but i always imagine c being used for the impractical challenges.

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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).

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  10. #10
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    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

  11. #11
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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)

  12. #12
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    I like the StringBuffer solution a lot better...

  13. #13
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    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
    Java Code:
    for(int i=0; i<100000; i++)
      for(String s : sArray){
        Word.isPalindrome(s);
      }
    and the timing for String.toCharArray() solution would be even less if it's a mixed list.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

Similar Threads

  1. NullPointerException converting String to double
    By infaddict in forum New To Java
    Replies: 3
    Last Post: 07-19-2008, 06:01 PM
  2. Converting object to string
    By Preethi in forum New To Java
    Replies: 4
    Last Post: 06-14-2008, 03:29 AM
  3. Converting an Array to a Vector
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:44 PM
  4. Converting String to Double
    By srini in forum New To Java
    Replies: 1
    Last Post: 12-24-2007, 08:03 PM
  5. Converting Epoch to string Date
    By amyedwards in forum New To Java
    Replies: 3
    Last Post: 12-14-2007, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •