Problem removing data from an Arraylist

Asked

Viewed 84 times

-2

Good afternoon, I am having a certain difficulty in reading and writing the data in an arraylist. The data from my TXT file:

Mark 12345

Rafaela 54321

Kaique 12345

Matheus 54321

Just follow my code:

if( evento.getSource( ) == bENTRAR )
  {
     openFile();
     //Gravando records = new Gravando();
     int i = 0;
     int cont = 0;
     while( i <= grava.getListaMateria().size()-1)
     {
        System.out.println("Login:"+grava.getListaMateria().get(i).getLogin());
        System.out.println("Senha:"+grava.getListaMateria().get(i).getSenha()+"Fezes"+cont);
        if(grava.getListaMateria().get(i).getLogin().equals(l) && grava.getListaMateria().get(i).getSenha().equals(s))
           {
              JOptionPane.showMessageDialog(null,"Login feito com sucesso","Bem vindo",1);
              dispose();
              MenuGUI oples = new MenuGUI();
           }

       i++;
       cont++;
       } 
      JOptionPane.showMessageDialog(null,"Login ou senha incorretos","Desculpe",1);


  }
}

 public void openFile()
 {
  l = tLogin.getText();
  s = Senha.getText();
  try
  {
     input = new Scanner(new File( "login.txt" ));
  } // end try
  catch( FileNotFoundException fileNotFoundException )
  {
     System.err.println( "Error opening file." );
     System.exit( 1 );
  } // end catch


  ArrayList<Gravando> lp = new ArrayList<Gravando>();
  try // read records from file using Scanner object
  {
     while(input.hasNext())
     {
        //Scanner scanner = new Scanner(new File("login.txt"));
        input.useDelimiter(" ");

        grava.setLogin(input.nextLine() /*input.next()*/ ); // read account number
        grava.setSenha(input.nextLine()/*input.next()*/ ); // read first name

        lp.add(grava); 
     } // end try

  }
  catch ( NoSuchElementException elementException )
  {
     System.err.println( "File improperly formed." );
     input.close();
     System.exit( 1 );
  } // end catch
  catch ( IllegalStateException stateException )
  {
     System.err.println( "Error reading from file." );
     System.exit( 1 );
  } // end catch

  grava.setListaMaterias(lp);
  input.close();
 } // end method openFile

The problem is when I perform the commands: System.out.println("Login:"+grava.getListaMateria().get(i).getLogin()); System.out.println("Password:"+grava.getListaMateria(). get(i). getSenha()+"Feces"+cont);

He returns me in my getLogin():Kaique 12345 He returns me in my getSenha(): Matheus 54321

But it was for him returns me in getLogin(): Kaique But it was for it returns me in getSenha(): 12345

I wonder what I’m doing wrong.

   quando tento compilar o metodo que voce me falou da a seguinte mensagem.

    LoginGUI.java:350: error: no suitable method found for add(String[])
        lp.add(linhaArquivo.split(" ")); 
          ^
method Collection.add(String) is not applicable
  (argument mismatch; String[] cannot be converted to String)
method List.add(String) is not applicable
  (argument mismatch; String[] cannot be converted to String)
method AbstractCollection.add(String) is not applicable
  (argument mismatch; String[] cannot be converted to String)
method AbstractList.add(String) is not applicable
  (argument mismatch; String[] cannot be converted to String)
method ArrayList.add(String) is not applicable
  (argument mismatch; String[] cannot be converted to String)

1 answer

0


The method nextLine() is returned all line, from your file, at once. I believe that if you use the split() can solve your problem, once this method can split a string, in the patterns you define, and put each part in a String array.

For example:

String linhaArquivo = input.nextLine();
String []valores = new String[2];
valores = linhaArquivo.split(" ");

The code above will cut the String to each blank space, that is, inside the array values, it will be:

valores[0] = "Kaique";
valores[1] = "12345";
  • So @Marcus Vinicius, and that real problem is that I don’t know how much data I’ll have in my TXT file. So I would need to use an arraylist to be able to store my data.

  • @Marcosleme You can store the data however you want, split() would only be a way for you to put the right values in the login and password. In the part where you do setLogin and setSelf, implements the method and would look something like setLogin(values[0]); and setSenha(values[1]);

  • Well you could edit the answer by exemplifying how to use the split in an element of the arraylist.

  • @diegofm, I edited the code, but it’s very simple, the input.nextLine() returns each line of the file, because it is inside a while(input.hasNext()), just save the return in a String and use the split. I don’t know if this is what you wanted to do with using "element of arraylist".

  • @Marcusvinicius when I try to compile the method Voce told me of the following message.

  • lp.add(filename.split(")); the split() method returns a string array, you won’t be able to add this to your lp array. You have to continue the same way you were doing before, populating the "record" with what you pull from the file.

  • @Marcusvinicius add a Strings vector in an Arraylist that only accepts Strings?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.