Java中类型非法的起始。

3
我有一个菜单驱动程序,允许用户在队列中添加、删除和显示人名。
我的程序对我来说编译和运行都完美无误。然而,当我的教练测试它时,他表示它无法编译,并收到以下错误消息:
QueueProgram.java:24: illegal start of type

      MyQueue<String> queue= new MyQueue<>(15);
                                         ^
1 error

我的代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JOptionPane;


public class QueueProgram {


    /**
     * Driver code to test class
     * 
     * @param arguments
     *            Commandline arguments not used
     * @throws IOException 
     */
   public static void main(String[] arguments) throws IOException {


    //Queue Object
      MyQueue<String> queue= new MyQueue<>(15);

      String name;
    //reading file
      read(queue,arguments[0]);

      String[] array = { "Offer Person", "Poll Person", "Peek person",
                  "Display Queue", "Exit Program"};
      int choice = 0;

         // display loop   
      while (choice != array.length-1) {
         choice = JOptionPane.showOptionDialog(null, // put in center of screen
                  "Press a Button", // message to user
                  "Queue(Line) of People", // title of window
                  JOptionPane.YES_NO_CANCEL_OPTION, // type of option
                  JOptionPane.QUESTION_MESSAGE, // type of message
                  null, // icon
                  array, // array of strings
                  array[array.length - 1]); // default choice (last one)


         if(choice==0){

                //inserting the new name in queue
            name=JOptionPane.showInputDialog(null,"Enter Person's name","Input");
            queue.offer(name);

         }
         else if(choice==1){

                //Display and remove the name which is at front of line
            JOptionPane.showMessageDialog(null, queue.poll() + " is next in line");

         }

         else if(choice==2){

                //Display name which is at front of line
            JOptionPane.showMessageDialog(null, queue.peek() + " is front of the line");

         }

         else if(choice==3){
                //Dispay all the list
            JOptionPane.showMessageDialog(null, queue.toString());


         }
               //JOptionPane.showMessageDialog(null, "Your pressed button #" + choice);
      }
    //calling writing function
      write(queue, arguments[1]);


   }// end of main()

   /**
     * Reads a file
     * @param queue 
     * @param file_name name of file
     */
   public static void read(QueueInterface<String> queue, String file_name) throws IOException{

      String name;
    //creating a buffer reader to read
      BufferedReader br= new BufferedReader(new FileReader(file_name));
      while((name=br.readLine()) != null){
        //putting in the queue
         queue.offer(name);


      }
    //closing buffer reader
      br.close();
   }

   /**
     * Writes to file
     * @param queue QueueInterface methods
     * @param file_name name of file
     */
   public static void write(QueueInterface<String> queue, String file_name) throws IOException{
      String name;
    //creating a buffer writer to write
      BufferedWriter bw= new BufferedWriter(new FileWriter(file_name));
      while((name=queue.poll()) != null){
        //writin in file
         bw.write(name);
         bw.newLine();

      }
    //closing buffer
      bw.close();
   }



}// end of class



class MyQueue<T> extends ArrayQueue<T>{

   /**
     * Constructor
     * 
     * @param max is the greatest number of elements in the queue
     */
   public MyQueue(int max) {
      super(max);

   }

   /**
     * Returns a string representation of the object
     * 
     * @return a name on different lines
     */
   public String toString() {
    // create a variable
      String element = "";
      int count=frontIndex;
    // check to see if not empty
      if (!this.empty()) {

        // get the address of front element
         while(count<=endIndex){


            element = element +(String) array[count]+"\n";
            count++;
         }
      }
    // return either null or element
      return element;

   }
}

有任何想法是什么可能导致这个错误?

你的教师是否可能在使用早于Java 7的版本(在我们甚至可以使用Java 8的时代)? - Pshemo
很可能是Java的不同版本。不过这是一个教练应该知道的东西。 - Jeroen Vannevel
1个回答

3

谢谢Bhesh!还有解释的链接。那绝对是问题所在哈哈。 - Asia x3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接