我需要一些帮助,因为我被困了。 我刚刚用Java编写了一个程序,它逐行读取文件,进行一些计算并打印结果。例如,如果我在我的csv文件中有60行,它将打印60000个结果。问题是,当我有100行时它工作正常,当我有1000(这个场景中的程序将打印100000个值)它只返回100000零。 会出现什么问题?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
public class WillemainApp {
static String delimiter = ",";
public static void main(String args[]) throws IOException {
BufferedReader br = null;
try {
String sCurrentLine;
// read the file and store each line in an object
br = new BufferedReader(new FileReader("C:\\Book2.csv"));
int [] Demand = new int[36];
int [] LeadTime = new int[1000];
List<Materials> materials = new ArrayList<Materials>();
List<LeadTimeDemand> lead = new ArrayList<LeadTimeDemand>();
while ((sCurrentLine = br.readLine()) != null) {
String [] fields = sCurrentLine.split(delimiter);
for (int i = 0; i < fields.length; i++) {
Demand[i] = Integer.parseInt(fields[i]);
if (Demand[i] < 0) {
Demand[i] = 0;
}
}
materials.add(new Materials(Demand));
}
for (Materials m :materials) {
for(int f = 0; f < LeadTime.length; f++) {
LeadTime[f] = 0;
}
// some preperations in order to define some parameters of the problem
double ZeroToZero = 0;
double ZeroToNonZero = 0;
double NonZeroToZero = 0;
double NonZeroToNonZero = 0;
int [] materialdemand = m.getDemand();
int countzero = 0;
for (int v =0; v < materialdemand.length; v++) {
if (materialdemand[v] >0) {
countzero++;
}
}
int NonZeroValues[] = new int [countzero];
int q = 0;
for(int e = 0; e < materialdemand.length; e++) {
if (materialdemand[e] != 0) {
NonZeroValues[q] = materialdemand[e];
q++;
}
}
int [] forecastsequence = new int [materialdemand.length];
for(int j = 0; j < materialdemand.length - 1; j++) {
if ((materialdemand[j] == 0) && (materialdemand[j + 1] == 0)) {
ZeroToZero = ZeroToZero + 1;
}
if ((materialdemand[j] == 0) && (materialdemand[j + 1] != 0)) {
ZeroToNonZero = ZeroToNonZero + 1 ;
}
if ((materialdemand[j] != 0) && (materialdemand[j + 1] == 0)) {
NonZeroToZero = NonZeroToZero + 1;
}
if ((materialdemand[j] != 0) && (materialdemand[j + 1] != 0)) {
NonZeroToNonZero = NonZeroToNonZero + 1;
}
}
double ZeroToZeroInterval = (ZeroToZero / (ZeroToZero + ZeroToNonZero)) * 100;
double ZeroToNonZeroInterval = (ZeroToNonZero / (ZeroToZero + ZeroToNonZero)) * 100;
double NonZeroToZeroInterval = (NonZeroToZero / (NonZeroToZero + NonZeroToNonZero)) * 100;
double NonZeroToNonZeroInterval = (NonZeroToNonZero / (NonZeroToZero + NonZeroToNonZero)) * 100;
if (ZeroToZeroInterval == ZeroToNonZeroInterval) {
ZeroToZeroInterval = ZeroToZeroInterval - 0.1;
}
if (NonZeroToZeroInterval == NonZeroToNonZeroInterval) {
NonZeroToZeroInterval = NonZeroToZeroInterval - 0.1;
}
double min = 0;
int kal = 0;
if (ZeroToZeroInterval < ZeroToNonZeroInterval) {
min = ZeroToZeroInterval;
} else {
min = ZeroToNonZeroInterval;
}
double min2 = 0;
if (NonZeroToZeroInterval < NonZeroToNonZeroInterval) {
min2 = NonZeroToZeroInterval;
} else {
min2 = NonZeroToNonZeroInterval;
}
while (kal <1000) {
forecastsequence[0] = materialdemand[0];
for (int w = 1; w < forecastsequence.length; w++) {
forecastsequence[w] = 0;
}
for (int z = 0; z < materialdemand.length - 1; z++) {
Random rn = new Random();
int random = rn.nextInt(101);
if (materialdemand[z] == 0) {
if ((random > min) && (min == ZeroToZeroInterval)) {
forecastsequence[z + 1] = NonZeroValues[rn.nextInt(NonZeroValues.length)];
}
if ((random > min) && (min == ZeroToNonZeroInterval)) {
forecastsequence[z + 1] = 0;
}
if ((random < min) && (min == ZeroToZeroInterval)) {
forecastsequence[z + 1] = 0;
}
if ((random < min) && (min == ZeroToNonZeroInterval)) {
forecastsequence[z + 1] = NonZeroValues[rn.nextInt(NonZeroValues.length)];
}
}
if (materialdemand[z] != 0) {
if ((random < min2) && (min2 == NonZeroToZeroInterval)) {
forecastsequence[z + 1] = 0;
}
if ((random < min2) && (min2 == NonZeroToNonZeroInterval)) {
forecastsequence[z + 1] = NonZeroValues[rn.nextInt(NonZeroValues.length)];
}
if ((random > min2) && (min2 == NonZeroToZeroInterval)) {
forecastsequence[z + 1] = NonZeroValues[rn.nextInt(NonZeroValues.length)];
}
if ((random >min2) && (min2 == NonZeroToNonZeroInterval)) {
forecastsequence[z + 1] = 0;
}
}
}
for (int p = 0; p < forecastsequence.length; p++) {
if (forecastsequence[p] != 0) {
int JITTERED = (int) (1 + forecastsequence[p] + 0.5 * Math.sqrt(forecastsequence[p]));
forecastsequence[p] = JITTERED;
}
}
int [] LeadTimeDemand = new int [forecastsequence.length - 1];
for (int ram = 0; ram < LeadTimeDemand.length; ram++) {
LeadTimeDemand[ram] = 0;
}
int sum = 0;
for (int d = 0; d < LeadTimeDemand.length;d++) {
LeadTimeDemand[d] = forecastsequence[d] + forecastsequence[d+1];
sum = sum + LeadTimeDemand[d];
}
LeadTime[kal] = sum;
kal++;
System.out.println(sum);
}
}
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
答案 0 :(得分:0)
查看文件的编码! 在BBEdit或TextWrangler *中打开文件。 将文件设置为Unicode(UTF-16 Little-Endian)(行结束可以是Unix或Windows)。保存! 在Excel中:数据&gt;获取外部数据&gt;导入文本文件...
问题的根源可能是错误的编码。