2012-12-09 30 views
0
package test; 

    /* Read numbers from a text file and store them into an array; then 
    sort the array and display it on the screen. Save the sorted array 
in sorted.txt. */ 

import java.util.Scanner; 
import java.io.*; 

public class Test { 

static public void main(String[] args) throws IOException { 

    /* Initialize the input stream reading from a text file */ 

Scanner inputFile; 
    inputFile = new Scanner(new File("arrayex1.txt")); 

    /* Initialize the output stream writing into a text file */ 

PrintWriter outputFile; 
outputFile = new PrintWriter(new FileWriter("sorted.txt")); 

    /* Declare the array */ 

int[] numbers = new int[50]; 
int index = 0; 

    int temp, smallest, smallest_index; 

    /* Read the first number */ 
    numbers[index] = inputFile.nextInt(); 

    while (numbers[index] != 0) { 

     index++; 
     numbers[index] = inputFile.nextInt(); 

    } 

    /* Sort the array using the selection sort method; the inner 
     loop finds the smallest unsorted number, and the outer 
     loop places it in the right place. */ 

    for (int i = 0; i < index - 1; i++) { 

     smallest = numbers[i]; 
     smallest_index = i; 

     for (int j = i + 1; j < index; j++) { 

     if (numbers[j] < smallest) { 
       smallest = numbers[j]; 
       smallest_index = j; 
      } 
     } 

     /* If needed switch numbers[i] and numbers[smallest_index] */ 

     if (numbers[i] != numbers[smallest_index]) { 

      temp = numbers[i]; 
      numbers[i] = numbers[smallest_index]; 
      numbers[smallest_index] = temp; 
     } 
    } 

    /* Display the sorted array and also save the result in sorted.txt*/ 

    for (int i = 0; i < index; i++) {   
    System.out.print (numbers[i] + " "); 
     outputFile.print (numbers[i] + " "); 
    } 

    inputFile.close(); 
    outputFile.close(); 

} 
    } 

这里是Errror得到:我的Java代码将不会编译期我有一个错误,我不明白为什么

Exception in thread "main" java.util.NoSuchElementException 
at java.util.Scanner.throwFor(Unknown Source) 
at java.util.Scanner.next(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at java.util.Scanner.nextInt(Unknown Source) 
at test.Test.main(Test.java:37) 

请任何帮助和解决方案,将不胜感激。提前致谢。

+1

您的代码不会按照您希望的方式运行,但它会编译! – jlordo

+0

是的...这是假设排序一个txt文件....但它给出了错误 –

回答

1

始终通过调用Scanner.hasNext()来检查扫描仪是否具有下一个元素,而不是跳转到Scanner.Next()

显示错误是因为扫描仪到达最后一个元素,并且没有其他人再次显示。

+0

我试过你的建议...它仍然无法正常工作。 –

+0

如果它“不起作用”,这意味着你做错了什么。如果你想更有用的反馈,你需要提供有用的信息。 –

0

首先,由于我猜测每次都不会有相同数量的数字进行排序,因此我建议使用整数的ArrayList。

下面是方法读,和进口表现:

private static ArrayList<Integer> nums; 
    public static void readIn() throws FileNotFoundException{ 
     Scanner sc = new Scanner(new File("file.txt")); 
     boolean error = false; 
     while(sc.hasNext()&&!error){ 
      try{ 
       nums.add(Integer.parseInt(sc.next())); 
      }catch(NumberFormatException e){ 
       e.printStackTrace(); 
       error = true; 
      } 
     } 
    } 

这是可能是最有效的排序的代码。

 int[] numbers = (int[]) nums.toArray(); 
     Arrays.sort(numbers); 
     System.out.println(Arrays.toString(numbers)); 
相关问题