Похоже, моя программа работает так, как мне нравится. Однако при компиляции я получаю следующее сообщение:
Note: Program.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Что я могу сделать, чтобы определить небезопасные операции с -Xlint или что в программе вызывает это сообщение? Я думаю, это как-то связано с моим классом Node..?
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JOptionPane;
/**
* An application that reads from a file, enters/deletes in queue and writes output to the file
*/
public class Program {
/**
* Driver code to test class
*
* @param arguments
* Commandline arguments. 1st argument is input file and 2nd argument is output file
* @throws IOException
*/
public static void main(String[] arguments) throws IOException {
//Queue Object
MyQueue queue= (new MyQueue());
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 queue, String file_name) throws IOException{
try
{
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();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
/**
* Writes the contents of LinkedQueue to the output file at the ned of program
* @param queue QueueInterface methods
* @param file_name name of file
*/
public static void write(QueueInterface queue, String file_name) throws IOException{
try
{
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();
}
catch(Exception ex)
{
System.err.println(ex.getMessage());
}
}
}// end of class
/**
* Interface to be implemented by LinkedQueue
*/
interface QueueInterface
{
public boolean empty();
public boolean offer(String element);
public String poll();
public String peek();
}
class Node
{
private String data;
private Node nextNode;
public Node(String dataObject, Node nextNodeObject)
{
this.data=dataObject;
this.nextNode=nextNodeObject;
}
/**
* Gets the next node
* @return next node
*/
public Node getNext()
{
return nextNode;
}
/**
* Sets the next node of the current node
* @param nextNodeObject next node to be set as next to the current node
*/
public void setNext(Node nextNodeObject)
{
nextNode=nextNodeObject;
}
/**
* Sets data of the current node
* @param dataObject data to be inserted in new node
*/
public void setData(String dataObject)
{
this.data=dataObject;
}
/**
* Gets data of the current node
* @return data of the node
*/
public String getData()
{
return this.data;
}
}
class LinkedQueue implements QueueInterface
{
protected Node lastNode=null;
LinkedQueue() {
}
/**
* Checks if the queue is empty
* @return true if empty, false if not empty
*/
public boolean empty() {
if(lastNode==null)
{
return true;
}
else
return false;
}
/**
* Inserts new node in the queue
* @param element data to be inserted in new node
* @return true on success
*/
public boolean offer(String element)
{
Node newLastNode = new Node(element,null);
//If the LinkedQueue is empty, add the node to the last and point next to itself
if(empty())
{
newLastNode.setNext(newLastNode);
}
else
{
// Adding to the front of queue and updating next of the last node
newLastNode.setNext(lastNode.getNext());
lastNode.setNext(newLastNode);
}
lastNode=newLastNode;
return true;
}
/**
* Removes the first node and returns it
* @return data at first node
*/
public String poll()
{
// If queue is empty then return null
if(empty())
return null;
else
{
Node frontNode = lastNode.getNext();
//Check if there will be no node left after polling this one
if (frontNode == lastNode)
{
lastNode = null;
}
else //Remove the first node and update next of the last node
{
lastNode.setNext(frontNode.getNext());
}
return frontNode.getData();
}
}
/**
* Returns data of the first node without removing it from the queue
* @return data at first node
*/
public String peek()
{
if (empty())
{
return null;
}
else
{
Node frontNode = lastNode.getNext();
return frontNode.getData();
}
}
}
class MyQueue extends LinkedQueue{
/**
* Constructor
*
*/
public MyQueue()
{
super();
}
/**
* Returns a string representation of the object
*
* @return a name on different lines
*/
public java.lang.String toString()
{
// create a variable to return
java.lang.String toReturn = "";
// Traversing the list
Node frontNode = lastNode.getNext();
if(empty()) //If queue is empty
return "";
else if(frontNode==lastNode) //If only one elemtn
{
return frontNode.getData().toString();
}
else //More than one element in the queue
{
while(frontNode != lastNode)
{
toReturn=toReturn+frontNode.getData()+"\n";
frontNode=frontNode.getNext();
}
toReturn= toReturn+frontNode.getData()+"\n"; //Appending data of last node because it will be missed in the loop
}
return toReturn;
}
}
Подробнее здесь: https://stackoverflow.com/questions/228 ... int-jgrasp
Как мне перекомпилировать с -Xlint? Джхват ⇐ JAVA
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Перекомпилировать с -Xlint: для получения подробной информации флажок не установлен.
Anonymous » » в форуме JAVA - 0 Ответы
- 29 Просмотры
-
Последнее сообщение Anonymous
-