我的代码从三个文本文件中提取,以便识别列表中最高值的日期/时间(并显示之前和之后的五个值)。不幸的是,在从Eclipse导出它作为可运行的JAR之后(我在导出中包含了所有三个文本文件),它绝对没有输出。我试过谷歌和Stack Overflow,但似乎无法找到错误的来源。您是否认为我的代码更容易出问题,或者我在Eclipse中做的事情(例如导出文件时)? 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;}}
答案 0 :(得分:0)
如上所述,文件现在已经压缩并且在jar中,并且不会存在于文件系统中
使用InputStream in = this.getClass().getClassLoader().getResourceAsStream("SomeTextFile.txt");
查看here,了解如何将inputStream转换为String。或者您似乎可以直接在流上使用Scanner
。你需要知道char编码