1

Here is the output that I am getting:

Here is the output that I am getting

This code is supposed to read the text file and print all instances where the CPM is with 5 measurements of the Top CPM. The code uses a separate class to format (.getDateTime & .getCountsPerMinute) For some reason, the code doesn't print the formatted text file. I have been messing with it all day and cannot seem to figure out where I'm going wrong. Any pointers or help would be appreciated.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

public class geiger {
    public static final int COUNT_WINDOW = 5;
    public static void main(String[] args) throws Exception {
        try {
            Scanner sc = new Scanner(new File("7_14_2019.txt"));
            ArrayList<RadiationSample> samples = new ArrayList<RadiationSample>();
            String lineSeparator = System.getProperty("line.separator");
            sc.useDelimiter(lineSeparator);
            String dataLineTag = ".*Every Minute.*";
            int maxCounts = 0;
            while (sc.hasNext()) {
                String line = sc.next();
                if (line.matches(dataLineTag)) {
                    String[] parsed = line.split(",");
                    String dateTime = parsed[0];
                    int counts = Integer.parseInt(parsed[2]);
                    RadiationSample sample = new RadiationSample(dateTime, counts);
                    samples.add(sample);
                    if (counts > maxCounts) maxCounts = counts;
                }
            }
            System.out.println("    Date    Time    Counts Per Minute");
            for (int i = 0; i < samples.size(); i++) {
                if (samples.get(i).getCountsPerMinute() >= maxCounts - COUNT_WINDOW) {
                    System.out.println(samples.get(i).getDateTime()
                            + "             " + samples.get(i).getCountsPerMinute());
                }
            }
            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

separate class

public class RadiationSample {
    private String dateTime;
    private int countsPerMinute;

    public RadiationSample(String dt, int counts) {
        dateTime = dt;
        countsPerMinute = counts;
    }

    public String getDateTime() {
        return dateTime;
    }

    public int getCountsPerMinute() {
        return countsPerMinute;
    }

    public static void main(String[] args) {
        RadiationSample sample = new RadiationSample("4/6/2018 17:15", 17);
        System.out.println("Date and Time : " + sample.getDateTime() +
                "\nCounts Per Minute: " + sample.getCountsPerMinute());
    }
}

Text File

GQ Electronics LLC, GMC Data Viewer,Version 2.40,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Date Time,uSv/h,CPM,#1,#2,#3,#4,#5,#6,#7,#8,#9,#10,#11,#12,#13,#14,#15,#16,#17,#18,#19,#20,#21,#22,#23,#24,#25,#26,#27,#28,#29,#30,#31,#32,#33,#34,#35,#36,#37,#38,#39,#40,#41,#42,#43,#44,#45,#46,#47,#48,#49,#50,#51,#52,#53,#54,#55,#56,#57,#58,#59,#60
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
FEDERAL-WAY,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:13,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:14,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:15,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:16,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:17,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:18,Every Minute,23,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:19,Every Minute,25,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:20,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:21,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:22,Every Minute,25,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:23,Every Minute,22,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:24,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:25,Every Minute,8,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:26,Every Minute,20,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:27,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:28,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:29,Every Minute,11,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:30,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:31,Every Minute,19,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:32,Every Minute,13,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:33,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:34,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:35,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:36,Every Minute,21,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:37,Every Minute,19,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:38,Every Minute,23,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:39,Every Minute,20,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:40,Every Minute,19,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:41,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:42,Every Minute,19,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
FEDERAL-WAY,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:45,Every Minute,25,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:46,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:47,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:48,Every Minute,13,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:49,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:50,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:51,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:52,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:53,Every Minute,11,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:54,Every Minute,13,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:55,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:56,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:57,Every Minute,22,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:58,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 17:59,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:00,Every Minute,10,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:01,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:02,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:03,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:04,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:05,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:06,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:07,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:08,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:09,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:10,Every Minute,15,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:11,Every Minute,22,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:12,Every Minute,10,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:13,Every Minute,11,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:14,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:15,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:16,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:17,Every Minute,19,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:18,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:19,Every Minute,17,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:20,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:21,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:22,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:23,Every Minute,11,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:24,Every Minute,12,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:25,Every Minute,18,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:26,Every Minute,21,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:27,Every Minute,14,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
4/6/2018 18:28,Every Minute,16,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
MWiesner
  • 8,868
  • 11
  • 36
  • 70
wvvvy
  • 11
  • 3
  • I believe it has something to do with the for loop before the last print line but I don't understand what the issue with it is. – wvvvy Jun 03 '21 at 04:50

1 Answers1

1

The issue seems to be that your text file is stored as one line. So the first step would have to be to read this file and split it into separate lines. I have made some changes which seem to work.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

public class geiger {
    public static final int COUNT_WINDOW = 5;
    public static void main(String[] args) throws Exception {
        try {
            Scanner sc = new Scanner(new File("7_14_2019.txt"));
            ArrayList<RadiationSample> samples = new ArrayList<RadiationSample>();
            String lineSeparator = System.getProperty("line.separator");
            sc.useDelimiter(lineSeparator);
            String dataLineTag = ".*Every Minute.*";
            int maxCounts = 0;
            String[] file = sc.next().split(", ");
            int iter = 0, end = file.length;
            while (iter < end) {
                String line = file[iter];
                if (line.matches(dataLineTag)) {
                    String[] parsed = line.split(",");
                    String dateTime = parsed[0];
                    int counts = Integer.parseInt(parsed[2]);
                    RadiationSample sample = new RadiationSample(dateTime, counts);
                    samples.add(sample);
                    if (counts > maxCounts) maxCounts = counts;
                }
                iter++;
            }
            System.out.println("    Date    Time    Counts Per Minute");
            for (int i = 0; i < samples.size(); i++) {
                if (samples.get(i).getCountsPerMinute() >= maxCounts - COUNT_WINDOW) {
                    System.out.println(samples.get(i).getDateTime()
                            + "             " + samples.get(i).getCountsPerMinute());
                }
            }
            sc.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Program Output

Community
  • 1
  • 1
M. Amaan
  • 9
  • 2
  • This isnt giving me any input for the file. Console is just empty now. – wvvvy Jun 03 '21 at 05:20
  • Sorry I changed the name of the text file. Please revert it back to the original. "7_14_2019.txt" – M. Amaan Jun 03 '21 at 05:23
  • Im pretty sure the error on my code has something to do with the If statement right before the print line at the end. – wvvvy Jun 03 '21 at 05:24
  • Does my code work? Cuz it does give me results on my side. – M. Amaan Jun 03 '21 at 05:27
  • I have edited the answer to add the program output – M. Amaan Jun 03 '21 at 05:28
  • Im so confused on what im doing wrong, when I try this code now, it just prints the String output still without the actual info. Do you think that it could have something to do with my txt file? – wvvvy Jun 03 '21 at 05:33
  • Yes are you sure the text file is in the correct location? It should be in the project directory. Or else you need to enter the full path of the text file. For example if it is on Desktop you would have to enter "C://Users//Username//Desktop//7_14_2019.txt" – M. Amaan Jun 03 '21 at 05:36
  • even when I enter the full path, its just printing the string. I even tried deleting and reading the File. Do you have any other idea why it would be doing this? Thank you so much for all of your help so far. – wvvvy Jun 03 '21 at 05:38
  • Does it give any error? Is it possible to add a screenshot of the output? – M. Amaan Jun 03 '21 at 05:41
  • I have added the image of the output I get from both your code and my original code to my original post. Im not getting any errors that's whats driving me so insane – wvvvy Jun 03 '21 at 05:45
  • I really dont know what to say. Try rebuilding the project again and see if that works. – M. Amaan Jun 03 '21 at 05:55
  • I've once again tried deleting the txt file and redownloading it, that doesn't seem to fix it. I have no clue why its not working – wvvvy Jun 03 '21 at 05:55
  • Try adding System.out.println(end); before the while loop and see if it prints anything. – M. Amaan Jun 03 '21 at 06:01
  • Just tried rebuilding it its still giving me the same error :/ Thank you for trying to help. – wvvvy Jun 03 '21 at 06:01
  • "2 Date Time Counts Per Minute" – wvvvy Jun 03 '21 at 06:02
  • ^ the output of System.out.println(end) – wvvvy Jun 03 '21 at 06:02
  • One last thing before I give up. Do the following. Download the file to Desktop. Open and ensure it has all the data. Put the absolute path of the file (on Desktop) in Java. Test it . – M. Amaan Jun 03 '21 at 06:06
  • Still not working :/ thank you so much for trying to help. I'm beginning to wonder if this is an issue with my computer. I'm not a high enough level to upvote your post but will come back and upvote u when I get it. – wvvvy Jun 03 '21 at 06:11
  • Thanks. Sorry I couldn't help more. – M. Amaan Jun 03 '21 at 06:14