验证ArrayList java中的数据类型

验证ArrayList java中的数据类型,java,validation,netbeans,arraylist,Java,Validation,Netbeans,Arraylist,大家好~我对编程这个主题有点陌生。我在这里公开我大学项目的一个特殊案例,希望能找到一种验证java代码的方法 基本上,我正在设计一个系统来汇总公司的每日销售额,并将其保存到矩阵中。以下代码仅与第一天对应 package fsystem; import java.util.ArrayList; import java.util.Scanner; public class class_Sist { public class_Sist() { } ArrayList <Int

大家好~我对编程这个主题有点陌生。我在这里公开我大学项目的一个特殊案例,希望能找到一种验证java代码的方法

基本上,我正在设计一个系统来汇总公司的每日销售额,并将其保存到矩阵中。以下代码仅与第一天对应

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;
   }
}

因此,我需要验证的是,用户输入的数据只能是数字,而不能是其他形式,但我不完全知道ArrayList是如何工作的,因此,如果有人能解释一下我是如何做到这一点的,我将不胜感激。

在ArrayList中添加整数之前,您可以验证输入

我希望这将有助于-

使用Scanner hasnetint功能。 如果可以使用nextInt方法将此扫描仪输入中的下一个标记解释为指定基数中的int值,则返回true。扫描仪不会超过任何输入

与while一起使用,检查输入是否为整数

Scanner stdin = new Scanner(System.in);   
while (!stdin.hasNextInt()) stdin.next();
MondayPrice.add(stdin.nextInt());

您可以尝试类似于System.out.Printl的方法输入相应商品的价格。;tmp=scn.nextLine;试试{price=Integer.parseInttmp;MondayPrices.addprice;}捕获异常e{System.out.println必须是整数;}@tim谢谢你的评论,我试过了,成功了。事实上,这是一个我从未尝试过的函数,我不知道如何将其调整为一个循环,以便在用户输入错误字符时再次询问价格,也不知道如何将其保存在ArrayListPologies中,因为我不知道,但我不知道如何将这种方式调整为arraylist。它工作得非常完美。特别是对于这个案例,我需要一个验证,就像我在上面代码中所做的一样,但即使如此,你的验证还是非常有用的,还有链接。多谢各位~