Why does `if (guess != 'a' || guess != 'A' || ...)` not work? [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here is my code, I know the if statement is really long and the code could probably be more efficient, but I just want to know the answer to this as it is driving me nuts.
while (whileloop == 1)
{
if (guess != 'a' || guess != 'A' || guess != 'b' || guess != 'B' || guess != 'c' || guess != 'C' || guess != 'd' || guess != 'D' || guess != 'e' || guess != 'E' || guess != 'f' || guess != 'F' || guess != 'g' || guess != 'G' || guess != 'h' || guess != 'H' || guess != 'i' || guess != 'I' || guess != 'j' || guess != 'J' || guess != 'k' || guess != 'K' || guess != 'l' || guess != 'L' || guess != 'm' || guess != 'M' || guess != 'n' || guess != 'N' || guess != 'o' || guess != 'O' ||guess != 'p' || guess != 'P' || guess != 'q' || guess != 'Q' || guess != 'r' || guess != 'R' || guess != 's' || guess != 'S' || guess != 't' || guess != 'T' || guess == ' ')
{
System.out.println ("\nNot a Point. Try again.");
guess = IBIO.inputChar ("If the missile is launched from Point B, which Point will the missile hit? ");
}
else
whileloop = 2;
}

Because you wanted a logical and && (not a logical or ||). When you use || if any condition is true (such as the letter A not being the letter B, the entire expression is true). You could use an || with two ands. Something like
if (!(guess >= 'a' && guess <= 'z') || !(guess >= 'A' && guess <= 'Z'))

Related

Method that appends to a Stringbuilder based on user entered value

I have a homework problem that wants me to take a phone number and if the phone number has characters to determine what number that is. When I run my current method it returns the numbers but any characters input the method ignores them.
I have tried different versions of my current method.
class Telephone {
String telephoneNumber;
StringBuilder telephone = new StringBuilder();
public String translator(String telephoneNumber){
for(int i=0; i<telephoneNumber.length(); i++){
if(Character.isDigit(telephoneNumber.charAt(i))){
telephone.append(telephoneNumber.charAt(i));
} else if(telephoneNumber.charAt(i) == 'A' || telephoneNumber.charAt(i) == 'B' || telephoneNumber.charAt(i) == 'C') {
telephone.append(2);
} else if(telephoneNumber.charAt(i) == 'D' || telephoneNumber.charAt(i) == 'E' || telephoneNumber.charAt(i) == 'F'){
telephone.append(3);
} else if(telephoneNumber.charAt(i) == 'G' || telephoneNumber.charAt(i) == 'H' || telephoneNumber.charAt(i) == 'I'){
telephone.append(4);
} else if(telephoneNumber.charAt(i) == 'J' || telephoneNumber.charAt(i) == 'K' || telephoneNumber.charAt(i) == 'L'){
telephone.append(5);
} else if(telephoneNumber.charAt(i) == 'M' || telephoneNumber.charAt(i) == 'N' || telephoneNumber.charAt(i) == 'O'){
telephone.append(6);
} else if(telephoneNumber.charAt(i) == 'P' || telephoneNumber.charAt(i) == 'Q' || telephoneNumber.charAt(i) == 'R'){
telephone.append(7);
} else if(telephoneNumber.charAt(i) == 'T' || telephoneNumber.charAt(i) == 'U' || telephoneNumber.charAt(i) == 'V'){
telephone.append(8);
} else if(telephoneNumber.charAt(i) == 'W' || telephoneNumber.charAt(i) == 'X' || telephoneNumber.charAt(i) == 'Y' || telephoneNumber.charAt(i) == 'Z' ){
telephone.append(9);
}
}
return telephone.toString();
}
}
Current results: telephone entered 555555food returns 555555. I would need it to return 555 555 3662
Write a function that takes in two chars, converts them to Strings using String.valueOf() then compare them using equalsIgnoreCase(). And then use that function in all of your else-if conditions. That should make it cleaner.
EDIT
You can also convert the telephoneNumber to upper case (or lower case, your wish) and then proceed as you were as suggested by #Stultuske in the comments

if-else loop within a do while loop within another do while loop

When 'y' or 'Y' is inputted, I want the code to the prompt System.out.print("Y/y or N/n: "); starting from the first do,but instead the code will prompt System.out.print("bad answer, try again: ");, and I only want char besides y,Y,n,N to be the case.
The only code that follows is when 'n' or 'N' is entered, following System.exit(0);
import java.util.Scanner;
public class Test
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
char ch;
do
{
System.out.print("Y/y or N/n: ");
do
{
ch = kb.nextLine().charAt(0);
if (ch == 'n' || ch == 'N')
{
System.exit(0);
}
else if (ch != 'y' || ch != 'Y' || ch != 'n' || ch != 'N');
{
System.out.print("bad answer, try again: ");
}
}
while (ch != 'y' || ch != 'Y' || ch != 'n' || ch != 'N');
}
while (ch == 'y' || ch == 'Y');
}
}
Because you're using OR rather than AND, this condition will always be true:
else if (ch != 'y' || ch != 'Y' || ch != 'n' || ch != 'N')
{
System.out.print("bad answer, try again: ");
}
By the way, you could simplify this to have a single do-while. If the answer is bad, just go on to the next iteration of the outer loop.
Remove semicolon and check for ch and take AND.
Code could be like this:
else if (ch != 'y' && ch != 'Y' && ch != 'n' && ch != 'N')
{
System.out.print("bad answer, try again: ");
}
OR
Remove semicolon and check for ch and take OR and finally take NOT.
Code could be like this:
else if (!(ch == 'y' || ch == 'Y' || ch == 'n' || ch == 'N'))
{
System.out.print("bad answer, try again: ");
}

Java if and || issue

I'm doing this little program, but unfortunately I ran into this issue..
if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3') {
System.out.println("The String entered does not fit any of the Credit card standards");
System.exit(0);
}
My program does not recognize if I put in any of the integers in my String.
However, if I delete my || and the last part, the if statement recognizes the first integer.
What am I missing here?
if (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3')
Is always true.
Every character is != '4' or != '3'
I guess you want && instead.
Details:
The statement A || B is true if A is true or B is true (or both are true).
In your example, lets say that the first character is '4'.
A = ccnString.charAt(0) != '4' is false (4 != 4 is false)
B = ccnString.charAt(0) != '3' is true (3 != 4 is true)
So A || B is true because B is true.
This is an addition to the many other answers that correctly state that you must use and (&&) instead of or (||).
You have been fooled by De Morgan's laws. They define how boolean expressions are negated.
In your example, the original expression that defines a valid user input is as follows:
validInput = userPressed3 or userPressed4
But as we are interested in invalid user input, this expression has to be negated:
not(validInput) = not(userPressed3 or userPressed4)
According to De Morgan, not(A or B) is equal to not(A) and not(B). So we can also write:
not(validInput) = not(userPressed3) and not(userPressed4)
In other words: It's De Morgan's fault! ;)
You probably want
if (ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3') {
System.out.println("The String entered does not fit any of the Credit card standards");
System.exit(0);
}
This would give your error message only for Strings that don't start with 4 AND don't start with 3.
Your original condition gives an error for any String that either doesn't start with 4 OR doesn't start with 3, and since all Strings satisfy that condition, you'll always get your error message.
If you require additional conditions after the initial test, you can do :
if (ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3') {
System.out.println("The String entered does not fit any of the Credit card standards");
System.exit(0);
} else if (ccnString.charAt(0) == '3' && ccnString.charAt(1) == '7') {
// here you know that ccnString starts with 37
} else if (...) {
...
}
... add as many else ifs as you need ...
else {
// default behavior if all previous conditions are false
}
It should be && not ||
ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3'
Else (ccnString.charAt(0) != '4' || ccnString.charAt(0) != '3' always true
if ((ccnString.charAt(0) != '4' && ccnString.charAt(0) != '3')
|| (ccnString.charAt(0) == '3' && ccnString.charAt(1) == '7')) {
System.out.println("The String entered does not fit any of the Credit card standards");
System.exit(0);
}

JAVA if statements logic or syntax error? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
I getting the wrong result every time I run this program and I feel like an extra pair of eyes would be helpful at 4 in the morning. Can someone please help find where my curly braces or parentheses are off because I cannot find it for the life of me.
System.out.println("Please enter your guess") ;
userGuess = kbd.next() ;
if( userGuess != "a" || userGuess != "b" || userGuess != "c" ||
userGuess != "d" || userGuess != "e" || userGuess != "f" || userGuess != "g" ||
userGuess != "h" || userGuess != "i" || userGuess != "j" || userGuess != "k" ||
userGuess != "l" || userGuess != "m" || userGuess != "n" || userGuess != "o" ||
userGuess != "p" || userGuess != "q" || userGuess != "r" || userGuess != "s" ||
userGuess != "t" || userGuess != "u" || userGuess != "v" || userGuess != "w" ||
userGuess != "x" || userGuess != "y" || userGuess != "z" || userGuess!= "A" ||
userGuess != "B" || userGuess != "C" || userGuess != "D" || userGuess != "E" ||
userGuess != "F" || userGuess != "G" ||userGuess != "H" || userGuess != "I" ||
userGuess != "J" || userGuess != "K" ||userGuess != "L" || userGuess != "M" ||
userGuess != "N" || userGuess != "O" || userGuess != "P" || userGuess != "Q" ||
userGuess != "R" || userGuess != "S" || userGuess != "T" || userGuess != "U" ||
userGuess != "V" || userGuess != "W" || userGuess != "X" || userGuess != "Y" ||
userGuess != "Z" ) {
System.out.println("Invalid character, please enter your guess") ;
}userGuess = kbd.next() ;
Strings should be compared with the .equals() method and not ==.
That being said, in your case you might want to take a look at regular expressions, which would allow you to do a clean validation of the input. So in short:
//This code is untested, but it should guide you to what you need to do
Pattern userInput = Pattern.compile("^[A-Za-z]$"); //A-Z will match all the characters ranging from A to Z. a-z will do the same but it will check the lower case range. Alternatively, you could use ^[a-z]/i$ to make your regular expression case insensitive.
Scanner kbd = new Scanner(System.in);
String input = kbd.next();
Matcher matcher = userInput.matcher(input);
if(!matcher.matches())
{
System.out.println("Invalid character, please enter your guess") ;
}
You need to change == to equals to compare values of String (and any other object).
Also you have problem with logic, because userGuess != "a" || userGuess != "b" is true for all characters since if something is a then it is not b so one of these conditions will be always true.
You may want to change || to && or use proper tools like regex, or methods from Character class like Character.isLetter.

l33t why is it printing three U's

i am having a problem i am making a l33t translator and my translator from English to l33t works for my l33t to English works other then u and what is happening is it is printing three U's i have tryed taking it out of the for loop and it will only print one but it prints it at the start of the word i have tryed putting it at the bottom out of the for and it doesnt even print one. i have also tryed if(phrase.charAt(i) == '|') && phrase.charAt(i+1) == '_' && phrase.charAt(i+2) == '|'). here is what i got.
public static String translateToEnglish(String phrase) {
Scanner scan = new Scanner(System.in);
System.out.println("Hello what pharse would you like to convert in to l33t");
phrase = scan.nextLine();
String NewString = "";
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) == '4') {
NewString += "a";
}
if (phrase.charAt(i) == '8') {
NewString += "b";
} else {
if (phrase.charAt(i) == '3') {
NewString += "e";
}
}
if (phrase.charAt(i) == '1') {
NewString += "l";
} else {
if (phrase.charAt(i) == '0') {
NewString += "o";
}
}
if (phrase.charAt(i) == '5') {
NewString += "s";
} else {
if (phrase.charAt(i) == '7') {
NewString += "t";
}
}
if (phrase.contains("|_|")) {
NewString += "u";
}
if (phrase.charAt(i) == '2') {
NewString += "z";
}
if (phrase.charAt(i) == 'c' || phrase.charAt(i) == 'd' || phrase.charAt(i) == 'f' || phrase.charAt(i) == 'g'
|| phrase.charAt(i) == 'h' || phrase.charAt(i) == 'i'
|| phrase.charAt(i) == 'j' || phrase.charAt(i) == 'k' || phrase.charAt(i) == 'm' || phrase.charAt(i) == 'n'
|| phrase.charAt(i) == 'p' || phrase.charAt(i) == 'q'
|| phrase.charAt(i) == 'r' || phrase.charAt(i) == 'v' || phrase.charAt(i) == 'w' || phrase.charAt(i) == 'x'
|| phrase.charAt(i) == 'y') {
NewString += phrase.charAt(i);
}
// if (phrase.charAt(i) == 'c') {
}
System.out.println(NewString);
return phrase;
}
For every char in the word you check if it contains a "u". It will always be true because you don't check at a specific position of the word. You would have to check for a | followed by _ followed by | and then add a "u" instead of generally checking if it's somewhere in the input.
Your if statement for |_| is doing a String#contains comparison and is not elsed. So for every iteration in the loop it will print a u if the phrase contains this sequence of characters.
If you use your alternative of checking each char at i, i+1, i+2 you will firstly have to make sure that your phrase is long enough and then if it is true, in that if statement you will have to make sure you increment i by 3 i.e
if(phrase.length() < i+2
&& phrase.charAt(i) == '|')
&& phrase.charAt(i+1) == '_'
&& phrase.charAt(i+2) == '|')
{
NewString += "u";
i += 2; // Will get the third increment from loop
continue;
}
Also if you make sure the structure is always if..if else...else, the final check where is just replaces with the same character can just be reduced to an else without needing to or together every other character

Categories