Hello everyone~ I'm a little new in this theme of programming. I'm here to expose a particular case, of my college project, hoping that can find a way to validate the java code.
Basically, I'm designing a system to sum the daily sales of a company and save them into a matrix. The code below corresponds to the first day only.
package fsystem;
import java.util.ArrayList;
import java.util.Scanner;
public class class_Sist {
public class_Sist() {
}
ArrayList <Integer> MondayPrices = new ArrayList();
int Week[][]= new int [2][7];
public void addPricesDay1(){
Scanner scn = new Scanner(System.in);
String answer;
Integer auxAddition=0;
boolean flagNext= false, flagAgain= false;
System.out.println("Next it is come to apply the amounts of sales made on the day "+Week[0][0]+". Please "
+ "indicate the price of each of the items that were sold.");
do {
System.out.println("Enter the price of the corresponding article.");
MondayPrices.add(scn.nextInt());
do {
System.out.println("It requires enter the price of another article?");
answer= scn.next();
if (("Yes".equals(answer))||("yes".equals(answer))) {
flagNext=false;
flagAgain=true;
}
if (("No".equals(answer))||("no".equals(answer))){
flagNext=false;
flagAgain=false;
System.out.println("Introduced sales prices have been stored successfully.");
}
if ((!"Yes".equals(answer))&&(!"yes".equals(answer))&&(!"No".equals(answer))&&(!"no".equals(answer))) {
System.out.println("Error. Please respond using by answer only yes or no.");
flagNext=true;
}
} while (flagNext==true);
} while (flagAgain==true);
for (int i=0; i<MondayPrices.size(); i++) {
auxAddition= auxAddition+MondayPrices.get(i);
}
System.out.println("The total amount in sales for monday is "+auxAddition);
Week[1][0]=auxAddition;
}
}
So, what I need is to validate that the data inputted by the user be only numeric, and never otherwise, but I don't know completely how ArrayList
works, therefore, I would greatly appreciate if someone could explain me how I can do that.