int selection;
selection = 0;
while (selection != -1)
{
selection = menu();
switch (selection)
{
case 1:
System.out.println("Hur många varor vill du lägga till?");
int numberOfArticles = input();
System.out.print("Du har lagt till: " + numberOfArticles + " varor\n");
articles = checkFull(articles, numberOfArticles);
articleNumber = insertArticles(articles, articleNumber, numberOfArticles);
break;
case 2:
System.out.println("Vilken artikel vill du ta bort?");
removeArticle(articles);
break;
case 3:
System.out.println("Visa artiklar");
printArticles(articles);
break;
case 4:
System.out.println("Vilken artikel vill du sälja? Hur stort antal vill du sälja?");
sellArticle(sales, salesDate, articles);
break;
case 5:
System.out.println("Orderhistorik");
printSales(sales, salesDate);
break;
case 6:
System.out.println("Avsluta");
System.exit(0);
break;
default:
System.out.print("Felaktig inmatning!\n");
break;
When the user gives a string value then the programs says Wrong value then the program just stops. But when I select a number like 22 or something, the program says Wrong value and the loop continues and gives the user another chance to try. But when it comes to a string input it just stops and don't give the user another chance.
To end the loop selection should equal to -1. I think changing the default case like below will solve the problem.
default:
System.out.print("Felaktig inmatning!\n");
selection = -1;
break;
Then it will exit the loop when the user gives an incorrect value.
public static int input()
{
int input;
input = 0;
while (input == 0)
{
if (keyboard.hasNextInt())
{
input = keyboard.nextInt();
break;
} else
{
input = -1;
break;
}
}
return input;
}