I am trying to write a program that forms a random 2 character word and then asks the user to guess the word. After the user inputs a guess, the program should check the guess with the answer. The program should then give the user a hint depending on their guess in the form nAkB where nA is the right letters in the right position and kB is the right letters but in the wrong position. My problem is that my program won't show the hint when their are right letters but in the wrong position (say the answer is aD and the user guesses Dc) the output should be "Hint: 0A1B". Here is my code for the getHint Method:
public static String getHint(String guess, String answer){
String hint = "No";
if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "You win!";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) != answer.charAt(0) && guess.charAt(1) == answer.charAt(1)){
hint = "Hint: 1A0B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(1) == answer.indexOf(guess.charAt(1))){
hint = "Hint: 0A2B";
}
else if(guess.charAt(0) == answer.charAt(0) && guess.charAt(1) == answer.indexOf(guess.charAt(1)) &&
guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(1) == answer.charAt(1) && guess.charAt(0) == answer.indexOf(guess.charAt(0)) &&
guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 1A1B";
}
else if(guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0) &&
guess.charAt(1) != answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1)){
hint = "Hint: 0A1B";
}
else if(guess.charAt(1) == answer.indexOf(guess.charAt(1)) && guess.charAt(1) != answer.charAt(1) &&
guess.charAt(0) == answer.indexOf(guess.charAt(0)) && guess.charAt(0) != answer.charAt(0)){
hint = "Hint: 0A1B";
}
else{
hint = "Hint: 0A0B";
}
return hint;
It never outputs anything with a 1B or 2B. It only does 1A0B or 0A0B.
Your issue is code like guess.charAt(0) == answer.indexOf(guess.charAt(0)) . indexOf will return the index of the character. So you are comparing the index of a character (rhs) to the ASCII value of the character (lhs) and your if statements using this indexOf construction are not being entered.
Rather, consider something like this:
else if(answer.indexOf(guess.charAt(0))!=-1 && answer.indexOf(guess.charAt(1)) != -1{
hint = "Hint: 0A2B";
}
From Java API, indexOf returns -1 if the given character is not found within the string.
Related
Why is the first if statement always true?
private String setDepartment (){
int code = Integer.parseInt(JOptionPane.showInputDialog("Enter The Department Code:\n" +
"1:Sales\n" +
"2:Development\n" +
"3:Accounting\n" +
"4:None"));
/*Why this if statement is always true? How do i solve it? */
if (code !=1 || code !=2 || code !=3 || code !=4)
{
JOptionPane.showMessageDialog(null, "Invalid Number.Enter a number between 1-4");
setDepartment();
}
if (code==1){
return "Sales";
}
else if (code==2){
return "Development";
}
else if (code==3){
return "Accounting";
}
else
return "";
}
Replace || with &&:
if (code !=1 && code !=2 && code !=3 && code !=4)
You need to use AND instead of OR.
If the user enters 1 then it's automatically different than 2,3 and 4.
You can therefore use :
if (code !=1 && code !=2 && code !=3 && code !=4)
OR
if (code ==1 || code ==2 || code ==3 || code ==4)
From first principles, picking code = 1:
code !=1 || code !=2 || code !=3 || code !=4
= 1 !=1 || 1 !=2 || 1 !=3 || 1 !=4
= false || true || true || true
= true
You probably meant && rather than ||.
Because at any point of time code value would be any of 1-4. And 3 of 4 conditions would always be true. Change your if condition to
If( !( code ==1 || code == 2 || code == 3 || code == 4))
I have this password system that relies on four character types to determine whether a password is one of four categories:
boolean upper=false;
boolean lower=false;
boolean number=false;
boolean symbol=false;
for(int i=0; i<password.length(); i++)
{
character=password.charAt(i);
if ((character>=65)&&(character<=90)){
System.out.println("This character is a uppercase letter:" +character);
upper=true;}
if ((character>=97)&&(character<=122)){
System.out.println("This character is a lowercase letter:" +character);
lower=true;}
if ((character>=48)&&(character<=57)){
System.out.println("This character is a number:" +character);
number=true;}
if (((character>=33)&&(character<=47))||((character>=58)&&(character<=64))||((character>=93)&&(character<=96))||((character>=123)&&(character<=126))){
System.out.println("This character is a symbol:" +character);
symbol=true;}
}
This is how I put them into four categories: WEAK, MEDIUM, STRONG, and SUPERSTRONG:
boolean weak=false;
boolean medium=false;
boolean strong=false;
boolean superstrong=false;
int level = 0;
if((upper=true) || (lower=true) || (number=true) || (symbol=true)) {
weak=true;
level++;}
if (((upper=true) && (lower=true)) || ((upper=true) && (number=true)) || ((upper=true) && (symbol=true)) || ((lower=true) && (symbol=true)) || ((lower=true) && (number=true)) || ((number=true) && (symbol=true))){
medium=true;
level++;}
if(((upper=true) && (lower=true) && (number=true)) || ((upper=true) && (lower=true) && (symbol=true)) || ((upper=true) && (number=true) && (symbol=true)) || ((lower=true) && (number=true) && (symbol=true))){
strong=true;
level++;}
if((upper=true) && (lower=true) && (number=true) && (symbol=true)){
superstrong=true;
level++;}
if(level >= 1){
System.out.println("This password is weak ");}
if(level >= 2){
System.out.println("This password is medium.");}
if(level >= 3){
System.out.println("This password is strong.");}
if(level >= 4){
System.out.println("This password is super strong.");}
}
The problem is that it compiles without error, and the password checker checks the password to the right category, but the problem is that no matter how many types of characters there are in the password, the category will always be outputted as SUPERSTRONG. I have tried this on all lowercase passwords and all other types of password possible in this system but this always happens.
How may I solve this problem?
Here's the code in its entirety if it helps:
import java.util.Scanner;
public class passwordChecker
{
public static void main(String [] args)
{
boolean repeat=true;
String password;
int length;
while(repeat==true){
System.out.println ("Please enter your password.");
Scanner scan = new Scanner(System.in);
password=scan.nextLine();
length=password.length();
if(length<=6 | length>=12) {
System.out.println("Your password does not meet the requirements.
Please enter a new password.");
}
else {
System.out.println("Your password meets the criteria.");
char character='\0';
boolean upper=false;
boolean lower=false;
boolean number=false;
boolean symbol=false;
for(int i=0; i<password.length(); i++)
{
character=password.charAt(i);
if ((character>=65)&&(character<=90)){
System.out.println("This character is a uppercase letter:" +character);
upper=true;}
if ((character>=97)&&(character<=122)){
System.out.println("This character is a lowercase letter:" +character);
lower=true;}
if ((character>=48)&&(character<=57)){
System.out.println("This character is a number:" +character);
number=true;}
if (((character>=33)&&(character<=47))||((character>=58)&&`enter code here`(character<=64))||((character>=93)&&(character<=96))||`enter code here`((character>=123)&&(character<=126))){
System.out.println("This character is a symbol:" +character);
symbol=true;}
}
boolean weak=false;
boolean medium=false;
boolean strong=false;
boolean superstrong=false;
int level = 0;
if((upper=true) || (lower=true) || (number=true) || (symbol=true)) {
weak=true;
level++;}
if (((upper=true) && (lower=true)) || ((upper=true) && (number=true)) || ((upper=true) && (symbol=true)) || ((lower=true) && (symbol=true)) || ((lower=true) && (number=true)) || ((number=true) && `enter code here`(symbol=true))){
medium=true;
level++;}
if(((upper=true) && (lower=true) && (number=true)) || ((upper=true) && (lower=true) && (symbol=true)) || ((upper=true) && (number=true) && (symbol=true)) || ((lower=true) && (number=true) && (symbol=true))){
strong=true;
level++;}
if((upper=true) && (lower=true) && (number=true) && (symbol=true)){
superstrong=true;
level++;}
if(level >= 1){
System.out.println("This password is weak ");}
if(level >= 2){
System.out.println("This password is medium.");}
if(level >= 3){
System.out.println("This password is strong.");}
if(level >= 4){
System.out.println("This password is super strong.");}
}
System.out.println("Do you want to enter another password?");
Scanner scan2 = new Scanner (System.in);
String choice=scan2.nextLine();
if(choice.equals("yes")||choice.equals("Yes")) repeat = true;
else{
repeat=false;
}}}}
Sorry if the code looks like it shouldnt. It should just work all fine if you type it in as normal.
When you do
upper=true
you are assigning upper to be true.
So when you do
if (upper=true)
this is the same as
upper=true;
if (upper)
I suspect you wanted to use == for comparison however a better solution is to use the boolean as a condition.
if (upper || lower || number || symbol) {
BTW a better way to check for characters is
if (Character.isUppercase(ch)) {
// upper case
} else if (Character.isLowercase(ch)) {
// is lower
} else if (Character.isDigit(ch)) {
// is digit
} else {
// is symbol
}
and for levels you can do
int level = (upper?1:0) + (lower?1:0) + (number?1:0) + (symbol?1:0);
This is for an assignment in data structures and I can't seem to find the exact answer I need.
I am attempting to take user input for a date, preferably YY-MM-DD format, then check for validity.. if not valid, cycling until valid. This really seems very tedious and I am receiving two illegal starts of expression errors.
I did a search on Google and StackOverflow, but only find things addressing Date
public String hireDate(){
Scanner input = new Scanner(System.in);
boolean answer = false;
while(answer == false){
String temp = input.nextLine();
if((temp.charAt(0) >= 0 && temp.charAt(0) <= 9) && (temp.charAt(1)
>= 0 && temp.charAt(1) <= 9) && (temp.charAt(2) == -)
&& (temp.charAt(3) >= 0 && temp.charAt(3) <= 9) && (temp.charAt(4)
>= 0 && temp.charAt(4) <= 9) && (temp.charAt(5) == -)
&& (temp.charAt(6) >= 0 && temp.charAt(6) <= 9)){
answer = true;
} else {
answer = false;
}
}
return temp;
}
You can do it easily with Regex. Just check if your input string matches your pattern (YY-MM-DD)
if (temp.matches("\\d{2}-\\d{2}-\\d{2}"))
temp.charAt(1)>=0
You are comparing a character to an integer.
Problem specification: Infinite loop ( menu enters an infinite loop );
Objective: Request user for an integer input and proceed in accordance to specified logic inside of the code. How to avoid the infinite loop?;
Code:
public Purchase groceryStoreMenu(LemonadeStand lemonadeStand){
boolean getMenu = true;
int userEnteredNumber = -1;
currentPurchase = new Purchase();
while(getMenu){
try{
System.out.println("Grocery Store");
System.out.printf("%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n%s\t%s%n" , "1:" , "Buy lemons", "2:", "Buy cups" , "3:" , "Buy sugar" ,
"4:" , "Buy ice" , "5:" , "Done"); //change this
userEnteredNumber = reader.nextInt();
if (userEnteredNumber == 1 ) {
money = lemonadeStand.profit(0);
lemonsMenu(money);
}else if (userEnteredNumber == 2){
money = lemonadeStand.profit(0);
cupsMenu(money);
}else if (userEnteredNumber == 3){
money = lemonadeStand.profit(0);
sugarMenu(money);
}else if (userEnteredNumber == 4){
money = lemonadeStand.profit(0);
iceMenu(money);
}else{
money = lemonadeStand.profit(0);
dailyGreetingMenu();
}
if (userEnteredNumber != 1 && userEnteredNumber !=2 && userEnteredNumber != 3
&& userEnteredNumber != 4 && userEnteredNumber != 5) {
throw new Exception();
} else if(userEnteredNumber == 6) {
getMenu = false;
//break;
} else {
getMenu = false;
//break;
}
}
catch(Exception e)
{
System.out.println("Error in number format. Enter a valid number from the choices (1,2,3,4,5,6)");
}
}
return currentPurchase;
Your program never reaches if(userEnteredNumber == 6) with 6 value, because if userEnteredNumber is 6, the first condition will grab it:
if (userEnteredNumber != 1 && userEnteredNumber !=2 && userEnteredNumber != 3
&& userEnteredNumber != 4 && userEnteredNumber != 5) {
throw new Exception();
}
then it throws exception, and it is catched within loop block. And any number other than 1, 2, 3, 4, 5, 6 is the same. So the loop never ends.
I don't like your code-style at all. But if you want to do it this way, you shall add userEnteredNumber != 6 to first if condition.
looks like the execution is never gonna enter the following block:
else if(userEnteredNumber == 6) {
getMenu = false;
//break;
}
the if condition should not contain '&&'.it should have been '||'
Also looks like you dont want your user to enter any number other than 1,2,3,4,5.
if (userEnteredNumber != 1 || userEnteredNumber !=2 || userEnteredNumber != 3
|| userEnteredNumber != 4 || userEnteredNumber != 5) {
getMenu = false;
throw new Exception();
}
This loop keeps giving me my error message even when I enter a valid input and I can't find what is wrong with it. It's the same as my other loops in the program which all work fine. Does anyone know the problem? Here is the loop:
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
while((gender != 'm') || (gender != 'f')) {
System.out.println("ERROR Please enter a valid age");
System.out.println("Male or Female (M/F)");
gender = userInput.next().charAt(0);
gender = Character.toLowerCase(gender);
}//end while
Now if I type 'm' or 'f' it will give me the error message? ??? ???
while((gender != 'm') || (gender != 'f')) {
should really be
while((gender != 'm') && (gender != 'f')) {
The condition (gender != 'm') || (gender != 'f') always evaluates to true. To understand why, consider one case - gender = 'm'...
(gender != 'm') || (gender != 'f')
('m' != 'm') || ('m' != 'f')
false || true
true
Basically, java doesn't know the common English idiom if gender isn't (either) male or female.