简体   繁体   中英

Reading numbers from a txt file and comparing them

With this program I want to 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 problem with my program is that it only reads 13 of the 15 lines of the "numbers.txt" and the buy-sell is at wrong places.

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

public class Practise {
    public static void main(String[] args) throws IOException {
        int num = 0;
        int up = 0;
        int down = 0;
        int same = 0;
        FileInputStream file = new FileInputStream("numbers.txt");
        Scanner scanner = new Scanner(file);
        while (scanner.hasNextDouble()) {
            Double[] array = new Double[15];
            for (int i = 0; i < array.length; i++) {
                array[i] = scanner.nextDouble();
            }
            for (int a = 0; a < array.length && a + 1 < array.length - 1; a++) {
                num++;
                System.out.print(num + "  " + (array[a]));
                if (array[a] < array[a + 1]) {
                    up++;
                } else if (array[a] > array[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();
            }
        }
    }
}

The file numbers.txt:

26.375
25.500
25.125
25.000
25.250
27.125
28.250
26.000
25.500
25.000
25.125
25.250
26.375
25.500
25.500

Expected output:

1 26.375
2 25.500
3 25.125
4 25.000
5 25.250 buy
6 27.125
7 28.250
8 26.000 sell
9 25.500
10 25.000
11 25.125 buy
12 25.250
13 26.375
14 25.500 sell
15 25.500

You use a + 1 < array.length - 1 , which can be transformed to a < array.length - 2 . I guess you understand that the second operand in && limits your iteration to 13.

a < array.length && a < array.length - 2 = a < 15 && a < 13 = a < 13

I did some minor changes on your code for it to work; you could refactor it a lot, but I stick to your style, so you can better understand the logic.

    int num = 1;
    //print the 1st element
    System.out.println(num + "  " + array[0]);
    for (int a = 1; a < array.length; a++) {
        num++;
        System.out.print(num + "  " + (array[a]));
        //plz note that we check with the before, not after
        if (array[a] < array[a - 1]) {
            down++;
        } else if (array[a] > array[a - 1]) {
            up++;
        } else {
            same++;
        }
        //changed down > to down ==
        if ((up >= 3 && (down == 1 || same >= 1))) {
            System.out.print("  " + "sell");
            up = 0;
            same = 0;
        } 
        //changed up > to up ==
        else if ((down >= 3 && (up == 1 || same >= 1))) {
            System.out.print("  " + "buy");
            down = 0;
            same = 0;
        }
        System.out.println();
    }

Answering to OPs comment: if you want to support more than 15 records you can keep adding to a list:

List<Double> list = new ArrayList<>();
while (scanner.hasNextDouble()) {
    list.add(scanner.nextDouble());
}
//if you want to work with array
Double[] array = new Double[list.size()];
array = list.toArray(array);

Here's a working example (had to change array to list)

int num = 0, up = 0, down = 0, same = 0;

FileInputStream file = new FileInputStream("numbers.txt");
Scanner scanner = new Scanner(file);
List<Double> list = new ArrayList<>();

while (scanner.hasNextDouble()) {
    list.add(scanner.nextDouble());
}

int position = 0;

while (position + 2 < list.size()) {
    Double[] nums = new Double[3];

    System.out.println(nums[0] = list.get(position));
    System.out.println(nums[1] = list.get(position + 1));
    System.out.println(nums[2] = list.get(position + 2));

    if (nums[1] > nums[0] && nums[2] > nums[1]) {
        System.out.println("buy");
        up++;
    } else if (nums[1] < nums[0] && nums[2] < nums[1]) {
        System.out.println("sell");
        down++;
    } else {
        same++;
    }
    position += 2;
}
System.out.println("Ups total: " + up);
System.out.println("Downs total: " + down);
System.out.println("Same total: " + same);

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