Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.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 解析float fom文本文件并按降序排序_Java_Arrays_Parsing_Sorting_Text - Fatal编程技术网

Java 解析float fom文本文件并按降序排序

Java 解析float fom文本文件并按降序排序,java,arrays,parsing,sorting,text,Java,Arrays,Parsing,Sorting,Text,我需要一些整理文本行的帮助 Kazimira Danutė PRUNSKIENĖ : 3,86% Dalia GRYBAUSKAITĖ : 68,21% Valdemar TOMAŠEVSKI : 4,68% Algirdas BUTKEVIČIUS : 11,68% Valentinas MAZURONIS : 6,08% Česlovas JEZERSKAS : 0,66% Loreta GRAUŽINIENĖ : 3,57% 我需要按数字对这个文本文档进行排序 Dalia GRYBAUS

我需要一些整理文本行的帮助

Kazimira Danutė PRUNSKIENĖ : 3,86%
Dalia GRYBAUSKAITĖ : 68,21%
Valdemar TOMAŠEVSKI : 4,68%
Algirdas BUTKEVIČIUS : 11,68%
Valentinas MAZURONIS : 6,08%
Česlovas JEZERSKAS : 0,66%
Loreta GRAUŽINIENĖ : 3,57%
我需要按数字对这个文本文档进行排序

Dalia GRYBAUSKAITĖ : 68,21%
Algirdas BUTKEVIČIUS : 11,68%
Valentinas MAZURONIS : 6,08%
Valdemar TOMAŠEVSKI : 4,68%
Kazimira Danutė PRUNSKIENĖ : 3,86%
Loreta GRAUŽINIENĖ : 3,57%
Česlovas JEZERSKAS : 0,66%
最好的方法是什么

           Reader reader = new InputStreamReader( new FileInputStream(desktopPath + "/RINKIMAI/INFO.txt"), "UTF-8");
           @SuppressWarnings("resource")
           BufferedReader br = new BufferedReader(reader);

          java.util.List<Float> allMatches = new ArrayList<Float>();
           String word = null;
           while((word =br.readLine()) != null){
               Pattern p = Pattern.compile("\\d+,\\d+");
               Matcher m = p.matcher(word); 
               while (m.find()) {
                   allMatches.add(Float.parseFloat(m.group()));   
               }
               Arrays.sort(allMatches);
           }
Reader Reader=新的InputStreamReader(新文件InputStream(desktopath+“/RINKIMAI/INFO.txt”),“UTF-8”);
@抑制警告(“资源”)
BufferedReader br=新的BufferedReader(读卡器);
java.util.List allMatches=new ArrayList();
字符串字=null;
while((word=br.readLine())!=null){
模式p=模式。编译(\\d+,\\d+);
匹配器m=p.Matcher(字);
while(m.find()){
add(Float.parseFloat(m.group());
}
Arrays.sort(所有匹配项);
}
我无法将所有解析的信息放入数组,因为它是浮点型的,无法找到解决方案。。在降序中对所有信息进行排序后,是否仍然可以返回降序中包含所有信息的完整行?

您需要使用

数字格式

集合。排序

像这样

public static void main(String[] args) throws NumberFormatException, IOException, ParseException {
     Reader reader = new InputStreamReader( new FileInputStream("test.txt"), "UTF-8");
     @SuppressWarnings("resource")
     BufferedReader br = new BufferedReader(reader);
     NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);

    java.util.List<Float> allMatches = new ArrayList<Float>();
     String word = null;
     while((word =br.readLine()) != null){
         Pattern p = Pattern.compile("\\d+,\\d+");
         Matcher m = p.matcher(word); 
         while (m.find()) {
             Number number = format.parse(m.group());
             allMatches.add(number.floatValue());   
         }
//         Arrays.sort(allMatches);
         Collections.sort(allMatches);
     }

     for (Float float1 : allMatches) {
        System.out.println(float1);
    }
}
publicstaticvoidmain(String[]args)抛出NumberFormatException、IOException、ParseException{
Reader Reader=新的InputStreamReader(新文件InputStream(“test.txt”),“UTF-8”);
@抑制警告(“资源”)
BufferedReader br=新的BufferedReader(读卡器);
NumberFormat format=NumberFormat.getInstance(Locale.FRANCE);
java.util.List allMatches=new ArrayList();
字符串字=null;
while((word=br.readLine())!=null){
模式p=模式。编译(\\d+,\\d+);
匹配器m=p.Matcher(字);
while(m.find()){
Number=format.parse(m.group());
allMatches.add(number.floatValue());
}
//Arrays.sort(所有匹配项);
Collections.sort(所有匹配项);
}
for(Float float1:allMatches){
系统输出打印项次(float1);
}
}

您需要创建一个包含名称和浮点字段的类,并实现
Comparable
。您需要将字符串和浮点作为单个条目添加,并使用自定义比较器单独对浮点进行排序。顺便说一句,以防您不必在Java中使用它,只需对文件进行排序,您可以在Bash:
sort-t':'-k2,2-nr myfile.txt中执行此操作,因此我现在使用的方法不正确。我从来没有使用过比较器之类的东西,我不知道如何使用它。对浮点值进行排序后,是否有可能恢复所有字符串信息?在这种情况下,您需要将所有信息以对象的形式放置。对象将有一个存储文本数据的字符串和另一个保存数字的float属性。然后需要实现自定义比较器,并将其传递给collections.sort