Java类型的非法启动

时间:2014-04-03 02:08:33

标签: 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;

   }
}

知道什么可能导致此错误吗?

1 个答案:

答案 0 :(得分:3)

菱形运算符(<>)仅在Java 7中引入。您应该明确指定类型参数(new MyQueue<String>(15)),您的教师将能够编译它。

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