Gli scanner leggono solo il primo elemento

This commit is contained in:
Orange_Dugongo 2016-10-21 13:42:23 +02:00
parent dcf8057269
commit 3f0a55026c
7 changed files with 30 additions and 23 deletions

Binary file not shown.

View File

@ -12,21 +12,29 @@ public class Esame{
return voto; return voto;
} }
public Esame lookUp(int mat){ public int getMat(){
return mat;
}
public String getNome(){
return nome;
}
public boolean lookUp(int mat){
if(this.mat==mat) if(this.mat==mat)
return this; return true;
return null; return false;
} }
public static Esame read(Scanner sc) throws Exception{ public static Esame read(Scanner sc) throws Exception{
String nome; String nome;
int voto; int voto;
int mat; int mat;
if(sc.hasNext()){ if(sc.hasNextLine()){
nome=sc.nextLine(); nome=sc.nextLine();
if(sc.hasNext()){ if(sc.hasNextInt()){
voto=sc.nextInt(); voto=sc.nextInt();
if(sc.hasNext()){ if(sc.hasNextInt()){
mat=sc.nextInt(); mat=sc.nextInt();
return new Esame(nome, voto, mat); return new Esame(nome, voto, mat);
} }

View File

@ -1,7 +1,4 @@
Chimica Chimica
30
1212
Chimica
18 18
1313 1313
Economia Economia
@ -10,3 +7,6 @@ Economia
Economia Economia
30 30
1414 1414
Chimica
30
1212

Binary file not shown.

View File

@ -9,7 +9,6 @@ public class Main{
Esame e; Esame e;
Studente s; Studente s;
int i=0; int i=0;
boolean trovato = false;
double media=0; double media=0;
String nome, cnome; String nome, cnome;
@ -20,20 +19,20 @@ public class Main{
sc=new Scanner(srcStudenti); sc=new Scanner(srcStudenti);
s=Studente.read(sc); s=Studente.read(sc);
while(s!=null && !trovato){ while(s!=null){
s=s.lookUp(nome, cnome); if(s.lookUp(nome, cnome)){
if(s!=null){
trovato=true;
sc=new Scanner(srcEsami); sc=new Scanner(srcEsami);
e=Esame.read(sc); e=Esame.read(sc);
while(e!=null){ while(e!=null){
e=e.lookUp(s.getMat()); System.out.println(e.getVoto()+" "+e.getMat()+" "+e.getNome());
if(e!=null){ if(e.lookUp(s.getMat())){
System.out.println("ok "+e.getVoto());
media+=e.getVoto(); media+=e.getVoto();
i++; i++;
} }
e=Esame.read(sc); e=Esame.read(sc);
} }
break;
} }
s=Studente.read(sc); s=Studente.read(sc);
} }

Binary file not shown.

View File

@ -16,21 +16,21 @@ public class Studente{
return nome.concat(" ").concat(cnome); return nome.concat(" ").concat(cnome);
} }
public Studente lookUp(String nome, String cnome){ public boolean lookUp(String nome, String cnome){
if(this.nome.equals(nome) && this.cnome.equals(cnome)) if(this.nome.equals(nome) && this.cnome.equals(cnome))
return this; return true;
return null; return false;
} }
public static Studente read(Scanner sc) throws Exception{ public static Studente read(Scanner sc) throws Exception{
String nome; String nome;
String cnome; String cnome;
int mat; int mat;
if(sc.hasNext()){ if(sc.hasNextLine()){
nome=sc.nextLine(); nome=sc.nextLine();
if(sc.hasNext()){ if(sc.hasNextLine()){
cnome=sc.nextLine(); cnome=sc.nextLine();
if(sc.hasNext()){ if(sc.hasNextInt()){
mat=sc.nextInt(); mat=sc.nextInt();
return new Studente(nome, cnome, mat); return new Studente(nome, cnome, mat);
} }