打印出.txt文件

时间:2014-10-28 19:53:13

标签: java arrays

我有一个简单的问题我有一个程序我用java编写,它需要一个.txt文件读取里面的东西(这种情况下它是一堆数字)程序将它存储在一个数组中,然后它排序它从最小到最大它也找到平均值,它告诉我阵列中有多少数字大于平均值。我已经设法完成了所有这些,但我遇到的麻烦现在我希望程序打印出另一个.txt文件,但这次是我的程序的结果。我希望它打印排序的数组平均数组中的元素数以及大于平均数的数字。这是我的代码:

import java.util.Scanner;
import java.util.Arrays;


class numberSorter{


   public static void main (String[]args)throws Exception{
   //calling the .txt file
      java.io.File file= new java.io.File("numTestData.txt");
      java.io.File file2=new java.io.File("dataOut.txt");
      Scanner input = new Scanner(file);

      //getting the numbers from the file   
      int num=input.nextInt();
      //starting variables
      int ct=0;
      int bigger=0;
      double average;
      double track=0;
      double[]numberArray=new double[num];
     //filling in the rest of the numbers in the array
      for(int i =0; i < numberArray.length; i++){

         numberArray[i] = input.nextInt();
      }
       input.close();  

      //calling the sort method to sort the array
      sort(numberArray);
      //tracking how many elements are in the array
      for(int i=0;i<numberArray.length;i++){

         track+=numberArray[i];

      }
      //finding the average of the sorted array
      average=track/numberArray.length;
      //looking through the array to find which number is bigger than the average
      for(int i=0;i<numberArray.length;i++)
      {
         if(numberArray[i]>average)
            bigger++;
      }
      //checking to see of the .txt file exists
      if(file2.exists()){
            System.out.println("file exists");
            System.exit(0);
         }
         //creating a file
         try(
         java.io.PrintWriter output=new java.io.PrintWriter(file2);
         ){
         //printing out to the .txt file 
         output.println("Number of slots:");
         output.println(numberArray.length);
         output.println("sorted:");
          for(int i =0; i < numberArray.length; i++){

            output.println(numberArray[i]);

         }

         output.println("Average:");
         output.println(average);
         output.println("number of above average values: ");
         output.println(bigger);
        }

   }



   //sort method
   public static void sort(double[]arrange)
   {
      //looking for the smallest number
      for(int i=0;i<arrange.length-1;i++){
         double currentMin=arrange[i];
         int currentMinIndex=i;
         //checking to see if the current number is smaller or bigger
         for(int j=i+1;j<arrange.length;j++){
            if(currentMin>arrange[j]){
               currentMin=arrange[j];
               currentMinIndex=j;
            }
         }
         //will arrange the numbers if current number is not smaller
         if(currentMinIndex!=i){
            arrange[currentMinIndex]=arrange[i];
            arrange[i]=currentMin;
         }
      }

   }
}

现在我的问题是我一直收到这个错误,一切都符合,但是当我尝试运行它时,我遇到了这个:

 ----jGRASP exec: java numberSorter

Exception in thread "main" java.util.NoSuchElementException
    at java.util.Scanner.throwFor(Scanner.java:862)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at numberSorter.main(numberSorter.java:26)

 ----jGRASP wedge2: exit code for process is 1.
 ----jGRASP: operation complete.

我试过用代码来解决这个问题,但我还是继续得到任何帮助吗?我还在学习java

1 个答案:

答案 0 :(得分:0)

为了摆脱与第一行中不匹配的int相关的错误,我建议不要使用第一个int计算数字,你要解析。 所以你的输入文件是:

12345 23456 123 4567 123456 7654 999 3453 997733 43 654321

您可以使用ArrayList而不是数组,然后使用input.hasNext()遍历该文件。

我更新了代码中的部分:

import java.util.ArrayList;
import java.util.Scanner;

public class NumberSorter {

    public static void main(String[] args) throws Exception {
        // calling the .txt file
        java.io.File file = new java.io.File("numTestData.txt");
        java.io.File file2 = new java.io.File("dataOut.txt");
        Scanner input = new Scanner(file);

        int bigger = 0;
        double average;
        double track = 0;
        ArrayList<Double> numberArray = new ArrayList<Double>();
        while (input.hasNext()) {
            numberArray.add(input.nextDouble());
        }
        input.close();

        // calling the sort method to sort the array
        sort(numberArray);
        // tracking how many elements are in the array
        for (int i = 0; i < numberArray.size(); i++) {
            track += numberArray.get(i);
        }
        // finding the average of the sorted array
        average = track / numberArray.size();
        // looking through the array to find which number is bigger than the
        // average
        for (int i = 0; i < numberArray.size(); i++) {
            if (numberArray.get(i) > average)
                bigger++;
        }
        // checking to see of the .txt file exists
        if (file2.exists()) {
            System.out.println("file exists");
            System.exit(0);
        }
        // creating a file
        try (java.io.PrintWriter output = new java.io.PrintWriter(file2);) {
            // printing out to the .txt file
            output.println("Number of slots:");
            output.println(numberArray.size());
            output.println("sorted:");
            for (int i = 0; i < numberArray.size(); i++) {
                output.println(numberArray.get(i));
            }

            output.println("Average:");
            output.println(average);
            output.println("number of above average values: ");
            output.println(bigger);
        }
    }

    // sort method
    public static void sort(ArrayList<Double> arrange) {
        // looking for the smallest number
        for (int i = 0; i < arrange.size() - 1; i++) {
            double currentMin = arrange.get(i);
            int currentMinIndex = i;
            // checking to see if the current number is smaller or bigger
            for (int j = i + 1; j < arrange.size(); j++) {
                if (currentMin > arrange.get(j)) {
                    currentMin = arrange.get(j);
                    currentMinIndex = j;
                }
            }
            // will arrange the numbers if current number is not smaller
            if (currentMinIndex != i) {
                arrange.set(currentMinIndex, arrange.get(i));
                arrange.set(i,currentMin);
            }
        }
    }
}