简体   繁体   中英

Strange behavior in Java with recursivity

I am learning Java. This is my simple java test.

import java.io.Console;
import java.util.regex.Pattern;


public class Pendu {

    private static final int MAX_ERRORS_ALLOWED = 10;
    private static final int MAX_WORDL_LENGHT = 10;

    public static void main(String[] args) {
        String motSaisi = Pendu.readMot();
        System.out.println("Le mot saisi est "+motSaisi);
    }

    public static boolean hasSpecialChar (String s) {
        Pattern p = Pattern.compile("[^a-zA-Z]");
        return (p.matcher(s).find());
    }

    public static String readMot(){
        char[] ChaineSaisi = System.console().readPassword("Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentuées: ");
        String text = String.copyValueOf(ChaineSaisi);
        System.out.println("mot a testé "+text);

        if(Pendu.hasSpecialChar(text)) {
            Pendu.readMot();
        }
        System.out.println("mot correct "+text);
        return text;
    }
}

The goal of this is to enter a word witch only characters ( aZ AZ) and only this.When i run this with example like this :

 Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? )))
    Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? """
    Joueur 1 : Veuillez saisir un mot sans chiffres ni lettres accentu?es: 
    mot a test? po
    mot correct po
    mot correct """
    mot correct )))
   Le mot saisi est )))

")))" ( This is not the last word i have tpaed, the last is "po"). Why this happen ?

This is part of the recursion. When you first make a recursive call, it will make the current execution process as "pending" so when all the recursion is finished (when there's a special char), it checks the last word typed, then the (n-1)th and so on until it check your very first word.

Anyway, if you just want to check the validity, this is a terrible way of doing it, since you stack your execution pile at each input. Not only it is ugly, but it can create an OutOfMemory exception

Anyway, here's something that should work:

while (!StringUtils.isAlpha((input = readString())));

StringUtils.isAlpha(myString) returns true is the string is only made of ascii letters. I suppose that readString() is your read function. Here the program will ask you for an input until it is correct.

Your local variables ChaineSaisi and text are local to each call of readMot . That is, every time readMot is called, and these variables are initialised, you're actually getting new instances of these variables.

To do what you want, you'll need to set the text value to whatever is returned by the inner call to readMot . If you change the line inside the if branch to

text = Pendu.readMot();

then your program should do what you want, because you will then be setting each text variable to the value from the later call to readMot .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM