Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

Trying to validate characters in string - Java

hoping I can get some help. I'm new here and a student developer.

I'm trying to validate a string to ensure it has only characters included in another string.

There is a string of random generated characters (12 in length) and the user inputs a word but must only use characters from the string.

I also need to repeat the user input until there is a valid input. so a loop is necessary. I have tried numerous approaches but for some reason can't get it right

The user input will be within the main class which then calls a method from another class.

My logic is to check if the characters from the user input do not match the characters in the randomLetters String then count. and while that value is greater than 0 then there is an invalid entry so the use should try again. however, what actually is happening is: no matter what the user inputs the code still continues to show that it is incorrect

Here is my code:

import java.lang.Math;
public class TestValidChar{


    private String player1Guess;
    private final String ALPHABET;
    private String randomLetters;
    private StringBuffer strBuff;
    private String testValid;
    private int validCounter;

    public TestValidChar(){

        player1Guess=" ";
        player2Guess=" ";
        ALPHABET = "abcdefghijklmnopqrstuvwxyz";
        testValid = " ";
        validCounter=0;
    }

    public void setPlayer1Guess(String player1Guess){
        this.player1Guess=player1Guess.toLowerCase();
    }
    public void setPlayer2Guess(String player2Guess){
        this.player2Guess=player2Guess.toLowerCase();
    }

    public void setRandomLetters(String randomLetters){
        randomLetters=" ";
        strBuff = new StringBuffer();
            //for loop to create a string of 12 random letters -
            for (int i = 0; i < 12; i++) {
            // generate a random number between
            // 0 to 25 (instead of 1-26 for lenght of alphabet)
            int index = (int)(ALPHABET.length()* Math.random());
            // add Character one by one to strBuff
            strBuff.append(ALPHABET.charAt(index));
            }//end for loop
        randomLetters = strBuff.toString();

        this.randomLetters=randomLetters;
    }



    public void compute(){
    validCounter=0;
        for(int i=0; i<player1Guess.length(); i++){
            for(int j=0; j<randomLetters.length(); j++){
                if(player1Guess.charAt(i)!=randomLetters.charAt(j)){
                    validCounter++;
                }
            }
        }
    }


    public String getRandomLetters(){
        return randomLetters;
        }

    public int getValidCounter(){
        return validCounter;
        }
}

and the app class:

import javax.swing.*;
public class TestApp{
    public static void main(String[]args){

    //data members
    String player1Guess;
    String player2Guess;
    final String ALPHABET;
    String randomLetters=" ";
    int validCounter;


    TestValidChar myTest = new TestValidChar();
    myTest.setRandomLetters(randomLetters);
    randomLetters=myTest.getRandomLetters();

    player1Guess=JOptionPane.showInputDialog(null, " Enter word using these letters: " +randomLetters);
    myTest.setPlayer1Guess(player1Guess);

    myTest.compute();
    validCounter=myTest.getValidCounter();


    while(validCounter>0){

        player1Guess=JOptionPane.showInputDialog(null, " Invalid Characters used. Use only " +randomLetters+ " " +validCounter);
        myTest.compute();

        validCounter=myTest.getValidCounter();

        }


    }


}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The core issue lies in the compute method

public void compute(){
    validCounter=0;
        for(int i=0; i<player1Guess.length(); i++){
            for(int j=0; j<randomLetters.length(); j++){
                if(player1Guess.charAt(i)!=randomLetters.charAt(j)){
                    validCounter++;
                }
            }
        }
}

First of all it is ambiguous, there is a variable named validCounter but it seems you increment it when 2 characters do not match. It is very important for everyone involved in your code, especially you, of course, to provide meaningful names.

Try to watch step by step why this counter will not have the value you expect. Take for example "word" as a player1Guess and "drow" as randomLetters and iterate through the loops to see why it will increment a number of times, although you expect it not to and then update your logic accordingly.

If you want "validCounter" to increment only when a letter is not found in the "randomLetters" you could rename it to "invalidCounter" and try the following:

public void compute() {
    invalidCounter = 0;
    for (int i = 0; i < player1Guess.length(); i++) {
        if (randomLetters.indexOf(player1Guess.charAt(i)) == -1) {
            // indexOf returns -1 if char is not found
            invalidCounter++;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...