简体   繁体   English

Java 编译没有错误,运行第一行,然后不继续或结束

[英]Java compiles with no errors, runs first line, then doesn't continue or end

I am working on a project for my intro to java class and I can't get it to continue.我正在为我介绍 java class 做一个项目,但我无法继续。 The first part of the project is supposed to use a method to read a file and turn it into an array, then retrieve that array in the main method.项目的第一部分应该使用一种方法来读取文件并将其转换为数组,然后在 main 方法中检索该数组。 It compiled with no errors, but when I run it, it prompts the user for the file name and when you type it and press enter it just sits there.它编译时没有错误,但是当我运行它时,它会提示用户输入文件名,当您键入它并按 Enter 时,它就在那里。 It does not end the program but it also doesn't continue.它不会结束程序,但也不会继续。 Am I doing something wrong?难道我做错了什么?

Here is the method that reads the file and creates the array:这是读取文件并创建数组的方法:

   public static int[] inputData() throws IOException
   {
      Scanner kb = new Scanner(System.in);
      System.out.print("Enter input filename: ");
      String fileName = kb.nextLine();
      File file = new File(fileName);
      if(!file.exists())
      {
         System.out.println("File not found.");
         System.exit(0);
      }
      Scanner inFile = new Scanner(file);

      int size = 0;
      while(inFile.hasNext())
      {
         size++;
      }

      int[] array = new int[size];
      while(inFile.hasNext())
      {
         for(int i = 0; i < size; i++)
         {
            array[i] = inFile.nextInt();
         }
      }
      inFile.close();
      return array;
   }

and here is the main method:这是主要方法:

   public static void main(String[] args) throws IOException
   {
      int[] array = inputData();
      int length = array.length;
      System.out.println("Original array: ");
      printArray(array, length);
      System.out.println();

      System.out.print("The first element out of order is at index "
                       + outOfOrder(array));
      System.out.println(".");

      System.out.println("Reversed array: ");
      reverseArray(array);

      System.out.println("Sorted array in descending order: ");
      selectionSort(array);

      System.out.println("Delete repeats: ");
      deleteRepeats(array);
   }

I am very new to java so I apologize if this is an obvious error.我对 java 很陌生,所以如果这是一个明显的错误,我深表歉意。 Please help, thank you!请帮忙,谢谢!

Loop in your code will never complete as you are not using inFile.next() inside the loop.你的代码中的循环永远不会完成,因为你没有在循环中使用 inFile.next() 。

while(inFile.hasNext()){
    size++;
}

Remove the two while cycles and add this:删除两个while循环并添加:

        int[] array = new int[1];
        int index= 0;
        while (inFile.hasNext()) {
            array[index] = inFile.nextInt();
            if(inFile.hasNext()) {
                index++;
                int[] array2 = new int[array.length + 1];
                for (int i = 0; i<array.length;i++) {
                    array2[i] = array[i];
                }
                array = array2;
            }
        }

This code will increase the size of array[] for each next element.此代码将为每个下一个元素增加 array[] 的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM