调用磁盘文件程序时控制台错误

时间:2016-10-30 21:37:43

标签: java ioexception

我正在编写一个调用文件位置和文件名的程序,并将其转换为String数组。该程序编译但运行时它会返回控制台错误。我是java新手,我不知道如何解决这些控制台错误或它们发生的原因。如果有人可以帮助我,那就太好了。我用注释标记了两个错误。第一个错误在main方法中,另一个在bubblesort方法中。

编辑: 这是我收到的错误,

线程“main”java.lang.NullPointerException中的异常     at java.lang.String.compareTo(Unknown Source)     在Assignment7.bubbleSort(Assignment7.java:63)     在Assignment7.main(Assignment7.java:21)

当我点击它时,它们会引导我到下面我指出的地方。

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

public class program3 {

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


    String[] list2, targets, list1;
    list1 = getInput("C:\\Desktop\\input1.txt");    
    list2 = bubbleSort(list1);                      //Console error here
    targets = getInput("C:\\Desktop\\targets1.txt");

    double seqAvg = seqSearch(list1, targets);
    double binAvg = binSearch(list2, targets);
    System.out.println("Average number of searches for the seqeuntial search is " + seqAvg);
    System.out.println("Average number of searches for the binary search is " + binAvg);

}//end of main method

public static String[] getInput(String filename) throws IOException {
    String[] inputArr = new String[100];

    Scanner in = new Scanner(new File(filename));
    int count = 0;
    while(in.hasNext()) { 
        if (count < 100){
            inputArr[count++] = in.next();
        } else {
            break;
        }

        }       
    in.close();
    return inputArr;

}// end getInput method



 //This method will sort the array and pass it onto a list.
  public static String[]bubbleSort(String[] inputArr) {

    String[] Array2 = inputArr.clone();

    for (int i = 0; i<Array2.length; i++)
    {
        for (int j = 0; j<Array2.length; j++)
        {
           if (Array2[i].compareTo(Array2[i+1]) > 0)  //Console error here
            {
                String temp = Array2[i];
                Array2[i] = Array2[j];
                Array2[j] = temp;
            }
        }
    }
    return Array2;

}// End of sort method.

//This method will do a sequential search on the list1
public static double seqSearch(String[] list1, String[] targets){
      {
          for (int j = 0; j < list1.length; j++)
          {
              for(String str:targets){
              if(list1[j].equalsIgnoreCase(str)){
                  return j;
              }
          }
             {
                return j;
             }
         }
         return -1;
       }

}//end of sequentialSearch method


//This method will do a binary search on the list   
public static int binSearch(String[] list1, String[] targets) {
       int lo = 0;
        int hi = list1.length - 1;
        int mid = -1;

        while( lo <= hi ) {
          mid = (lo+hi)/2;

          for(String str:targets){
          if(list1[mid].equalsIgnoreCase(str)){
              return mid;
          }

          hi = mid-1;


          } for(String str:targets){
          if(list1[mid].equalsIgnoreCase(str)){
              return mid;
          }
      }

            lo = mid+1;
           {
            return mid;
          }
        }
        return mid;
      }

}//End of program3

1 个答案:

答案 0 :(得分:0)

你得到ArrayIndexOutOfBoundsException因为在这行的bubbleSort方法中:

if (Array2[i].compareTo(Array2[i+1]) > 0)

索引i + 1大于数组长度。 我建议你以这种方式改变冒泡排序方法中的循环:

for (int i = 0; i<Array2.length; i++)
{
  for (int j = 0; j<Array2.length-1; j++)
  {
      if (Array2[j].compareTo(Array2[j+1]) > 0)
      {
        String temp = Array2[j];
        Array2[j] = Array2[j+1];
        Array2[j+1] = temp;
      }
   }
}