使用循环Java将字符替换为子字符串

时间:2017-03-14 00:26:05

标签: java arrays loops replace replaceall

我正在尝试使用循环和数组替换两个括号之间的每个实例。 array1a和array1b是括号打开和关闭的索引。我想获取两个括号之间的数字并将其递增1并替换当前的值,但因为字符串文本当前是一个列表(例如“list item(0)list item(10)list item(1023)” “我想使用循环来增加每个值,而不是将括号内的所有值设置为相同的值。我希望这是有道理的!

String text = myString.getText();

for (int x = 0; x < 10; x++) {
                array2[x] = text.substring(array1a[x], array1b[x]);
                array2[x] = array2[x] + 1;
                array3[x] = "(" + array2[x] + ")";
                String text2 = text.replaceAll("\\(.*\\)", array3[x]);
                myString.setText(text2);
            }

完整代码:

public class CreateVideoList extends JFrame implements ActionListener {
    JButton play = new JButton("Play Playlist");
    JButton addVideo = new JButton("Add Video");
    TextArea playlist = new TextArea(6, 50);
    JTextField videoNo = new JTextField(2);
    private int x = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        String key = videoNo.getText();
        String name = VideoData.getName(key);
        String director = VideoData.getDirector(key);
        Integer playCount = VideoData.getPlayCount(key);
        String text = playlist.getText();
        String rating = CheckVideos.stars(VideoData.getRating(key));
        String output = name + " - " + director + "\nRating: "
                + rating
                + "\nPlay Count: " + playCount;
        String newItem = key + " " + name + " - " + director + " ("
                + playCount + ") " + "\n";
        String addToList = "";
        String[] array3 = new String[100];

        if ("Add Video".equals(e.getActionCommand())) {
            if (Character.isDigit(text.charAt(0)) == false) {
                playlist.setText("");
            }
            if (addToList.indexOf(key) == -1) {
                addToList += addToList + newItem;
                playlist.append(addToList);
                array3[x] = key;
                x++;
            } else if (addToList.indexOf(key) != -1) {
                JOptionPane.showMessageDialog(CreateVideoList.this,
                        "This video is already in the playlist. Please select a"
                        + " different video.", "Add to playlist error", JOptionPane.INFORMATION_MESSAGE);
            }
        }

        if ("Play Playlist".equals(e.getActionCommand())) {
            Integer length = (text.length());
            int counta = 0;
            Integer[] array1a = new Integer[100];
            Integer[] array1b = new Integer[100];
            String strPlayCount = "";

            for (x = 0; x < length; x++) {
                if (text.charAt(x) == '(') {
                    counta++;
                    array1a[counta - 1] = x;
                    array1a[counta - 1] = array1a[counta - 1] + 1;
                }
                if (text.charAt(x) == ')') {
                    array1b[counta - 1] = x;
                    array1b[counta - 1] = array1b[counta - 1];
                }
            }
            String[] array2 = new String[counta];
            String[] array4 = new String[100];

            for (int y = 0; y < counta; y++) {
                array2[y] = text.substring(array1a[y], array1b[y]);
                array2[y] = array2[y] + 1;
                playCount = Integer.parseInt(array2[y]);
                array4[y] = "(" + array2[y] + ")";
                String text2 = text.replaceAll("\\(.*\\)", array4[y]);
                playlist.setText(text2);
            }
        }
}

1 个答案:

答案 0 :(得分:0)

替换

array2[x] = array2[x] + 1;
array3[x] = "(" + array2[x] + ")";

Integer n = Integer.parseInt(array2[x]) + 1;
array3[x] = "(" + n.toString() + ")";