-1

My code pulls from three text files in order to identify the date/time of the highest value in a list (and display the prior and following five values). Unfortunately, after exporting it as a runnable JAR from Eclipse (I'm including all three text files in the export), it produces absolutely no output. I tried Google and Stack Overflow, but can't seem to find the source of the error. Do you think it's more likely to be an issue with my code, or something I'm doing in Eclipse (e.g. when exporting the file)? Here is how I'm exporting this as a Runnable Jar File

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.Arrays;
import java.util.Collections;

public class FindTheMaxGeiger {

    public static void main (String[] args) {

        String [] dateStamp = getDate("4_22_18_dates.txt");
        String [] timeStamp = getTime("4_22_18_times.txt");

            try {
            Scanner scanner1 = new Scanner(new File("4_22_18_radiation.txt"));

            int radCtr = 0; 
            while (scanner1.hasNextLine()) {
                radCtr++;
                scanner1.nextLine();
                }
            Scanner scanner2 = new Scanner(new File("4_22_18_radiation.txt"));
            int [] radiation = new int [radCtr]; //create the radiation array
            int i = 0;
            while(scanner2.hasNextLine()){
               radiation[i++] = scanner2.nextInt();
            }

            int max = getMax(radiation);
            System.out.println("Date        Counts per Minute");
            System.out.println("-------------------------------");
            System.out.println(dateStamp[max-5]+ " " + timeStamp[max-5] + "        " + radiation[max-5]);
            System.out.println(dateStamp[max-4]+ " " + timeStamp[max-4] + "        " + radiation[max-4]);
            System.out.println(dateStamp[max-3]+ " " + timeStamp[max-3] + "        " + radiation[max-3]);
            System.out.println(dateStamp[max-2]+ " " + timeStamp[max-2] + "        " + radiation[max-2]);
            System.out.println(dateStamp[max-1]+ " " + timeStamp[max-1] + "        " + radiation[max-1]);
            System.out.println(dateStamp[max]+ " " + timeStamp[max] + "        " + radiation[max] + "(This is the max)");
            System.out.println(dateStamp[max+1]+ " " + timeStamp[max+1] + "        " + radiation[max+1]);
            System.out.println(dateStamp[max+2]+ " " + timeStamp[max+2] + "        " + radiation[max+2]);
            System.out.println(dateStamp[max+3]+ " " + timeStamp[max+3] + "        " + radiation[max+3]);
            System.out.println(dateStamp[max+4]+ " " + timeStamp[max+4] + "        " + radiation[max+4]);
            System.out.println(dateStamp[max+5]+ " " + timeStamp[max+5] + "        " + radiation[max+5]);

            }

                catch (FileNotFoundException e){

                }



    }

        //here we call the method to find the max

        public static String[] getDate(String file) {
        //step 1:
        // count the number of lines in the file
        //step 2 - create the array and copy the elements in
        int ctr = 0;
        try {

            Scanner s3 = new Scanner(new File(file));
            while (s3.hasNextLine()) {
                ctr++;
                s3.nextLine();
            }
            String[] dateStamp = new String[ctr]; //creation

            Scanner s4 = new Scanner(new File(file));
            for (int i = 0; i < ctr; i++) {
                dateStamp[i] = s4.next();           
            }

            return dateStamp;
        }
        catch (FileNotFoundException e){

        }
        return null;
    }

    //get time
        public static String[] getTime(String file) {
            //step 1:
            // count the number of lines in the file
            //step 2 - create the array and copy the elements in
            int ctr = 0;
            try {

                Scanner s5 = new Scanner(new File(file));
                while (s5.hasNextLine()) {
                    ctr++;
                    s5.nextLine();

                }
                String[] timeStamp = new String[ctr]; //creation

                Scanner s6 = new Scanner(new File(file));
                for (int i = 0; i < ctr; i++) {
                    timeStamp[i] = s6.next();

                }

                return timeStamp;
            }
            catch (FileNotFoundException e){

            }
            return null;
}

        public static int getMax(int[] inputArray){ 
            int maxValue = inputArray[0]; 
            int maxLoc = 0;
            for(int i=1;i < inputArray.length;i++){ 
              if(inputArray[i] > maxValue){ 
                 maxValue = inputArray[i]; 
                 maxLoc = i;
              } 
            } 
            return maxLoc;}}    
  • 2
    do not eat up the exception in catch (FileNotFoundException e) then probably you can see the error. – gagan singh Jun 07 '18 at 18:56
  • Never throw aways exceptions like that, always at least call `e.printStackTrace`. It looks like you are trying to use `File` to access something in a Jar - objects in Jars are **not** files and you can't use `File` to access them. Look at something like `getResourceAsStream`. – greg-449 Jun 07 '18 at 18:59
  • When I take out the exception catch, I get FileNotFoundException. I'm not sure what to interpret from this since (1) the code successfully pulls the .txt files WITH the catch, and (2) the files are imbedded into the .java – Benjamin Pardue Jun 07 '18 at 19:04
  • You should leave to catch's in but you must report the error they catch for example by calling 'e.printStacktrace'. But as I said when you package everything in to a Jar using `File` will no longer work because objects in a Jar are **not** files. – greg-449 Jun 07 '18 at 19:12
  • _"the code successfully pulls the .txt files WITH the catch"_ -- clearly this is not true. _"the files are imbedded into the .java "_ -- no, they're not, only the filenames are hardcoded, and unless the files are in the right place they won't be found. – Jim Garrison Jun 07 '18 at 19:14
  • @JimGarrison so the thing is when he runs from IDE they are files with path so it "works" – mavriksc Jun 07 '18 at 19:17

1 Answers1

0

as mentioned the files are now compressed and inside the jar and don't live on the file system use something like InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");

look here for how to convert the inputStream to a String. or it seems that you can use a Scanner directly on the stream. you will need to know the char encoding

mavriksc
  • 1,130
  • 1
  • 7
  • 10