简体   繁体   中英

Java add field in csv file using JFrame

I wrote the program to create file and add in field using by jframe but it not working correctly, please help me thanks!!

read file get field full name and customer number and add one more field using jframe

full name and customer number working fine but jframe not working correctly.. so please help me...

and actually I need verticall scroll down as well, please help me..

suppose is there a 6 records in files in..

i need to put in jframe:

00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

file output is not correct actually it put all 6 scanner line in all lines file out..

custID,fullname, 00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

custID,fullname, 00240000844928953504
00240000844928953505
00240000844928953506
00240000844928953507
00240000844928953508
00240000844928953509

it like this!!:(

it should be like this!!

custID,fullname,00240000844928953504
custID,fullname,00240000844928953505
....

HEre is my code!!

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;


public class scanner {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub



        final JFrame frame = new JFrame("Scan Here: ");
        JPanel panel = new JPanel();

        final JTextArea text = new JTextArea(20, 40);
        JButton button = new JButton("Enter");

        frame.add(panel);
        panel.add(text);
        panel.add(button);

        button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {


         BufferedReader br = null;
         BufferedWriter lbwp = null;


         String scanner = (text.getText());
         System.out.println(scanner);


         try {


             File folderall = new File("FilesIn");
             File[] BFFileall = folderall.listFiles();

             for (File file : BFFileall) {

                 String str = file.getName();

                 String reprintbwletterbwpca = ("FileOut" + "\\" + str);
                 lbwp = new BufferedWriter(new FileWriter(reprintbwletterbwpca));
                 lbwp.write("\"CUSTID\",\"FullName\",\"ONECODE\"," + "\n");


                 br = new BufferedReader(new FileReader(file));

                 String line;
                 line = br.readLine();

                 while ((line = br.readLine()) != null) {
                    String[] actionID = line.split("\\\",\"");

                    String custnumber = actionID[3];
                    String fullname = actionID[18];
                    String add1 = actionID[19];


                    lbwp.write("\"" + custnumber +  "\",\"" + fullname+ "\"," + "\"" + scanner + "\"," + "\n");

                 }
                 lbwp.close();


             }

         } catch(Exception e1) {
                e1.printStackTrace();
            }


         frame.dispose();
            }});
        frame.setSize(500, 400);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    }

}

Please help me!!

Here you have two lists :

  • A list of entries coming from the file you are reading, each entry is read in "line" String.
  • A list of values you have input in a JTextArea separated by "\\n".

First of all, you propably need to check if you have the same number of entries in the both lists. You also need to split your String scanner using "\\n" as the separator.

As you are reading line by line, you can also use the following code which won't check if the number of entries are equals :

 int index = 0; String[] ids = scanner.split("\\n"); while ((line = br.readLine()) != null) { String[] actionID = line.split("\\\\\\",\\""); String custnumber = actionID[3]; String fullname = actionID[18]; String add1 = actionID[19]; if (ids.length > index) { lbwp.write("\\"" + custnumber + "\\",\\"" + fullname+ "\\"," + "\\"" + ids[index] + "\\"," + "\\n"); } else { lbwp.write("\\"" + custnumber + "\\",\\"" + fullname+ "\\"\\n"); } index++; } 

EDIT: replace the condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM