简体   繁体   English

Java类型的非法启动

[英]illegal start of type Java

I have a menu-driven program allows the user to add, remove and display the name of a person in a queue. 我有一个菜单驱动的程序允许用户添加,删除和显示队列中的人的名字。

My program compiles and runs perfectly for me. 我的程序编译并运行完美。 However, when my instructor tested it, he stated it won't compile and receives this error?: 但是,当我的导师测试它时,他说它不会编译并收到此错误?:

QueueProgram.java:24: illegal start of type

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

My code: 我的代码:

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;

   }
}

Any idea what could cause this error? 知道什么可能导致这个错误吗?

The diamond operator ( <> ) was introduced in Java 7 only. 菱形运算符( <> )仅在Java 7中引入。 You should specify the type argument ( new MyQueue<String>(15) ) explicitly and your instructor will be able to compile it. 您应该明确指定类型参数( new MyQueue<String>(15) ),您的教师将能够编译它。

You can find the explanation here - Java SE 7 Features and Enhancements - Type Inference for Generic Instance Creation . 您可以在此处找到解释 - Java SE 7功能和增强 - 通用实例创建的类型推断

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

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