简体   繁体   中英

Get TextField input during while loop instead of JOptionPane java

I'm struggling to get the user input from a Textfield when my program enters a while loop. Previously my program used a JOptionPane but now that i am relying on a button click to sen information to my engine class i get many errors

Here is my engine

public class engine  {
public final Object buttonClickedLock = new Object(); // possible clean up
guess gs = new guess();
public  boolean close = true;
char [] charda = new char[20];
char [] charwo = new char[20];
Highscore words = new Highscore();
main mn = new main();
int guesses =7;
char guess;


public engine() {

}

public void enginer(char guess) { //throws for wait method


    int count = 0;


    String word = words.getWord();


    for(int i = 0; i<word.length(); i++)
    {
        //instantiates two arrays one of dahses and one with the word
        charwo[count] = word.charAt(i);
        charda[count]= '_';
        count++;
    }

    for(int l=0; l<count; l++)
        {
            System.out.print(" "+charda[l]);

        }

    while(guesses !=0 && !Arrays.equals(charwo, charda))
    {
        guess = guess; //This is previously where my JoptionPane went 



        if(word.toUpperCase().contains(String.valueOf(guess).toUpperCase()))
        {


            for(int k = 0; k<word.length(); k++)
            {
                if(String.valueOf(guess).toUpperCase().equals(String.valueOf(charwo[k]).toUpperCase()))
                {
                            charda[k]=charwo[k];

                                    for(int l=0; l<count; l++)
                            {   //prints dashes here to avoid a letter being chopped off by the program stopping in the middle
                                System.out.print(" "+charda[l]);

                            }
                }

              }

        }

        else
        {

                guesses = guesses-1;

                System.out.println("guesses left "+guesses);
                //Re-displays dashes 
                for(int l=0; l<count; l++)
                {
                System.out.print(" "+charda[l]);

                }

        }

           if(Arrays.equals(charwo, charda))
           {
               System.out.println("");
           System.out.println("You are Winner");

                    }
     }

}



}

Any information you can provide me with would be really helpful and if this is too unspecific i will happily post as much more info as you need :)

What was the purpose of JOptionpane in this code? There no relation between JOptionPane and JTextField in the given code. Whether you put a JoptionPane or not the you can always get input from textfield. eg- String strTemp = textfield.getText();// this will give you the text field value.

I think you are passing this getText() value directly to the method enginer() which accepts character. Are you getting invalid argument error?

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