0

So, I'm rather new to Java and I just starting a project, but I ran into some issues.

My question is... How do I link a user input (argument) with a String?

I have already defined a few Strings earlier on in my code, but in this line I want it to match up and check from the String which matches the argument and check if it contains something:

if (!cs.hasPermission("foo." + args[0]) &&   [CODE HERE]   ){

I want [CODE HERE] to check If args[0] (user input) matches a String, if it does then check if it matches some text.

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
user2033914
  • 1
  • 1
  • 2

3 Answers3

0

This is String equals api, so it should look something like:

if (!cs.hasPermission("foo." + args[0]) && args[0].equals(string){
  //code
}
Grammin
  • 11,808
  • 22
  • 80
  • 138
0

Java has a .equals() method which can be used to compare two Strings. It can used to compare two variables which hold references to String objects or to compare String literals

if( args[0].equals(someString) ) { // compare args[0] to another String variable
}

if( "someText".equals(args[0]) ) { // compare args[0] to a String literal
}

Reading through the String documentation will also be very useful to you starting out.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
  • Thanks for your contribution, but I don't think you're getting what I mean... I want it to get the argument and match it with the CORRESPONDING string. :( – user2033914 Oct 22 '13 at 17:12
  • What corresponding string are you talking about? I don't see one in your question. – Hunter McMillen Oct 22 '13 at 17:29
  • Earlier on in my code I define many different strings. What I want [CODE HERE] to do is match the Args[0] with the corresponding string. – user2033914 Oct 22 '13 at 17:40
  • then replace `someString` in the code above with the name of your own variable. – Hunter McMillen Oct 22 '13 at 17:40
  • For example: Defined Strings: Test, Foo, Stack Args[0] Input: Foo In this case code here would check some text from the string 'Foo' – user2033914 Oct 22 '13 at 17:41
  • But my variable would be a single string...? Please give another example, really confused! :S – user2033914 Oct 22 '13 at 17:41
  • so you want to test some variable `Foo` for the actual value `"Foo"`? If you could post the String you have defined in your question, it would be really helpful. – Hunter McMillen Oct 22 '13 at 17:42
  • I don't get you... All I want is a way to match a input with ONE of MANY strings. So if input is foo, I want it to match string foo. If the input is test, then I want to match string test. I defined strings with String ; and then later on in my code I set the values for each of these strings . Sorry for the inconvenience – user2033914 Oct 22 '13 at 17:50
0

You can easily do it with the equals() method. But you should also check that args[0] is set.

if (args.length > 0) {
    if (!cs.hasPermission("foo." + args[0]) && "StringToCompare".equals(args[0])) {
        // do something
    }
} else {
    // handle error
}
noone
  • 19,520
  • 5
  • 61
  • 76