Java中的数组索引越界

Java中的数组索引越界,java,Java,通过这个程序,我阅读并比较文本文件中给出的数字,并在数字连续三次上升时打印“买入”,在数字连续三次下降时打印“卖出” 程序运行得很好,但最后我得到了 java.lang.ArrayIndexOutOfBoundsException 我想要的只是一种获得无限数量作为参数的方法,而不必每次读取txt文件时都更改数组编号([15]) public class Practise1 { public static void main(String [] args) throw

通过这个程序,我阅读并比较文本文件中给出的数字,并在数字连续三次上升时打印“买入”,在数字连续三次下降时打印“卖出”

程序运行得很好,但最后我得到了
java.lang.ArrayIndexOutOfBoundsException

我想要的只是一种获得无限数量作为参数的方法,而不必每次读取txt文件时都更改数组编号([15])

public class Practise1
{         
  public static void main(String [] args)
    throws IOException 
  {
    int num=0;
    String choice;
    int up=0;
    int down=0;
    int same=0;
    FileInputStream w = new FileInputStream("numbers.txt");
    Scanner scanner  = new Scanner(w);
    while(scanner.hasNextDouble())
    {
      Double [] con= new Double [15];
      for (int i=0; i<con.length; i++)
      {
        con[i]=scanner.nextDouble();
      }
      for (int a=0; a<con.length&&a<con.length; a++)
      {
        num++;
        System.out.print(num+"  "+(con[a]));
        if(con[a]<con[a+1])
        {
           up++;
        }
        else if(con[a]>con[a+1])
        {    
          down++;
        }
        else
        {
          same++;
        }
         if ((up >= 3 && (down > 1 || same >= 1)))
        {  
          System.out.print("  "+"sell");
          up=0;
          same=0;
        }
        else if ((down >= 3 && (up > 1 || same >= 1)))
        {
          System.out.print("  "+"buy");
          down=0;
          same=0;
        } 
        System.out.println();
      }
    }
    scanner.close();
  }
}

我认为你的问题在于:

  for (int a=0; a<con.length&&a<con.length; a++)
  {
    num++;
    System.out.print(num+"  "+(con[a]));
    if(con[a]<con[a+1]) { 
       etc.
for(int a=0;a故障线路与此线路相似:

if(con[a]<con[a+1])
试试这个:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class ArrayIndexOutOfBoundException
{         
  public static void main(String [] args)
    throws IOException 
  {
    int num=0;
    int up=0;
    int down=0;
    int same=0;
    FileInputStream w = new FileInputStream("numbers.txt");
    Scanner scanner  = new Scanner(w);
    while(scanner.hasNextDouble())
    {
      Double [] con= new Double [15];
      for (int i=0; i<con.length; i++)
      {
        con[i]=scanner.nextDouble();
      }
      for (int a=0; a<con.length-1; a++)
      {
        num++;
        System.out.print(num+"  "+(con[a]));
        if(con[a]<con[a+1])
        {
           up++;
        }
        else if(con[a]>con[a+1])
        {    
          down++;
        }
        else
        {
          same++;
        }
        if ((up >= 3 && (down > 1 || same >= 1)))
        {  
          System.out.print("  "+"sell");
          up=0;
          same=0;
        }
        else if ((down >= 3 && (up > 1 || same >= 1)))
        {
          System.out.print("  "+"buy");
          down=0;
          same=0;
        } 
        System.out.println();
        if (a == con.length -2) {
            num++;
            System.out.print(num+"  "+(con[a]));
              if(con[con.length-2]<con[con.length-1])
              {
                 up++;
              }
              else if(con[con.length-2]>con[con.length-1])
              {    
                down++;
              }
              else
              {
                same++;
              }
              if ((up >= 3 && (down > 1 || same >= 1)))
              {  
                System.out.print("  "+"sell");
                up=0;
                same=0;
              }
              else if ((down >= 3 && (up > 1 || same >= 1)))
              {
                System.out.print("  "+"buy");
                down=0;
                same=0;
              } 
              System.out.println();
        }

      }
    }
    scanner.close();
  }
}
import java.io.FileInputStream;
导入java.io.IOException;
导入java.util.Scanner;
公共类ArrayIndexOutOfBoundException
{         
公共静态void main(字符串[]args)
抛出IOException
{
int num=0;
int up=0;
int-down=0;
int-same=0;
FileInputStream w=新的FileInputStream(“numbers.txt”);
扫描仪=新扫描仪(w);
while(scanner.hasNextDouble())
{
Double[]con=新的Double[15];
对于(int i=0;i 1 | |相同>=1)))
{  
系统输出打印(“+”销售”);
up=0;
相同=0;
}
否则如果((向下>=3&(向上>=1 | |相同>=1)))
{
系统输出打印(“+”购买“);
向下=0;
相同=0;
} 
System.out.println();
如果(a==con.length-2){
num++;
System.out.print(num++(con[a]);
if(con[con.length-2]con[con.length-1])
{    
down++;
}
其他的
{
相同的++;
}
如果((向上>=3&(向下>1 | |相同>=1)))
{  
系统输出打印(“+”销售”);
up=0;
相同=0;
}
否则如果((向下>=3&(向上>=1 | |相同>=1)))
{
系统输出打印(“+”购买“);
向下=0;
相同=0;
} 
System.out.println();
}
}
}
scanner.close();
}
}

如果您不想设置数组的大小,也可以使用ArrayList来设置。

检查
java.util.ArrayList的JavaDoc
aI尝试了,仍然是相同的错误。无需检查相同的条件两次:
aI尝试了,仍然是相同的错误。@JohnTrib尝试添加
System.out.println(a)
到您的循环中,查看您正在访问的索引。它实际上起了作用,但现在的问题是我的程序只打印14行(因为我们使用的是长度1)。因此,现在我没有得到错误,但我只打印了14行,而不是15行。这是因为程序的逻辑涉及检查下一个数字。最后一个条目没有下一个数字,因此无法打印。否则,添加一个额外的print语句来简单地打印最后一个数字1)Java由于某种原因没有读取列表。2)您只提供了y读取txt文件的15行中的14行。我不知道你想要实现什么。.用懒惰的方式更新了解决方案!!程序运行正常,谢谢!
for (int a = 0; a < con.length; a++)
for (int a = 0; a < con.length - 1; a++)
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

public class ArrayIndexOutOfBoundException
{         
  public static void main(String [] args)
    throws IOException 
  {
    int num=0;
    int up=0;
    int down=0;
    int same=0;
    FileInputStream w = new FileInputStream("numbers.txt");
    Scanner scanner  = new Scanner(w);
    while(scanner.hasNextDouble())
    {
      Double [] con= new Double [15];
      for (int i=0; i<con.length; i++)
      {
        con[i]=scanner.nextDouble();
      }
      for (int a=0; a<con.length-1; a++)
      {
        num++;
        System.out.print(num+"  "+(con[a]));
        if(con[a]<con[a+1])
        {
           up++;
        }
        else if(con[a]>con[a+1])
        {    
          down++;
        }
        else
        {
          same++;
        }
        if ((up >= 3 && (down > 1 || same >= 1)))
        {  
          System.out.print("  "+"sell");
          up=0;
          same=0;
        }
        else if ((down >= 3 && (up > 1 || same >= 1)))
        {
          System.out.print("  "+"buy");
          down=0;
          same=0;
        } 
        System.out.println();
        if (a == con.length -2) {
            num++;
            System.out.print(num+"  "+(con[a]));
              if(con[con.length-2]<con[con.length-1])
              {
                 up++;
              }
              else if(con[con.length-2]>con[con.length-1])
              {    
                down++;
              }
              else
              {
                same++;
              }
              if ((up >= 3 && (down > 1 || same >= 1)))
              {  
                System.out.print("  "+"sell");
                up=0;
                same=0;
              }
              else if ((down >= 3 && (up > 1 || same >= 1)))
              {
                System.out.print("  "+"buy");
                down=0;
                same=0;
              } 
              System.out.println();
        }

      }
    }
    scanner.close();
  }
}