arnelyn
arnelyn

Reputation: 3

How to check the values inputted in the JTextField and transfer the string input to an array of characters?

I want to compare every digits of the input number. I am trying to make a DFA program and my DFA accepts strings that does not have any consecutive symbol from 0 and 1.

here's the part of my class that I'm having a trouble with.. I just want to somehow parse and compare each characters/integers...

class check implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        JTextField inp= (JTextField) event.getSource();
        char[] text=inp.toCharArray();
        char[] result=new char[text.length];
        int ctr=0;

        while (ctr<result.length)
        {
            if(passy[ctr]==passy[ctr+1])
            {
            }

            else
            {
                JOptionPane.showMessageDialog(null, "The String is NOT ACCEPTED!");
            }

            ctr++;
        }
    }
}

I'm having an error in this part:

char[] text=inp.toCharArray();

Upvotes: 0

Views: 239

Answers (1)

Reimeus
Reimeus

Reputation: 159844

The method toCharArray is undefined for JTextField. You need to get the text from the component first. Replace

char[] text = inp.toCharArray();

with

char[] text = inp.getText().toCharArray();

Upvotes: 3

Related Questions