0

With this program i read and compare the numbers that I'm given in a text file and print out "buy" whenever the number goes up three consecutive time and "sell" whenever the number goes down three consecutive times.

The program runs perfectly but at the end i get
java.lang.ArrayIndexOutOfBoundsException

All i want is a way to get unlimited numbers as an argument and dont have to change the array number([15]) every time i want to read a txt file.

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

The result i get :

1  26.375
2  25.5
3  25.125
4  25.0
5  25.25  buy
6  27.125
7  28.25
8  26.0  sell
9  25.5
10  25.0
11  25.125  buy
12  25.25
13  26.375
14  25.5  sell
15  25.5

java.lang.ArrayIndexOutOfBoundsException: 15
at Practise1.main(Practise1.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
John Trib
  • 5
  • 4

3 Answers3

4

I think your problem is here:

  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.

When you iterate through the array, you are comparing each item with the next item in your list. When you get to the 15th item (a=14 and con[14]), there is no con[15] to compare it to. You should iterate to one before the end of the array.

TangledUpInBlue
  • 746
  • 4
  • 13
3

The faulty line(s) is/are similar to this one:

if(con[a]<con[a+1])

Your loop goes from 0 to the max index of the array.

for (int a = 0; a < con.length; a++)

If you try to access con[a+1] you will access con[15], which is out of bounds.

Change your for loop to

for (int a = 0; a < con.length - 1; a++)
Arc676
  • 4,445
  • 3
  • 28
  • 44
  • I tried that, still the same error. – John Trib Jan 05 '16 at 13:58
  • @JohnTrib Try adding `System.out.println(a)` to your loop to see what indices you are accessing. – Arc676 Jan 05 '16 at 14:02
  • It actually worked but the problem now is that my program prints only 14 lines (since we did length-1).So now i dont get the error but i print only 14 lines instead of the 15. – John Trib Jan 05 '16 at 14:26
  • That's because the logic of your program involves checking the next number. The last entry has no next number so it can't be printed. Otherwise, add an extra print statement to simply print the last number – Arc676 Jan 06 '16 at 08:16
1

Try this:

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

And also use ArrayList to if you don't want to set the size of the Array.

Ravindra Devadiga
  • 692
  • 1
  • 6
  • 14