java - Java 类型的非法开始

标签 java compiler-errors queue

我有一个菜单驱动的程序允许用户在队列中添加、删除和显示一个人的名字。

我的程序编译和运行对我来说是完美的。 但是,当我的讲师测试它时,他说它不会编译并收到此错误?:

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 中引入。您应该明确指定类型参数 ( new MyQueue<String>(15) ),您的教师将能够对其进行编译。

您可以在此处找到解释 - Java SE 7 Features and Enhancements - Type Inference for Generic Instance Creation .

关于java - Java 类型的非法开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22826547/

相关文章:

java - 无法使用 Spring 创建 java.util.concurrent.ThreadPoolExecutor 的 bean

java - DefaultTableModel 的 dataVector 的克隆问题

c - Windows 上的 GCC 创建不可删除的文件

c++ - 收到错误,但在 '{' token C++之前预期的unqualified-id

c++ - 为什么空队列的大小在 pop 后会减一?

java - 用于在表单中填充今天日期的链接

java - 使用 View 寻呼机的 TabLayout 图标

scala - 仅对 “diverging implicit expansion”记录隐式日志

c - 我需要这个队列来显示学生 ID 和姓名,我该如何解决这个问题?

c - 函数似乎返回正确的值,但外部调用者永远不会在 C 中获取它