3

So, I've searched around stackoverflow for a bit, but I can't seem to find an answer to this issue.

My current homework for my CS class involves reading from a file of 5000 random numbers and doing various things with the data, like putting it into an array, seeing how many times a number occurs, and finding what the longest increasing sequence is. I've got all that done just fine.

In addition to this, I am (for myself) adding in a method that will allow me to overwrite the file and create 5000 new random numbers to make sure my code works with multiple different test cases.

The method works for the most part, however after I call it it doesn't seem to "activate" until after the rest of the program finishes. If I run it and tell it to change the numbers, I have to run it again to actually see the changed values in the program. Is there a way to fix this?

Current output showing the delay between changing the data:

Not trying to change the data here- control case.

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. n
What number are you looking for?    66
66 was found 1 times.
The longest sequence is [606, 3170, 4469, 4801, 5400, 8014]
It is 6 numbers long.

The numbers should change here but they don't.

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. y
What number are you looking for?    66
66 was found 1 times.
The longest sequence is [606, 3170, 4469, 4801, 5400, 8014]
It is 6 numbers long.

Now the data shows that it's changed, the run after the data should have been changed.

elkshadow5$ ./CompileAndRun.sh

Create a new set of numbers? Y for yes. n
What number are you looking for?    1
1 was found 3 times.
The longest sequence is [1155, 1501, 4121, 5383, 6000]
It is 5 numbers long.

My code:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;

public class jeftsdHW2 {
static Scanner input = new Scanner(System.in);

public static void main(String args[]) throws Exception {
    jeftsdHW2 random = new jeftsdHW2();
    int[] data;
    data = new int[5000];
    random.readDataFromFile(data);
    random.overwriteRandNums();
}
public int countingOccurrences(int find, int[] array) {
    int count = 0;
    for (int i : array) {
        if (i == find) {
            count++;
        }
    }
    return count;
}
public int[] longestSequence(int[] array) {
    int[] sequence;
    return sequence;
}
public void overwriteRandNums() throws Exception {
    System.out.print("Create a new set of numbers? Y for yes.\t");
    String answer = input.next();
    char yesOrNo = answer.charAt(0);
    if (yesOrNo == 'Y' || yesOrNo == 'y') {
        writeDataToFile();
    }
}
public void readDataFromFile(int[] data) throws Exception {
    try {
        java.io.File infile = new java.io.File("5000RandomNumbers.txt");
        Scanner readFile = new Scanner(infile);

        for (int i = 0; i < data.length; i++) {
            data[i] = readFile.nextInt();
        }
        readFile.close();
    } catch (FileNotFoundException e) {
        System.out.println("Please make sure the file \"5000RandomNumbers.txt\" is in the correct directory before trying to run this.");
        System.out.println("Thank you.");
        System.exit(1);
    }
}
public void writeDataToFile() throws Exception {
    int j;
    StringBuilder theNumbers = new StringBuilder();
    try {
        PrintWriter writer = new PrintWriter("5000RandomNumbers.txt", "UTF-8");
        for (int i = 0; i < 5000; i++) {
            if (i > 1 && i % 10 == 0) {
                theNumbers.append("\n");
            }
            j = (int) (9999 * Math.random());
            if (j < 1000) {
                theNumbers.append(j + "\t\t");
            } else {
                theNumbers.append(j + "\t");
            }
        }
        writer.print(theNumbers);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        System.out.println("error");
    }
}
}
elkshadow5
  • 369
  • 2
  • 4
  • 17
  • You haven't specified how much time it is taking. – Nabin Bhandari Oct 02 '17 at 00:48
  • 1
    is your _longest sequence_ code reads from file each time ? can you share that part of code ? – jmj Oct 02 '17 at 00:51
  • I tried to display how long it's taking, it doesn't write to the file until after the program has finished running. I'm "detecting" it because the numbers it is printing out for longestSequence isn't changing until the next time it runs – elkshadow5 Oct 02 '17 at 01:47
  • `longest sequence` reads from an array that is created and filled in a method that reads all the data from the file – elkshadow5 Oct 02 '17 at 01:49
  • You still need to show the client of these functions; the order of its operations may matter a lot. – Davis Herring Oct 02 '17 at 02:23
  • You mean like just the entire program? – elkshadow5 Oct 02 '17 at 02:31
  • That's the [MCVE](https://stackoverflow.com/help/mcve) idea. You can actually leave out `longestSequence`'s definition, so long as we know that it does nothing but read its `int[]` argument. – Davis Herring Oct 02 '17 at 02:37
  • 1
    Do you mean you want to run your logic on always newly created random numbers? If yes, then change the order below lines as below: `random.overwriteRandNums(); random.readDataFromFile(data);` – Rishikesh Darandale Oct 02 '17 at 03:05
  • The stub `longestSequence` is great, but where is the call to it? – Davis Herring Oct 02 '17 at 03:35

1 Answers1

0

It is possible that the file has not been physically written to the disk, using flush is not enough for this, from the java documentation here:

If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.

Because of the HDDs read and write speed, it is advisable to depend as little as possible on HDD access.

Perhaps storing the random number strings to a list when re-running and using that would be a solution. You could even write the list to disk, but this way the implementation does not depend on the time the file is being written.

EDIT

After the OP posted more of its code it became apparent that my original answer is not relatede to the problem. Nonetheless it is sound.

The code OP posted is not enough to see when is he reading the file after writing. It seems he is writing to the file after reading, which of course is what is percieved as an error. Reading after writing should produce a program that does what you want. Id est, this:

random.readDataFromFile(data); 
random.overwriteRandNums();

Will be reflected until the next execution. This:

random.overwriteRandNums();
random.readDataFromFile(data); 

Will use the updated file in the current execution.

Odo Frodo
  • 26
  • 3
  • 2
    It doesn't matter. Opening the file again for read will still read what has been written to the file. – user207421 Oct 02 '17 at 03:16
  • I agree with you, of course. The aim of my answer is to offer something the OP can try to solve the problem and simplify the implementation (and giving a reason to try it) , making debbuging easier if the proposed approach is not enough (by eliminating the I/O part). But I agree with you. – Odo Frodo Oct 02 '17 at 03:26