Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在java中删除列表最后一个值之后的逗号_Java_Loops_Csv - Fatal编程技术网

无法在java中删除列表最后一个值之后的逗号

无法在java中删除列表最后一个值之后的逗号,java,loops,csv,Java,Loops,Csv,我使用下面的代码读取csv文件并以逗号分隔的格式列出其值,但我甚至在最后一个值之后得到逗号,这不是必需的 String strFile = "C:\\Users\\msudhera\\Desktop\\data.csv"; BufferedReader br = null; dto data = new dto(); String nextLine = ""; String[] country = null; List<String> list1 = null; String va

我使用下面的代码读取csv文件并以逗号分隔的格式列出其值,但我甚至在最后一个值之后得到逗号,这不是必需的

String strFile = "C:\\Users\\msudhera\\Desktop\\data.csv";
BufferedReader br = null;
dto data = new dto();
String  nextLine = "";
String[] country = null;
List<String> list1 = null;
String val = "";
        try {
            String list = null;
            br = new BufferedReader(new FileReader(strFile));
            while ((nextLine = br.readLine()) != null) {
                country = nextLine.split(",");

                list = country[0].toString();
                list1 = Arrays.asList(list.substring(1,9));
                String values2 = "";



                for (String values: list1)
                {
                    values2 = StringUtils.join("'"+values+"'", ',').toString();
                    val = data.setVal(values2); 
                    System.out.println(data.getVal());
                }

            }
        }
        catch (Exception e){
            System.out.println("caught Exception:" + e.getMessage());
        }`
String strFile=“C:\\Users\\msudhera\\Desktop\\data.csv”;
BufferedReader br=null;
dto数据=新的dto();
字符串nextLine=“”;
字符串[]country=null;
List list1=null;
字符串val=”“;
试一试{
字符串列表=空;
br=新的BufferedReader(新的文件读取器(strFile));
而((nextLine=br.readLine())!=null){
country=nextLine.split(“,”);
列表=国家[0]。toString();
list1=Arrays.asList(list.substring(1,9));
字符串值2=“”;
用于(字符串值:列表1)
{
values2=StringUtils.join(“'”+values+“'”,“,”).toString();
val=data.setVal(值2);
System.out.println(data.getVal());
}
}
}
捕获(例外e){
System.out.println(“捕获的异常:+e.getMessage());
}`
正在打印:

'20170213',
'20170213',
'20170213',
'20170420',
‘20170509’,


请帮助解决此问题。

您调用了错误的重载联接方法。您正在调用接受字符串“var args”的方法,因此它只是将其连接起来

您需要调用一个带有分隔符的列表,如

StringUtils.join(theList, ","); //In your case the list is list1
另外,不要将字符串变量重命名为
list
,因为这会产生误导


编辑:我还看到,您需要在字符串的两边用单引号填充。如果不需要实际使用
StringUtils
,那么可以使用Java8流和收集来实现同样的功能

list1.stream()
    .map(element -> "'" + element + "'")
    .collect(Collectors.joining(","));

在获得最终解决方案之前,请尝试此快速修复:

 String arr[]= new String [list1.size()];

    for (int i=0;i< list1.size();i++)
  {
   values2 = StringUtils.join("'"+values+"'", ',').toString();
    val = data.setVal(values2);
     arr[i]=data.getVal();
                        //System.out.println(data.getVal());
                    }
    String temp= Arrays.toString(arr);
    temp= temp.replace("[","");
    temp= temp.replace("]","");

    StringBuilder mystring = new StringBuilder(temp);
    mystring.setCharAt(temp.length()-1, '');

    System.out.println(String.valueOf(mystring));
String arr[]=新字符串[list1.size()];
对于(int i=0;i
您使用的是哪些StringUtils.org.apache.commons.lang3.StringUtils。。在这种情况下,我通常使用s.substring(0,s.length()-1)或s.replaceAll(“,”,”)…但在这种情况下,我确信不会有任何问题。因此,我使用了`list1.stream().forEach(elem->System.out.println(StringUtils.join(elem,“,”);`但是我仍然得到相同的输出。@MohitRaja您需要传递列表,因为它是连接的,而不是单个字符串。如果您这样做,它将把分隔符视为null,并将元素和“,”连接起来。我尝试使用更新的代码,但没有得到逗号分隔的值。您可以给出示例数据吗?P20170213070008000034 P20170213070008000035 P20170213070008000036 P20170420075000380000018 P20170509083000270000008这是csv文件中的值列表