How can i create a two word variable?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: How can i create a two word variable?

  1. #1
    Join Date
    Oct 2004
    Posts
    6

    How can i create a two word variable?

    I am making a programming that allows a user to search a database of player statistics. I am using conditionals, and I was wondering how i could make a variable two words, (The Players First Name and the Players Last Name). Here is what i mean.

    public class Players
    {
    public static void main (String[] args) throws IOException
    {
    Buffered reader......

    Here is what i want ----> String John Smith;
    String playername;

    System.out.print("Player Name::");
    playername = keyboard.readline..

    if(playername.equals("John Smith"){
    System.out.print(Stats);
    }
    }

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    What you are trying to do poses some problems.

    In general it is considered pretty bad form to make a variable name look like a constant. Variables should be variable and shouldn't necessarily be specific to one person.

    You could do something like a constant:

    public final String NAME = "John Smith" if you're certain you will always be using it in your code.

    You could name a string variable John_Smith, but it would do nothing for you so I don't know how to adequately answer this.

  3. #3
    Join Date
    Oct 2004
    Posts
    6
    in my conditional could i make a statement that requires the first and last name to be present, and just define each part of the name by itself, such as

    String John;
    String Smith;

    if(playername.equals("John" and "Smith").

  4. #4
    Join Date
    Sep 2004
    Posts
    150
    I'm still baffled by why you would want to name a variable after a name that is going to be put inside of it.

    The only other thing I can think of is to make your own "name" object and make comparisons based on it.

    Of course you might want to add other stats to Name and then it would become more of a "Player" class anyway.

    public class Name
    {

    private String first, last;

    public Name(String first, String last)
    {
    this.first = first;
    this.last = last;

    }

    public String getFirst()
    {
    return first;
    }

    public String getLast()
    {
    return last;
    }

    public boolean equals(Name otherName)
    {
    // return true if and only if both the first and last names match
    if (first.equals(otherName.getFirst())
    && last.equals(otherName.getLast())

    return true;

    else return false;

    }

    }


    Expanding on the above idea might be helpful to you. Other than that I don't know what you hope to gain from it.

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    in my conditional could i make a statement that requires the first and last name to be present, and just define each part of the name by itself, such as

    String John;
    String Smith;

    if(playername.equals("John" and "Smith").
    I'm not sure you have the right idea about how the variables work. The name of the variable is totally irrelevant. You can call it whatever you want and it doesn't matter. All that matters is what it points to. In the above code John and Smith point to nothing, so they are null.

    Also, they are not used in your conditional. They have no relation whatsoever to the string literals "John" and "Smith" in the if statement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center