jeudi 1 décembre 2016

Java "Type of expression must be an array but it resolved to a char " Error in For Loop

So I'm creating a program that creates x amount of random letters based on users input, for example if the user put 7, it will created 7 random letter and the user has to figure the 7 letters. I'm not sure why it is showing an error.

Here is my program:

import java.util.Scanner;
import java.util.Random;

public class CodeBreaker {
Random rand = new Random();// Random Class
private boolean done; // Checks if user Enters "quit".
private char letterValue; // Creating Secret Code.
private int numberOfChar; // Number of letters user wants.
private Scanner input = new Scanner(System.in); // Get input from the user.
private String guess; // User's letter input.

public CodeBreaker() { // Constructor.
    System.out.print("How many letters would you like to create?: ");
    numberOfChar = input.nextInt();

    while (numberOfChar < 0 || numberOfChar > 20) {
        System.out.print("Please enter a number between 1 and 20: ");
        numberOfChar = input.nextInt();
    }

    char[] letterValue = new char[numberOfChar];
    for (int i = 0; i < numberOfChar; i++) {
        letterValue[i] = (char) (rand.nextInt(26) + 'a');
    }

    System.out.println();
    System.out.println("Welcome My Fellow User to CodeBreaker.");
    System.out.println("You have 6 tries to find the secret 3 letter code.");
    System.out.println("The letters range from A to E.");
    System.out.println("Good luck.");
    System.out.println("The code may have repeated letters. \n");
    System.out.print("SECRET CODE: ");
    System.out.println(letterValue);

}

public boolean done() {
    return done;

}

private boolean isValid(char valid) {
    if (!(valid >= 'a' && valid <= 'z'))
        return true;
    else
        return false;
}

public void getGuess(int guessNum) {
    Scanner input = new Scanner(System.in);

    System.out.print("Enter guess #" + guessNum + ": ");
    guess = input.nextLine().toLowerCase();

    for (int i = 0; i < guess.length(); i++) {
        while (isValid(guess.charAt(i)) || guess.length() != 5) {
            if (guess.equals("quit"))
                break;
            if (guess.length() != numberOfChar)
                System.out.print("Please Enter a string equal to:" + numberOfChar);

            if (isValid(guess.charAt(i)))
                System.out.print("Please enter a Valid Letter (A to Z) or (a to z). ");

            System.out.print("\nEnter guess #" + guessNum + ": ");
            guess = input.nextLine().toLowerCase();
            i = 0;
        }
    }
    if (guessNum == 6 || guess.equals("quit")) {
        System.out.println("You lost the game.");
        done = true;
    }

    checkGuess();
}

public void checkGuess() {
    int counterRight = 0;
    int counterWrong = 0;

    for(int a = 0; a <numberOfChar;a++){
        for(int b= 0; b<numberOfChar; b++){
            if (letterValue[a] == guess.charAt(b) && a == b){
                counterRight++;
            }
            if (letterValue[a] == guess.charAt(b) && a != b){
                counterWrong++;
            }
        }
    }

    {
        if (counterRight == 3) {
            System.out.println("The code is correct! You won the game!");
            done = true;
        }

        else {
            System.out.println(counterRight + " of Right letters in Right places.");
            System.out.println(counterWrong + " of Right letters in Wrong places.");
        }
    }
}

public void display() {
    System.out.print("The code was: ");
    System.out.println(letterValue);
}
}

Main Class

public class Assign4 {

public static void main(String[] args) {
    CodeBreaker result = new CodeBreaker();

    for (int count = 1; count <= 6; count++) {
        result.getGuess(count);

        if (result.done() == true)
            break;

    }

    result.display();
}

}

The error is occuring here:

if (letterValue[a] == guess.charAt(b) && a == b){
            counterRight++;
        }
        if (letterValue[a] == guess.charAt(b) && a != b){
            counterWrong++;




Aucun commentaire:

Enregistrer un commentaire