有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么我的程序会显示此错误?

我已经看了一个小时了,我不明白为什么它总是告诉我同样的错误。 对于那些想知道我的程序是什么的人来说,基本上它读取一个包含条形码、名称和价格的文本文件。然后,我将阅读另一个只包含条形码的文本文件,并打印其各自的名称和价格。 不幸的是,它只打印一行,然后显示此错误:-

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 99, Size: 99
    at java.util.ArrayList.rangeCheck(ArrayList.java:635)
    at java.util.ArrayList.get(ArrayList.java:411)
    at javaapplication9.JavaApplication9.Search(JavaApplication9.java:66)
    at javaapplication9.JavaApplication9.main(JavaApplication9.java:85)

主要

public class JavaApplication9 
{
    long value1;
    String value2;
    double value3;
    ArrayList<String> toBeSplit = new ArrayList(); 
    String[] split;
    ArrayList <Inventory> productList = new ArrayList<> ();
    ArrayList <Long> barcodes = new ArrayList ();

    public long ReadFile(String sfile) throws IOException 
            {       
                int x = 0;
                File inFile = new File(sfile);
                BufferedReader reader = new BufferedReader(new FileReader(inFile));
                String sline = null;
                while ((sline=reader.readLine()) != null) 
                    {
                        toBeSplit.add(x,sline);
                        x++;
                    }
                reader.close();  
                return inFile.length();
            }

    public void splitString ()
    {   
        int a = 0;
        while (a<toBeSplit.size())
        {
            split = toBeSplit.get(a).split(",");            
            value1 = Long.parseLong(split[0]);
            value2 = split[1];
            value3 = Double.parseDouble(split[2]);
            productList.add(new Inventory (value1,value2,value3,split[0]));
            a++;
        }
    }

     public long readBarcodes (String file) throws IOException 
            {       
                File text = new File(file);
                int x = 0;
                BufferedReader reader = new BufferedReader(new FileReader(text));
                String line = null;
                while ((line=reader.readLine()) != null) 
                    {
                        long barcod = Long.parseLong (line);
                        barcodes.add(x,barcod);
                        x++;
                    }
                reader.close();  
                return text.length();
            }

     public void Search()
     {
         int size = barcodes.size();
         int counter = 0;
         for (Inventory e : productList)
            {
                if ((e.getBarcode() - barcodes.get(counter) == 0) && counter <= size)
                {
                    System.out.println (e.getRBarcode()+ "\t" + e.getName() + "\t"+ e.getPrice());
                }
                else
                {
                    counter++;
                }
            }
     }

    public static void main(String[] args)  
    { 
        try
        {
            JavaApplication9 instance = new JavaApplication9 ();
            instance.ReadFile("Products (1).csv");
            instance.splitString();
            instance.readBarcodes("Items.txt");
            instance.Search();

        }
        catch (IOException e)
        {
            System.out.print ("Error");
        }
    } 
}

存货类别

public class Inventory
{
    long barcode;
    String name,realBar;
    double price;

   public Inventory (long bars,String pname,double prices,String realBarcode)
    {
        barcode = bars;
        name = pname;
        price = prices;
        realBar = realBarcode;   
    }

    public long getBarcode ()
    {
        return barcode;
    }

    public String getName ()
    {
        return name;
    }

    public String getRBarcode()
    {
        return realBar;
    }

    public double getPrice ()
    {
        return price;
    }
}

共 (3) 个答案

  1. # 1 楼答案

    从错误中,我们可以看到您试图访问仅包含99项的Arraylist的索引99。请记住,索引从0开始,因此您只能在“出界”之前到达98(这将提供您在上面看到的IndexOutOfBoundsException

    这条线告诉我们,这条线是罪魁祸首:

    if ((e.getBarcode() - barcodes.get(counter) == 0) && counter <= size)
    

    counter0开始,这很好,但您允许counter到达size,这是99,因为您没有任何限制。一旦counter到达99,您尝试执行barcodes.get(counter),这当然会给您一个IndexOutOfBoundsException,因为99超出了barcodes的范围

    尝试在for循环中添加预防性if语句,然后在计数器过高时将其添加到break

    for (Inventory e : productList)
    {
        if (counter == size)
            break;
        if ((e.getBarcode() - barcodes.get(counter) == 0) && counter <= size)
        {
            System.out.println (e.getRBarcode()+ "\t" + e.getName() + "\t"+ e.getPrice());
        }
        else
        {
            counter++;
        }
    }
    

    另一个解决方案是修改if语句。如果将<=更改为<,然后重新排列语句的求值顺序,则可以防止此异常:

    if (counter < size && (e.getBarcode() - barcodes.get(counter) == 0))
    

    在这种情况下,一旦计数器到达99,它将在counter < size上计算false,并且由于双与符(&&),if语句的后半部分将不会计算,因此它将继续到else语句

    让我知道这是否适合你

  2. # 2 楼答案

    Search方法中,当你说barcodes.size()时,你得到了实际的大小

    现在,counter是基于零的,在你的for循环中,你正在做barcodes.get(counter),实际上一直到barcodes大小+1。而且,counter <= size有点让它达到那个值

    所以,你需要做的就是改变

    if ((e.getBarcode() - barcodes.get(counter) == 0) && counter <= size)

    if (counter < size && (e.getBarcode() - barcodes.get(counter) == 0))

  3. # 3 楼答案

    Search方法中barcodes.size()给出了ArrayList的实际大小。 但是,您的计数器是从0开始的。 因此,尝试改变

    if ((e.getBarcode() - barcodes.get(counter) == 0) && counter <= size)
    

    致:

    if ((counter < size) && (e.getBarcode() - barcodes.get(counter) == 0))