0

I'm trying to make my program validate between the use of two single characters that are input by the user, which must be A or M.

Here's my code I have thus far:

static char getCustomerType() {
    System.out.println("Please enter the term for the Policy the client would like");
    System.out.println("A for Annual or M for Monthly. Annual provides the user with a 10% discount");

    String s = inputs.next();

    while (s != 'A' && s != 'M') {
        System.out.println("Incorrect, please try again");
        s = inputs.next();
    }
}

Netbeans however, does not like this stating the inputs.next is never used when I have set it to be used before the while statement? It also doesn't like the while statement producing incompatible string type referencing boolean to string.

I assume this is because I have declared s as a String?

Arvind Gupta
  • 63
  • 1
  • 1
  • 12
FUBAR
  • 9
  • 1
  • 6
  • 2
    A `String` isn't a `char`, and `!=` is different from `! =`... – fvu Jan 18 '17 at 12:12
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – moe Jan 18 '17 at 12:14
  • Changed to String and altered the it to !=, throws error String cannot be converted to Boolean and still throwing error about bad operand type for char, even after changing the method from Char to String and changing "s" to "letter" – FUBAR Jan 18 '17 at 12:17

2 Answers2

1

why not write

while ( ("A".equalsIgnoreCase(s) || "M".equalsIgnoreCase(s)) == false) 
fvu
  • 32,488
  • 6
  • 61
  • 79
martin
  • 11
  • 1
1

You can have single characeter input from user using below code assuming inputs is your scanner object:

char c = inputs.next(".").charAt(0);

and then you can compare it using != or .equals() or .equalsIgnoreCase()

Arvind Gupta
  • 63
  • 1
  • 1
  • 12
  • Yup, inputs is my scanner object declared in the main class of the code which is called is several methods throughout. I assume by using this, then I could set the method to a char type rather than string? – FUBAR Jan 18 '17 at 12:26
  • are you talking about getCustomerType() method ? Did my answer solve your problem, if yes, please accept it. thanks – Arvind Gupta Jan 18 '17 at 12:55
  • Yeah talking about that one; struggling a bit getting it work your way though System.out.println("Please enter the term for the Policy the client would like"); System.out.println("A for Annual or M for Monthly. Annual provides the user with a 10% discount"); char c = inputs.next(".").charAt(0); while (("A".equalsIgnoreCase(c) || "M".equalsIgnoreCase(c)) == false) { System.out.println("Incorrect, please try again"); c = inputs.next; Can't see what i put as my return statement and it doesn't like the .next where I ended – FUBAR Jan 18 '17 at 13:05
  • why are you using c = inputs.next; again in while loop, use c = inputs.next(".").charAt(0); only. hope this helps :) – Arvind Gupta Jan 18 '17 at 13:07
  • Ahhh that works haha, how do I now send that back to return? – FUBAR Jan 18 '17 at 13:27