public static int parseInt(String s)
                    throws NumberFormatException

解决方案 »

  1.   

    你的这些数据来自文件,是按文件里的顺序读出来的,你想排序的话,那得把这些数据加入到一个Collection再用Collections.sort();来排序
      

  2.   

    那怎么把数据加入到Collection中呢?谢谢
      

  3.   

    int[] arr = new int[st.countTokens()];
        int index=0;
        while (st.hasMoreTokens()){
            arr[index++] = Integer.parseInt(st.nextToken());
        }
      

  4.   

    你的文件:t.txt 如下
    12 56 3 15 8 20 9 17
    16 19 2 78 95 61 32======================================
    Sort.java如下:import java.io.*;
    import java.util.*;public class Sort{
    public Sort() throws IOException, 
                         FileNotFoundException{
        
        ArrayList list = new ArrayList();       
    BufferedReader bf = new BufferedReader(
                    new FileReader("t.txt"));
    String line;
    while((line = bf.readLine()) != null){
    StringTokenizer st = new StringTokenizer(line," ");
    while(st.hasMoreTokens()){
    //添加到ArrayList
    list.add(st.nextToken());
    }
    }
    //对ArrayList排序
    Collections.sort(list, new Comparator(){
    public int compare(Object o1, Object o2){
    return Integer.parseInt((String)o1)
            - Integer.parseInt((String)o2);
    }
    });
    System.out.println(list);
    }
    public static void main(String[] args){
         try{
         new Sort();
         }catch(Exception e){
         e.printStackTrace();
         }    
        }
    }
      

  5.   

    List<Integer> li = new ArrayList<Integer>();
    while((line = in.readLine()) != null){
        String[] sNum = line.split("\\s*,\\s*");
        for (String str: sNum){
            li.add(Integer.parseInt(str));
        }
    }
    Collections.sort(li);
    Integer[] arr = new Integer[0];
    arr = li.toArray(arr);