Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have code that creates a text file based on user input for price and puts a time stamp.
I'm trying to figure out a way that I can read the file and print a message if the price is different then the day before by 10% or more.

For example if at 11 am on Tuesday the value is 50, if the value is 60 on Wednesday, it will print "price at 11 on Wednesday, September 30, 2020 is more than 10% from the day before"

Thanks so much!

What I have tried:

The price.txt file looks like:

50 29-09-2020 11:49:54
55 29-09-2020 12:54:41
60 29-09-2020 13:08:16
58 29-09-2020 14:08:21
60 30-09-2020 11:29:34
56 30-09-2020 12:34:21
60.3 30-09-2020 13:48:36
58.1 30-09-2020 14:18:11

I read the file:
public class ReadFiles {
  public static void main(String[] args) {
    try {
      File readFile = new File("price.txt");
      Scanner fileReader = new Scanner(readFile);
      while (fileReader.hasNextLine()) {
        String fileContent = fileReader.nextLine();
    } catch (FileNotFoundException e) {
      System.out.println("file was not found");
Updated 30-Sep-20 5:32am
Garth J Lancaster 30-Sep-20 1:17am    
Some questions
1) You use "is different then the day before" and "at 11 am on Tuesday the value is 50, if the value is 60 on Wednesday" but don't really clarify the basis for comparison .. are you saying that at 11:00am Wednesday, you wish to check the price 11:00am Tuesday, or, 'end-of-day Tuesday' .. what happens if Tuesday doesn't have a record for 11:00am ?
2) are the time-stamps per day unique - eg for Tuesday there will only be one or none of (time part) 12:34:41 ?
ynjay 30-Sep-20 9:54am    
Thank you for checking out my question.
1) Yes I want to check the price at 11 am on Wednesday and compare it with the price at 11 on Tuesday, each day should have a record for every hour.
2) They are not unique, so 11:32 and 11:56 are both for 11 o'clock. The price gets checked only once for each hour, even if there is a recording of 11:59 and 12:01, those are still different, 11 and 12 respectively.

1 solution

So you just need to check the date and the hour to be able to compare the different values. Use the String (Java Platform SE 7 )[^] methods to extract the different fields from the lines of input.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900