-
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);
}
}
-
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.
-
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 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.
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks