vendredi 26 février 2016

if-statement and try-catch doesn't prevent error

this part of my code should do the following:

  1. Check if input is a int-type (try-catch)
  2. if input is a int, check if it is in between the list:

Code

try{
     id=sclocal.nextInt();
}catch(InputMismatchException exception)
{
 System.out.println("Invalid input!");
}
id=sclocal.nextInt();
if(listperformer.size() <= id || id < 0)
{
 System.out.println("Invalid input! 294");
 return;
}
else
{
 idlist.add(listperformer.get(id));
}

doesn't work. Problem: 1.

  1. if i put in a wrong ID he asks me to put in another and after this throws an exception
  2. if i put in a string throws an "InputMismatchException"

Aucun commentaire:

Enregistrer un commentaire