简体   繁体   中英

Finding 3 equal consecutive numbers in two different Arrays. Java

I am writing a program that reads a CSV file and stores each column into a separate array. later I want to search arrays A and B and make for 3 consecutive 0s and then 2 ones following them. An example of the console after I run the program is;

                                            A   B
Line # 1    1.3167  1.3164  1.318   1.3174  0   0
Line # 2    1.3167  1.3164  1.318   1.3174  0   0
Line # 3    1.3175  1.3164  1.3182  1.3169  0   0
Line # 4    1.3168  1.3167  1.3225  1.3212  1   1
Line # 5    1.3213  1.3206  1.3221  1.321   1   0
Line # 6    1.3211  1.3208  1.3241  1.3239  1   1
Line # 7    1.324   1.3237  1.3262  1.3242  1   1
Line # 8    1.3243  1.3234  1.3271  1.3245  0   1
Line # 9    1.3244  1.3223  1.3251  1.324   0   0
Line # 10   1.3241  1.3226  1.3269  1.3269  1   1

There are many more lines, however I want the program to find and a message to me every time the pattern from line 1-4 occur.

My code is

package pacakge;
import java.io.*;
import java.util.Scanner;

public class read {
  public static void main(String[] args) throws FileNotFoundException  {
    int size=0;
    double[] open = new double[1000000];
    double[] low = new double[1000000];
    double[] high = new double[1000000];
    double[] close = new double[1000000];
    int[] A = new int[1000000];
    int[] B = new int[1000000];

    int zeroCount = 0;

    Scanner read = new Scanner(new File("C:/Users/Eric/Documents/Data Structures/EURUSD.csv"));
    read.nextLine();
    while(read.hasNextLine()) {
      String[] n= read.nextLine().split(",");
      open[size]=Double.parseDouble(n[0]);
      low[size]=Double.parseDouble(n[1]);
      high[size]=Double.parseDouble(n[2]);
      close[size]=Double.parseDouble(n[3]);
      A[size]=Integer.parseInt(n[4]);
      B[size]=Integer.parseInt(n[5]);
      size++;
    }

    for (int i = 0; i < size; i++){
      System.out.println(open[i] + "\t" + low[i] + "\t" + high[i] + "\t" + close[i] + "\t" + A[i] + "\t" + B[i]);
      System.out.println("Line" + i);
      System.out.println(open[68722]);

      if(A[i]==0 && A[i+1] == 0 && A[i+2] == 0 && B[i]==0 && B[i+1] == 0 && B[i+2] == 0) {
        zeroCount++;
        System.out.println("Found Pattern !!!" + zeroCount);
        break;
      }
    }
  }
}

After I run it runs fine, but it finds three 0s however, the order isn't consecutive. I'm not sure why this is not working. Thank you.

A simpler and perhaps more efficient solution to your problem is to convert these arrays A and B you have into String Astring and Bstring where each character in the string is an element in your array: ie if

A={0,0,0,1,1,1,1,0,...};

Then Astring is:

Astring="00011110...";

Once you have this, finding what you are looking for is achieved in one line:

int index = Astring.indexOf("00011");

if index=-1, you do not have the sequence 0,0,0,1,1 you were looking for. Otherwise, A(index) will be the first element in the pattern you were looking for (ie A(index)=0, A(index+1)=0, ..., A(index+4)=1).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM