Ovviamente memorizza l'Utente, e dovrebbe avere come minimo i metodi getter/setter. Scherzi? 8 classi? Mah, la stai sparando grossa secondo me... :asd:
Ad ogni modo ho usato il seguente file:
Il metodo che ho scritto è:codice:Nome1;Cognome1;1;1; Nome2;Cognome2;2;2; Nome3;Cognome3;3;3; Nome4;Cognome4;4;4; Nome5;Cognome5;5;5;
si può svolgere in diversi modi... usando Scanner, StringTokenizer oppure scorrendo la stringa.codice:Utente getUtente(String name, String pwd) throws IOException { Scanner parser = new Scanner(new File(file)); parser.useDelimiter("\r\n"); while(parser.hasNext()) { String s = parser.next(); String[] splittedText = s.split(";"); // [0] = Nome // [2] = Ped if(splittedText[0].equals(name) && splittedText[2].equals(pwd)) return new Utente(name, splittedText[1], pwd, Integer.parseInt(splittedText[3])); } return null; }
Se vuoi provarlo ecco il codice completo:
codice:import java.util.Scanner; import java.io.*; class Utente { private String name, surname, pwd; private int age; Utente(String name, String surname, String pwd, int age) { this.name = name; this.surname = surname; this.pwd = pwd; this.age = age; } String getName() { return name; } String getSurname() { return surname; } int getAge() { return age; } String getPwd() { return pwd; } public String toString() { return name+"\n"+surname+"\n"+pwd+"\n"+age; } } class SplitString { private String file; SplitString(String file) { this.file = file; } Utente getUtente(String name, String pwd) throws IOException { Scanner parser = new Scanner(new File(file)); parser.useDelimiter("\r\n"); while(parser.hasNext()) { String s = parser.next(); String[] splittedText = s.split(";"); // [0] = Nome // [2] = Ped if(splittedText[0].equals(name) && splittedText[2].equals(pwd)) return new Utente(name, splittedText[1], pwd, Integer.parseInt(splittedText[3])); } return null; } } class Example { public static void main(String[] args) throws IOException { SplitString ss = new SplitString("utente.txt"); System.out.println((ss.getUtente("Nome4","4")).toString()); } }

Rispondi quotando