如何只将CSV中的唯一值添加到ComboBox中? [英] How to add only unique values from CSV into ComboBox?

查看:123
本文介绍了如何只将CSV中的唯一值添加到ComboBox中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想阅读一篇csv文件,并将Jakarta和Bandung放在一个组合框中。这是输入

I want to read a csv File and put words " Jakarta " and " Bandung " in a combobox. Here's the input

id,from,
1,Jakarta
2,Jakarta
5,Jakarta
6,Jakarta
10,Bandung
11,Bandung
12,Bandung

我设法得到了单词并将其放入组合框中,但正如您所看到的,文本文件本身包含很多单词Jakarta和Bandung,而我只想显示两者一次在组合框中。

I managed to get the words and put it in the combobox, but as you can see, the text file itself contains a lot word " Jakarta " and " Bandung " while i want to show both only once in the combobox.

这是我的临时代码,现在有效,但效率低,如果单词有更多种类可能无法使用

Here's my temporary code, which works for now but inefficient and probably can't be used if the word has more variety

public String location;

private void formWindowOpened(java.awt.event.WindowEvent evt) {

    String csvFile = "C:\\Users\\USER\\Desktop\\Project Data.csv";
    BufferedReader br = null;
    LineNumberReader reader = null;
    String line = "";
    String cvsSplitBy = "-|\\,";

        br = new BufferedReader(new FileReader(csvFile));
        reader = new LineNumberReader(new FileReader(csvFile));


        while ((line = br.readLine()) != null) {

            // use comma as separator

            String[] bookingdata = line.split(cvsSplitBy);

            location = bookingdata[1];
            ComboBoxModel model = cmb1.getModel();
            int size = model.getSize();

            cmb1.addItem(location);

            for(int i = 1; i < size; i++){

                if(model.getElementAt(i).equals("from")){
                    cmb1.removeItemAt(i);
                }

                else if(model.getElementAt(i).equals("Bandung")){
                    cmb1.removeItemAt(i);
                }


                for(int j = 2; j < i; j++){
                    if(model.getElementAt(j).equals("Jakarta")){
                        cmb1.removeItemAt(j);
                    }
                }
           }
       }
}

其他人推荐这种方法

boolean isEquals = false;
for(i = 0; i < a && !isEquals; i++){
   isEquals = location.equals("Jakarta");
   if(isEquals){
      cmb1.addItem("Jakarta");
   }
}

此代码不起作用。由于代码一旦添加雅加达就不会停止,但它在完成循环后停止。因此它仍然在组合框内创建副本。

This code doesn't work. As the code doesn't stop once it adds a " Jakarta " but it stops after it completed a loop. thus it still creates duplicate within the combobox.

我想知道是否有任何其他代码我可以尝试。谢谢

I would like to know if there's any other code i can try. Thank you

推荐答案

首先尝试将所有单词放入Set中,然后将其添加到组合框中。设置本身将照顾每个单词的确切一次出现。

Try putting all the words in a Set first and then add it in the combobox. Set itself will take care of exact one occurrence of each word.

这样的事情:

    while ((line = br.readLine()) != null) {

        // use comma as separator

        String[] bookingdata = line.split(cvsSplitBy);

        location = bookingdata[1];
        ComboBoxModel model = cmb1.getModel();
        int size = model.getSize();
        // add all location in set and set will only allow distinct values
        locationSet.add(location);

       }
       // after looping through all location put it in combobox
       for(String location:locationSet)cmb1.addItem(location);
   }
  }

如评论中所述,集合旨在保持唯一值。请在下面找到JShell的屏幕截图:

As discussed in comments, Sets are meant to keep unique values. Please find the screenshot of JShell below:

PS:这只是为了提出一个想法,可能需要根据要求进行一些修改。

PS: This is just to give an idea and may need some amendment as per requirement.

- 编辑 -

正如所讨论的,似乎你仍然缺少一些东西,我试着写下一段代码并且工作正常

As discussed, it seems you are still missing something, I tried and write below piece of code and worked fine

package com.digital.core;

import java.util.HashSet;
import java.util.Set;

import javax.swing.JComboBox;
import javax.swing.JFrame;

public class Test {

    public static void main(String[] args) {
       JFrame jframe = new JFrame();
       jframe.setSize(300, 300);
       String data = "id,from,\n" + 
            "1,Jakarta\n" + 
            "2,Jakarta\n" + 
            "5,Jakarta\n" + 
            "6,Jakarta\n" + 
            "10,Bandung\n" + 
            "11,Bandung\n" + 
            "12,Bandung";
       String[] dataArr = data.split("\n");

       Set<String> locationSet = new HashSet<>();
       for(String line:dataArr) {
           locationSet.add(line.split(",")[1]);
       }
       JComboBox<String> comboBox = new JComboBox<>();
       for(String location:locationSet)
       comboBox.addItem(location);
       jframe.add(comboBox);
       jframe.setVisible(true);


    }


}

这篇关于如何只将CSV中的唯一值添加到ComboBox中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆