从文本文件中读取并递归合并排序

时间:2014-10-26 07:26:05

标签: java filereader mergesort

我知道这个问题似乎是重复的,但我一遍又一遍地看着每一个相关问题而无法找到问题的答案。我应该从一个文本文件中读取,该文件的名称由用户输入确定。该文件包含未排序的双重数字集,例如:

" 0.1212312" " 0.0013315" " 0.0106026"

然后我被提示通过递归方式合并排序。我的程序在编译方面没有问题,并且我没有得到任何运行时错误,但程序没有正确响应某些输入,我似乎无法弄清楚原因。所以我要求我的senpais' assitance。这是我的代码:

import java.io.*;
import java.nio.file.Path;
import java.util.*;
import java.lang.*;
public class ProjectAssingment3 
{
    public static void main(String[] args) throws IOException {
        System.out.println("Enter file name please:");
        Scanner input;
        input = new Scanner(System.in);
        String in;
        in = input.nextLine();
        int count = 0;
        for(int i = 0; i < in.length(); i++)
        {
            if(Character.isWhitespace(in.charAt(i)))count++;
        }
        while(in.equals(null)||(count>1))
        {
            count=0;
            for(int i = 0; i < in.length(); i++) // Counts the white spaces to determine the number of arguments in input
            {
                if(Character.isWhitespace(in.charAt(i)))
            count++;
            }
            System.out.println("Invalid input! Please try again!");
            input = new Scanner(System.in);
            in = input.nextLine();
        }
        try {
            Scanner sc = new Scanner(new FileReader(new File(in)));
            List<Double> lines = new ArrayList<Double>();
            int i =0;
            while (sc.hasNextLine()) 
            {
            if(sc.nextLine().equals(null))//Checks if any empty lines
            {
                System.out.println("Empty Line Encountered!");
                System.exit(-1);
            }
            else if(!sc.hasNextDouble())//Checks if any non-double type lines
            {
                System.out.println("Invalid Line Encountered" + sc.nextLine());
                System.exit(-1);
            }
            lines.add(Double.parseDouble(sc.nextLine()));
            System.out.println(lines.get(i));i++; //Prints out original Unsorted array.
        }
        sc.close();

        //Unboxes the values from the ArrayList onto a array of type double
        double arr[] = new double[lines.size()];
        for(int j=0;j<arr.length;j++)
        {
            arr[j] = lines.get(j);
        }
        mergeSort(arr, 0, arr.length - 1);

        //Prints the sorted array
        for(int x=0;x<arr.length;x++)
        {
            System.out.println(arr[x]);
        }
    } catch (IOException e) {
        System.out.print("Failed to read input file:" + e.getMessage());
    }
}
public static void doMerge(double [] numbers, int left, int mid, int right)
{
  double [] temp = new double[25];
  int i, left_end, num_elements, tmp_pos;  
  left_end = (mid - 1);
  tmp_pos = left;
  num_elements = (right - left + 1);
  while ((left <= left_end) && (mid <= right))
  {
        if (numbers[left] <= numbers[mid])
          temp[tmp_pos] = numbers[left++];
        else
          temp[tmp_pos++] = numbers[mid++];
        }
        while (left <= left_end)
          temp[tmp_pos++] = numbers[left++];
        while (mid <= right)
          temp[tmp_pos++] = numbers[mid++];
        for (i = 0; i < num_elements; i++)
        {
          numbers[right] = temp[right];
          right--;
        }
    }
    public static void mergeSort(double [] numbers, int left, int right)
    {
    int mid;
    if (right > left)
    {
      mid = (right + left) / 2;
      mergeSort(numbers, left, mid);
      mergeSort(numbers, (mid + 1), right);
      doMerge(numbers, left, (mid+1), right);
    }
}

}

是的,这确实是给我的一项任务,但是老实说,我并没有要求你们中的任何人为我做这件事。我真的很蠢,只是要求澄清一下!

我很感激为我这个愚蠢的问题付出的努力!

1 个答案:

答案 0 :(得分:0)

当我提供完整路径时,它接受该文件,但又给出了另一个例外。

你也抛出异常,但是谁抓住了它?

最后一点,main的语法是public static void main(String[] args)必须得到String[]args作为参数,(至少在eclipse中),否则它不会识别主方法