How to compare a jtextfiel with a string

0

I must compare a data entered by the jtextfiel with the data that is a file txt and when comparing it it says:

  

Incompatible operand types string and JTextField

The data I want to compare is the username and password:

public int leerficheroo(){
String fichero = "C:/Usuario.txt";
try {
  FileReader fr = new FileReader(fichero);
  BufferedReader br = new BufferedReader(fr);

  String linea;
  while((linea = br.readLine()) != null) {
      String[] lineaArreglo = linea.split(";");
      String usuario=lineaArreglo[1];
      String contrasena=lineaArreglo[2];

      if(usuario.equals(ventana1.user) && contrasena.equals(ventana1.pass)) 
      {
          return 1;
      }
      else
          return 2;

  }

the error comes up in the (if) when asking if the data of the txt is equal to the one of the jtextfiel that is in another class in which it tries to change the type of data so

public static JTextField txtUsuario;
public static JPasswordField passUsuario;
static String user = txtUsuario.getText();
static String pass = passUsuario.getText();
    
asked by RkuDkd Fix 17.07.2018 в 19:20
source

2 answers

0

With your portion of code I have tried to put together the same workbenck, only that the user data and password are taken from the same class.

Consider:

  • String usuario=lineaArreglo[1]; String contrasena=lineaArreglo[2];

    is going to give you a

      

    ArrayIndexOutOfBoundsException

  • since the array starts at the '0' position

  • The leerFicheroo() function will have to return some default value.

  • With respect to the window class, be sure to have the object and the user's values initialized and contresena.

  • Greetings.

    Note : If you want to upload the rest of the classes, you can see it in more detail.

        
    answered by 17.07.2018 в 21:07
    0

    Assuming that user and pass are the jTextField with which you want to compare, you only need to add the getText (). Instead of this:

    if(usuario.equals(ventana1.user) && contrasena.equals(ventana1.pass))

    Use this:

    if(usuario.equals(ventana1.user.getText()) && contrasena.equals(ventana1.pass.getText()))

    I hope to be of help, greetings!

        
    answered by 17.07.2018 в 21:23