Results 1 to 6 of 6
  1. #1
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default Retrieving a single value from a String array

    Hello,
    A while back I posted a topic about if there was a way to take a simple 2 word input and turn it into a name by capitalising the first letter, and making sure every other letter was lower case. After a lot of poking I managed to get something that not only does this; but trims space and all the other wonderful things the String class can do! It works, no problem, can take anything from "nAME sURNAME" to "nAmE SuRnAmE " and will always return "Name Surname".
    I then went on to stage 2, I was hoping to be able to construct an array in this method, so that it would return;
    Java Code:
    finalname[0] = Name Surname
    finalname[1] = Name
    finalname[2] = Surname
    This obviously doesn't include the constructors to assemble the name as I didn't think it was relevant (as it works). I end up with the String[] name that contains
    Java Code:
    [Name Surname, Name, Surname]
    and my question is; Is there a way of calling any individual value as a string and printing it to the console? Is it possible to call name[1] and get Name, then later name[2] and get Surname.
    I figure it isn't possible to just call them, and so far the only way of printing it at all is the Arrays.toString method which just prints all the contents.
    Thanks for your help,
    ~Karenthian~

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Is it possible to call name[1] and get Name

    Have you tried something like

    Java Code:
        // using name[1] as an argument
    System.out.println(name[1]);
        // using name[1] as something that can be assigned to some varaible
    String test = name[1];
    System.out.println("length=" + test.length());
        // using name[1] in an expression
    System.out.println("name[1] is" + name[1]);

    (we usually speak of name[1] as accessing the array rather than calling it)

  4. #4
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Ah! Pbrock you're a genius! I hadn't thought of assigning it as a value for another variable. Thank you for that, it works perfectly!
    ~Karenthian~

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome.

    The bottom line is that you can do anything with foo[bar] that you can do with any other expression: use it as an argument, return it from a method, assign it to a variable and use it a larger expression.

    [Edit] Missed one! You can also call methods of foo[bar] if it's an object. like name[1].length()

  6. #6
    Karenthian is offline Member
    Join Date
    Mar 2011
    Posts
    20
    Rep Power
    0

    Default

    Yeah, I've used it in expressions and returned it twice, but hadn't quite twigged how to call it, as if you do a simple println with an array[x] it...
    Okay... when I first tried to simply println an array value it threw an exception and wouldn't do it... I've just tried it again now and it's working perfectly accepting array[x] as an argument. I don't know what I've done, but thank you again! My program is nice and stable again!
    ~Karenthian~

Similar Threads

  1. Testing for single string in IF
    By carman12 in forum New To Java
    Replies: 3
    Last Post: 01-31-2011, 08:21 PM
  2. how to change a single index of a string
    By ftrengnr in forum New To Java
    Replies: 5
    Last Post: 11-26-2010, 05:08 AM
  3. array to single char
    By rfviki in forum New To Java
    Replies: 5
    Last Post: 11-04-2010, 03:58 PM
  4. Replies: 2
    Last Post: 03-27-2010, 08:22 PM
  5. Splitting single string into array elements
    By phil128 in forum New To Java
    Replies: 11
    Last Post: 01-12-2009, 12:51 PM

Tags for this Thread

Posting Permissions

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