i'm trying to get multiple input from Scanner in Java, the input need to be in a for loop. Single input is working, as i put it in a for, the first cicle work as expected but the second stops at the first input. Before the loop i get an integer, so i've put a scan.nextLine() to get the '\n' character, in the loop i work only with strings.
I tought the problem could be the stream close, but i need it to be open, closing it i get a scanner Exception.
public void scegliEsame(Medico m)
{
Scanner scan=new Scanner(System.in);
String nome, data;
System.out.print("Inserire numero di esami da prenotare: ");
int ex=scan.nextInt();
System.out.println();
scan.nextLine();
for(int i=0;i<ex;i++)
{
System.out.print("Inserire nome esame da voler effettuare: ");
nome=scan.nextLine();
if(m.getCosto(nome)==-1)
{
System.out.println("Esame non trovato");
return;
}
else
costo+=m.getCosto(nome);
System.out.print("Inserire data di prenotazione: ");
data=scan.nextLine();
Esame e=new Esame(nome, m, data,costo);
esami.add(e);
}
}
Code method from class two
public void caricaEsami(Medico m)
{
String esame;
float costo;
System.out.print("Inserire numero di esami totali: ");
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
scan.nextLine();
for(int i=0;i<n;i++)
{
scan=new Scanner(System.in);
System.out.print("Inserire esame "+(i+1)+": ");
esame=scan.nextLine();
System.out.print("Inserire costo esame: ");
costo=scan.nextFloat();
Esame e=new Esame(esame, m, costo);
m.setEsame(e);
scan.nextLine();
}
medici.add(m);
}
I'm sorry for the italian language in my code, hope you could help me.