자바이용 text데이터 치환 시 "AWT-EventQueue-0"에러 발생 문제 도와주세요

조회수 50회

자바 독학으로 공부한지 3주 밖에 안되어 정확한 용어를 잘 모르는 점 양해 부탁 드립니다.

GUI에서 버튼 클릭시 textfield에 적힌 값으로 특정 텍스트파일의 내용을 치환하는 프로그램을 제작중에 있습니다.

텍스트 치환 class는 iotest3이며 textfied는 alpa3라는 다른 class에서 가져오게 했습니다.(iotest3내용은 밑에 첨부합니다. alpa3는 내용이 길어 생략합니다.)

그런데 alpa3 class에서 textfield 값을 가져오는데 문제가 생긴거 같습니다.

replaceString이라는 문자열에 textfield값을 넣으려 하는데

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 18

위와 같은 에러가 발생합니다.

제가 시도해본 것은

1) replaceString 문자열에 ""를 이용하여 특정 문자를 넣었을때는 잘 작동합니다.

2) textfield를 반복문을 이용하여 만들어서 textField[i]를 변수명으로 사용했는데 여기서 i를 정수로 정하여 실행해도 잘 작동됩니다.

단지, String replaceString = alpa3.textField[i].getText();처럼 textField[i] 형태로 받을경우 문제가 생겼습니다.

이 문제 해결에 관하여 많은 도움 부탁드립니다.

감사합니다.

import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException;

import javax.swing.JLabel; import javax.swing.JTextField;

import gui_test.alpa3;

class IOtest3 {

final static String FILE_NAME = ""; //수정할 Input Text 파일 경로
final static String FILE_NAME_Out = ""; //Output Text 파일 경로


IOtest3() {

    FileReader fr_before = null ;
    FileWriter fw = null ;

    BufferedReader br_before = null;
    BufferedWriter bw = null;

    System.out.println("버튼이 클릭되었습니다."); //버튼 작동 확인용

    String[] geo_para = new String[alpa3.BUTTON_SIZE_1];

    /*
    for (int i=0;i<alpa3.BUTTON_SIZE_1;i++) {

        geo_para[i] = alpa3.textField[i].getText();
        System.out.println(geo_para[i]);

        }
    */

    try{

        fr_before = new FileReader( FILE_NAME );
        br_before = new BufferedReader( fr_before );

        fw = new FileWriter( FILE_NAME_Out );
        bw = new BufferedWriter(fw);

        String readLine = null ;
        String repLine = null;


        int i = 0;


            while( ( readLine =  br_before.readLine()) != null ){


                String replaceString = alpa3.textField[i].getText();  //문제 발생지점
                //String replaceString = geo_para[i];  
                String originalString = "Geo"+i;

                    if(readLine.contains(originalString)){

                        repLine=readLine.replaceAll(originalString, replaceString);
                        bw.write(repLine);
                        bw.newLine();

                        i=i+1;

                    }
                    else {
                        bw.write(readLine);
                        bw.newLine();   
                    }
                }

            bw.flush();


    }catch ( IOException e ) {

        System.out.println(e);

    }finally{

        try { fr_before.close(); } catch ( IOException e ) {}
        try { br_before.close(); } catch ( IOException e ) {}

    }

}

}

2 답변

  • 결국 해결했습니다.

    replaceString 문자열이 textField[i]와 개수가 달라서 나타난 오류 같습니다.

    replaceString을textField[i]처럼 배열을 이용해 replaceString[i]로 선언해서 1:1 대응을 시켜주니 잘 작동됩니다.

    • 고생하셨네요. 저도 돌려볼려다가 생업에 바빠서 -.-; 편집요청빌런 2020.3.24 13:49
  • // 필요없는 부분 수정하여 alpa3 class code 올립니다.

    package gui_test;

    import java.awt.BorderLayout; import java.awt.EventQueue;

    import javax.swing.border.EmptyBorder; import javax.swing.*;

    import java.awt.GridBagLayout; import javax.swing.border.LineBorder; import javax.swing.border.TitledBorder;

    import java.awt.Color; import java.awt.Container; import java.awt.GridBagConstraints; import java.awt.GridLayout; import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.EventObject; import java.awt.*;

    import gui_test.IOtest3;

    public class alpa3 extends JFrame {

    public JPanel contentPane;
    public static JTextField textField[];
    public static JTextField textField_met[];
    public static JTextField textField_weld[];
    public JLabel lblNewLabel[];
    public JLabel lblNewLabel_met[];
    public JLabel lblNewLabel_weld[];
    public JMenuBar mb;
    public final static int BUTTON_SIZE_1 = 18;
    public final static int BUTTON_SIZE_met = 8;
    public final static int BUTTON_SIZE_weld = 2;
    
    
    JLabel ImgBox;
    
    
    private final static int img_p_y = 50;
    private final static int img_s_x = 250;
    private final static int img_s_y = 250;
    
    private final static int P_Geo_p_y = 25;
    private final static int P_Geo_s_x = 350;
    private final static int P_Geo_s_y = 487;
    
    private final static int P_met_p_y = 25;
    private final static int P_met_s_x = 350;
    private final static int P_met_s_y = 240;
    
    private final static int P_weld_p_y = 275;
    private final static int P_weld_s_x = 350;
    private final static int P_weld_s_y = 72;
    
    private final static int P_1st_raw = 10;
    private final static int P_2nd_raw = img_s_x+20;
    private final static int P_3rd_raw = P_2nd_raw+P_Geo_s_x+10;
    
    private final static int Frame_size_x = P_3rd_raw+P_weld_s_x+20;
    private final static int Frame_size_y = 562;
    
    
    
    
    
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    alpa3 frame = new alpa3();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    /**
     * Create the frame.
     */
    public alpa3() {
        setTitle("alpa 3");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(500, 100, Frame_size_x, Frame_size_y);
    
    
    
    
        contentPane = new JPanel();
        contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
        setContentPane(contentPane);
        contentPane.setLayout(null);
    
        JPanel panel = new JPanel();
        panel.setBounds(P_2nd_raw, P_Geo_p_y, P_Geo_s_x, P_Geo_s_y);
        panel.setBorder(new TitledBorder(new LineBorder(Color.gray,1),"Ge"));
        contentPane.add(panel);
        panel.setLayout(null);
    
        JPanel panel_met = new JPanel();
        panel_met.setBounds(P_3rd_raw, P_met_p_y, P_met_s_x, P_met_s_y);
        panel_met.setBorder(new TitledBorder(new LineBorder(Color.gray,1),"M"));
        contentPane.add(panel_met);
        panel_met.setLayout(null);
    
    
        JPanel panel_weld = new JPanel();
        panel_weld.setBounds(P_3rd_raw, P_weld_p_y, P_weld_s_x, P_weld_s_y);
        panel_weld.setBorder(new TitledBorder(new LineBorder(Color.gray,1),""));
        contentPane.add(panel_weld);
        panel_weld.setLayout(null);
    
        textField = new JTextField[BUTTON_SIZE_1];
    
        lblNewLabel = new JLabel[BUTTON_SIZE_1];
    
    
        for (int i=0;i<BUTTON_SIZE_1;i++)
        {
            lblNewLabel[i] = new JLabel();
            lblNewLabel[i].setBounds(13, 20+i*26, 200, 15);
            panel.add(lblNewLabel[i]);
    
            textField[i] = new JTextField();
            textField[i].setBounds(210, 17+i*26, 130, 21);
            panel.add(textField[i]);
            textField[i].setColumns(10);
            textField[i].setHorizontalAlignment(JTextField.RIGHT);
        }
        lblNewLabel[0].setText("");
        lblNewLabel[1].setText("");
        lblNewLabel[2].setText("");
        lblNewLabel[3].setText("");
        lblNewLabel[4].setText("");
        lblNewLabel[5].setText("");
        lblNewLabel[6].setText("");
        lblNewLabel[7].setText("");
        lblNewLabel[8].setText("");
        lblNewLabel[9].setText("");
        lblNewLabel[10].setText("");
        lblNewLabel[11].setText("");
        lblNewLabel[12].setText("");
        lblNewLabel[13].setText("");
        lblNewLabel[14].setText("B)");
        lblNewLabel[15].setText(")");
        lblNewLabel[16].setText("Ele");
        lblNewLabel[17].setText("E");
    
        textField[0].setText("2000");
        textField[1].setText("900");
        textField[2].setText("20");
        textField[3].setText("920");
        textField[4].setText("100");
        textField[5].setText("2.6");
        textField[6].setText("60");
        textField[7].setText("10");
        textField[8].setText("10");
        textField[9].setText("1400");
        textField[10].setText("1300");
        textField[11].setText("100");
        textField[12].setText("50");
        textField[13].setText("200");
        textField[14].setText("1700");
        textField[15].setText("1500");
        textField[16].setText("20");
        textField[17].setText("5");
    
    
    
        textField_met = new JTextField[BUTTON_SIZE_met];
    
        lblNewLabel_met = new JLabel[BUTTON_SIZE_met];
    
        for (int i=0;i<BUTTON_SIZE_met;i++)
        {
            lblNewLabel_met[i] = new JLabel();
            lblNewLabel_met[i].setBounds(13, 20+i*26, 200, 15);
            panel_met.add(lblNewLabel_met[i]);
    
            textField_met[i] = new JTextField();
            textField_met[i].setBounds(210, 17+i*26, 130, 21);
            panel_met.add(textField_met[i]);
            textField_met[i].setColumns(10);
            textField_met[i].setHorizontalAlignment(JTextField.RIGHT);
        }
    
        lblNewLabel_met[0].setText("S");
        lblNewLabel_met[1].setText("c");
        lblNewLabel_met[2].setText("S");
        lblNewLabel_met[3].setText("");
        lblNewLabel_met[4].setText("");
        lblNewLabel_met[5].setText("");
        lblNewLabel_met[6].setText("o");
        lblNewLabel_met[7].setText("");
    
        textField_met[0].setText("7850");
        textField_met[1].setText("212000");
        textField_met[2].setText("0.3");
        textField_met[3].setText("0.00001");
        textField_met[4].setText("7850");
        textField_met[5].setText("212000");
        textField_met[6].setText("0.3");
        textField_met[7].setText("0.00001");
    
        textField_weld = new JTextField[BUTTON_SIZE_weld];
    
        lblNewLabel_weld = new JLabel[BUTTON_SIZE_weld];
    
        for (int i=0;i<BUTTON_SIZE_weld;i++)
        {
            lblNewLabel_weld[i] = new JLabel();
            lblNewLabel_weld[i].setBounds(13, 20+i*26, 200, 15);
            panel_weld.add(lblNewLabel_weld[i]);
    
            textField_weld[i] = new JTextField();
            textField_weld[i].setBounds(210, 17+i*26, 130, 21);
            panel_weld.add(textField_weld[i]);
            textField_weld[i].setColumns(10);
            textField_weld[i].setHorizontalAlignment(JTextField.RIGHT);
        }
    
        lblNewLabel_weld[0].setText("");
        lblNewLabel_weld[1].setText("2");
    
        textField_weld[0].setText("-250");
        textField_weld[1].setText("250");
    
    /*
        ImgBox=new JLabel(images[18]);
        ImgBox.setBounds(P_1st_raw,img_p_y,img_s_x,img_s_y);
        contentPane.add(ImgBox);
        */
    
        JButton btnNewButton = new JButton("execute");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
    
                System.out.println("버튼이 클릭되었습니다.");
                new IOtest3();
    
    
            }
        });
        btnNewButton.setBounds(P_3rd_raw+50, 405, 97, 23);
        contentPane.add(btnNewButton);
    
    
        JButton btnNewButton_1 = new JButton("Analysis");
        btnNewButton_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
    
    
            }
        });
        btnNewButton_1.setBounds(P_3rd_raw+170, 405, 97, 23);
        contentPane.add(btnNewButton_1);
    
    
        JButton btnNewButton_2 = new JButton("Open Mentat");
        btnNewButton_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
    
    
            }
        });
        btnNewButton_2.setBounds(P_3rd_raw+50, 435, 97+120, 23);
        contentPane.add(btnNewButton_2);
    
    
    
        JMenuBar menuBar = new JMenuBar();
        menuBar.setBounds(0, 1, Frame_size_x, 21);
        contentPane.add(menuBar);
    
    
        JMenu File = new JMenu("File");
    
        File.add(new JMenuItem("New"));
        File.add(new JMenuItem("Open"));
        File.add(new JMenuItem("Save"));
        File.add(new JMenuItem("Save as"));
        File.add(new JMenuItem("Exit"));
    
    
        menuBar.add(File);
    
    
    
    
    }
    

    }

답변을 하려면 로그인이 필요합니다.

Hashcode는 개발자들을 위한 무료 QnA 사이트입니다. 계정을 생성하셔야만 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)

ᕕ( ᐛ )ᕗ
로그인이 필요합니다

Hashcode는 개발자들을 위한 무료 QnA사이트 입니다. 계정을 생성하셔야만 글을 작성하실 수 있습니다.