Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Define a String as multiple things

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Define a String as multiple things

    How do you define a single String variable as multiple things?

    i.e.
    String name = "Bob" and "Frank"

    I have tried the following:
    String name = "Bob", "Frank";
    String name = "Bob" || "Frank"; (I knew this wouldn't work since it is a boolean but I felt like it was worth trying)
    String name = "Bob"
    name.equals("Frank");

    Any contribution is appreciated.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Define a String as multiple things

    If I understand you correctly, you're looking to hold a list of strings? I would recommend using either an array or a collection.

    // array solution
    String name[] = new String[]{"Bob", "Frank"};

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    44
    My Mood
    Mad
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Define a String as multiple things

    what about having somthing where you take a lot of words and recognize them and somhow have a way to replace them is there a way to do that?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Define a String as multiple things

    a way to do that
    Some times there are methods to do things and some times you have to write the code yourself.
    The Collections and Collection classes have methods for some of what you want to do.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Define a String as multiple things

    Quote Originally Posted by helloworld922 View Post
    If I understand you correctly, you're looking to hold a list of strings? I would recommend using either an array or a collection.

    // array solution
    String name[] = new String[]{"Bob", "Frank"};
    I simply want to define one String variable as multiple things.

    For instance: String bigString = "WooHoo" and "HooWoo"
    Specifically what I'm looking for is this: String side = "One" AND "one" so that with or without capitals can be typed.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Define a String as multiple things

    I simply want to define one String variable as multiple things.
    That sounds like an array or a collection.

    with or without capitals can be typed.
    IF a variable can have more than one value (maybe hundreds), which value should it return to you when you use the variable in your code?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Define a String as multiple things

    Quote Originally Posted by bgroenks96 View Post
    Specifically what I'm looking for is this: String side = "One" AND "one" so that with or without capitals can be typed.
    If that's all you're looking to do, you might want to look at this: String (Java Platform SE 6)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    bgroenks96 (June 7th, 2011)

  9. #8
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Define a String as multiple things

    Quote Originally Posted by KevinWorkman View Post
    If that's all you're looking to do, you might want to look at this: String (Java Platform SE 6)
    Thank you! That is exactly what I needed!

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Define a String as multiple things

    That's quite different from your original request:
    String name = "Bob" and "Frank"

  11. #10
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Define a String as multiple things

    Quote Originally Posted by Norm View Post
    That's quite different from your original request:
    My original mentality was to just define the String as both "Bob" and "bob" (example). I wasn't aware that there was an equalsIgnoreCase function. That works much better for what I'm looking for.

  12. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Define a String as multiple things

    This is a good example of a time you should have described what your actual goal was, not how you thought you should go about accomplishing it. At first, I thought you were talking about Collections (or Enums) as well. This approach is listed as "Describe the goal, not the step" in the link in my signature on asking smart questions.

    Either way, I'm glad you eventually got it sorted, and the API should be one of your best friends (and your first stop for information) from here on out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Conversion from multiple ints to single string
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 12th, 2011, 10:08 PM
  2. [SOLVED] Splitting String by Multiple Delimiters
    By relixus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 4th, 2011, 08:54 PM
  3. How to define a string pattern using variables?
    By ice in forum What's Wrong With My Code?
    Replies: 8
    Last Post: January 7th, 2011, 10:45 PM
  4. adding multiple ints into a string
    By straw in forum Java Theory & Questions
    Replies: 1
    Last Post: March 18th, 2010, 06:02 PM
  5. ERROR, I CANT DEFINE IT
    By chronoz13 in forum AWT / Java Swing
    Replies: 7
    Last Post: December 2nd, 2009, 09:47 AM