昨天我有一个问题,对于我在工作中所做的项目有一个迅速而有用的答案,所以今天我想我会再试一次我的另一个问题。
我的项目是采取一个" ACH"文件(只是一个固定格式的文本文件)使用GUI,处理它,重新格式化数据(一些复制一次,一些不复制,一些不复制),最后导出到一个新的.ACH文件。
我首先要举一个我将要使用的文件类型的例子,这只是一个例子,因为我无法在线发布真正的ACH文件。
111111111111111111111111
522293884838383848484838483884
62837717273727
62993304993918
621272773727755828
821200303299191
90000000000000000000000000
99999999999999999999999999
99999999999999999999999999
"处理"要求我批量生产#34;上面的数据格式我把它放在" clusters" 5-6-8与" 5"和" 8"每个群集的数量相同,并使用列表中的每个六个,例如:
522293884838383848484838483884
62837717273727
821200303299191
522293884838383848484838483884
62993304993918
821200303299191
//and so on
我创建了一个程序,允许我(几乎)实现这个但我遇到了一个问题 - 一个" nullpointerexception"。在我开始进一步解释之前,我的问题允许我发布我的代码:
package nacha;
import java.io.*;
import javax.accessibility.Accessible;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Nacha extends JComponent implements Accessible
{
public static void main(String args[])
{
String five = null;
String eight = null;
BufferedReader br = null;
BufferedWriter bw = null;
int count1 = 0;
int count2 = 0; //Counter variables for the numbers that should only
int count3 = 0; //be called from the ACH once.
//Used for later constructing of the output file.
//GUI stuff below for the input file
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT & ACH Files", "txt", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try{ //Section for the buffer reader/writer.
String sCurrentLine;
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
while((sCurrentLine = br.readLine()) !=null)
{
//Below is conditional statements that check to see if the line contains certain
//characters. If they do the it takes the lines and writes them to a new
//document (set above). It also adds +1 to the counter so the same numbers
//will not be written twice.
System.out.println(sCurrentLine);
if (sCurrentLine.startsWith("5")){
count1++;
bw.write(sCurrentLine);
bw.newLine();
five = sCurrentLine;
}else if (sCurrentLine.startsWith("6") && count2 == 0){
count2++;
bw.write(sCurrentLine);
bw.newLine();
}else if (sCurrentLine.startsWith("8")){
count3++;
bw.write(sCurrentLine);
bw.newLine();
eight = sCurrentLine;
}else if(sCurrentLine.startsWith("6") && count2 ==1){
bw.write(five);
bw.write(sCurrentLine);
bw.write(eight);
}else{
}
}
}catch (IOException e){
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
if (bw != null)bw.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
}
}
}
假设我只有一个" 6"我的原始文件中的数字,该程序工作正常。当我有一个以上的时候会出现这个问题,因为我没有办法(我知道)要重新调整" 5"和" 8"数不止一次。我最初的计划是复制" sCurrentLine"字符串分为两个字符串,称为"五个"和"八"这可以在以后轻松引用,并在每个" 6"之前和之后输入它们的值。文本文件中剩余的数字如下所示:
if (sCurrentLine.startsWith("5")){
count1++;
bw.write(sCurrentLine);
bw.newLine();
five = sCurrentLine;
和
}else if (sCurrentLine.startsWith("8")){
count3++;
bw.write(sCurrentLine);
bw.newLine();
eight = sCurrentLine;
我会在这里使用这些值:
}else if(sCurrentLine.startsWith("6") && count2 ==1){
bw.write(five);
bw.write(sCurrentLine);
bw.write(eight);
我得到了一个" nullpointer"我尝试写入(五)变量的行上的错误,该变量应该包含我设置"五"时的sCurrentLine的值。变量。 (所以它应该简单地发布一个" 5 somtething"数字。无论我尝试了多少种方法,我都无法找到一种方法来制作"五"等于sCurrentLine而不会产生错误我也尝试过使用PrintScreen来检查我设置的方法" 5"它实际上确实设置成功,所以我完全迷失了如何纠正这种情况。
我希望有人可以提供我如何纠正这个问题。这是一项工作任务,而不是随意的,我很想解决这个问题。
提前致谢,如果您需要更多信息,请告诉我们!
编辑:我道歉:这是例外:
Exception in thread "main" java.lang.NullPointerException
at java.io.Writer.write(Unknown Source)
at nacha.Nacha.main(Nacha.java:83)
标记为:
bw.write(eight);
答案 0 :(得分:2)
if (sCurrentLine.startsWith("5")){
count1++;
bw.write(sCurrentLine);
bw.newLine();
five = sCurrentLine;
}else if (sCurrentLine.startsWith("6") && count2 == 0){
count2++;
bw.write(sCurrentLine);
bw.newLine();
}else if (sCurrentLine.startsWith("8")){
count3++;
bw.write(sCurrentLine);
bw.newLine();
eight = sCurrentLine;
}else if(sCurrentLine.startsWith("6") && count2 ==1){
bw.write(five);
bw.write(sCurrentLine);
bw.write(eight);
}
注意在第一个if语句中,设置变量5。
五现在有一个值,所以它不是null
。
您永远不会将八个值设置为值,因此它仍为null
。
此程序后的程序流程:
111111111111111111111111
522293884838383848484838483884
62837717273727
62993304993918
621272773727755828
821200303299191
90000000000000000000000000
99999999999999999999999999;
99999999999999999999999999
您首先到达第一个if语句,将五个设置为“522293884838383848484838483884”
达到第二个if语句,将“62837717273727”打印到文件。
未达到第三个if语句,因为“62993304993918”以6开头,而不是8开始。
然后达到第四个if语句,尝试写入五个变量,当前行和一个null 8变量。
我建议创建一个字符串ArrayList,对于以6开头的每一行,将字符串添加到arraylist。然后,当你设置五和八时,迭代arraylist并按你认为合适的方式写作。
ArrayList <String> sixValues = new ArrayList();
if(sCurrentLine.startsWith("6"){
sixValues.add(sCurrentLine);
}
和for-loop
for( String s : sixValues){
bw.write(five);
bw.write(sCurrentLine);
bw.write(eight);
}
答案 1 :(得分:0)
感谢Psychrom帮助我找到这个解决方案,他的回答是100%正确的我需要做的事情我希望我可以给他一些声誉,但我不能。
这是我的最终工作代码FYI:
package nacha;
import java.awt.List;
import java.io.*;
import java.util.ArrayList;
import javax.accessibility.Accessible;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Nacha extends JComponent implements Accessible
{
public static void main(String args[])
{
ArrayList<String> list = new ArrayList<String>();
String five = null;
String eight = null;
BufferedReader br = null;
BufferedWriter bw = null;
int count1 = 0;
int count2 = 0; //Counter variables for the numbers that should only
int count3 = 0; //be called from the ACH once.
//Used for later constructing of the output file.
//GUI stuff below for the input file
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TXT & ACH Files", "txt", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try{ //Section for the buffer reader/writer.
String sCurrentLine;
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
while((sCurrentLine = br.readLine()) !=null)
{
//Below is conditional statements that check to see if the line contains certain
//characters. If they do the it takes the lines and writes them to a new
//document (set above). It also adds +1 to the counter so the same numbers
//will not be written twice.
if (sCurrentLine.startsWith("5")){
five = sCurrentLine;
}else if (sCurrentLine.startsWith("6")){
list.add(sCurrentLine);
}else if (sCurrentLine.startsWith("8")){
eight = sCurrentLine;
}else{
}
}
while(list.size() > 0){
String six = null;
bw.write(five);
bw.newLine();
six = list.get(0);
bw.write(six);
bw.newLine();
list.remove(0);
bw.write(eight);
bw.newLine();
}
}catch (IOException e){
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
if (bw != null)bw.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
}
}
}
更新: 对于任何看过这个或有兴趣的人,我想我会更新我的最新进展。这次我必须编辑程序,以便它可以接受一个可能包含多个5或6的ACH文件,这意味着我需要程序循环遍历第一个5的实例,每6个循环后跟8,然后是下一个实例5然后是每6个后跟8个,依此类推。
我最终在“while”循环内部的“while”循环中使用了“if语句”。如果有人有办法更好地做到这一点或使代码更专业,我很乐意听到它:
package nacha;
import java.io.*;
import java.util.ArrayList;
import javax.accessibility.Accessible;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class Nacha extends JComponent implements Accessible
{
public static void main(String args[])
{
ArrayList<String> listSix = new ArrayList<String>(); //List used to store the 6 starting numbers.
ArrayList<String> listFive = new ArrayList<String>(); //List used to store the 5 starting numbers.
String five = null; //String used to store 5 starting numbers
String eight = null; //String used to store 8 starting numbers.
BufferedReader br = null;
BufferedWriter bw = null;
//GUI stuff below for the input file
JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Please choose ACH file to upload");
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION)
{
try{
String sCurrentLine; //Sets the string for the line the Buffered Reader is currently reading.
//Below sets the location for the reader/writer actions.
br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));
while((sCurrentLine = br.readLine()) !=null)
{
//Below are the conditional statements that check to see if the line begins with a certain
//character then either (1)writes the line to a string (2)writes the line into the array list
//or (3)does nothing and "ignores" the line.
if (sCurrentLine.startsWith("5")){
listFive.add(sCurrentLine);
}else if (sCurrentLine.startsWith("6")){
listSix.add(sCurrentLine);
}else if (sCurrentLine.startsWith("8")){
eight = sCurrentLine;
}else{
}
}
while(listFive.size() > 0){ //While there is some value in the "5" list.
five = listFive.get(0); //Make the string "five" equal to index 0 of the list.
ArrayList<String> listSixDuplicate = new ArrayList<String>(listSix); //Creates a duplicate of "six" list to manipulate.
while(listSixDuplicate.size() > 0){ //While there is some value in the "6" list.
bw.write(five);
bw.newLine();
bw.write(listSixDuplicate.get(0));// <------- Writes information in correct format to the outgoing file.
listSixDuplicate.remove(0);
bw.newLine();
bw.write(eight);
bw.newLine();
int size = listSixDuplicate.size(); //Creates a variable called "size" that is the size of "six".
if (size == 0) //If the size of the list "six" is empty..
listFive.remove(0); //Remove the value from index "0" of list "five".
//Loops back to beginning to see if list "five" has any more values.
}
}
//Lets the user know what the output file is named and where it has been saved.
JOptionPane.showMessageDialog(null, "Output file saved as NachaOutput.ach to " + chooser.getCurrentDirectory());
//Below is necessary exception handling for using the buffered reader/writer.
}catch (IOException e){
e.printStackTrace();
} finally {
try {
//Closes the buffer reader/writer.
if (br != null)br.close();
if (bw != null)bw.close();
}catch (IOException ex){
ex.printStackTrace();
}
}
}
}
}