Good afternoon, I'm having a hard time reading and writing data in an arraylist. The data from my TXT file:
Frames 12345
Rafaela 54321
Kaique 12345
Matheus 54321
Here is 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 execute the commands: System.out.println ("Login:" + grava.getListMaterial (). Get (i) .getLogin ()); System.out.println ("Password:" + grava.getListMaterial (). Get (i) .getSenha () + "Faeces" + cont);
It returns me in my getLogin (): Kaique 12345 It returns me in my getSenha (): Matheus 54321
But it was for him to get me back in getLogin (): Kaique But it was for it 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)