我正在尝试编写一个代码,要求用户输入以从我输入的文件中删除存储到我的内联列表索引的任何行。此外,如果用户超出范围或行不存在/不在范围内,我也会有错误异常。
我需要使用get方法获取索引并返回它,然后使用remove方法。最后,我将输出带有添加更改的文件并将其显示给用户。 我不知道我的获取和删除方法是否正确。 希望有人可以指出我正确的方向!
到目前为止,这是我的代码:
//class JOptionPane
import javax.swing.JOptionPane;
//class String Tokenizer
import java.util.StringTokenizer;
//class Scanner
import java.util.Scanner;
//class File
import java.io.File;
//class FileNotFoundException
import java.io.FileNotFoundException;
public class Testing19{
public static void main(String[]args){
//checks to see if there's an input file in the commandline
if(args.length != 1)
//displays an error if there's none
System.out.println("ERROR: NO INPUT FOUND");
else{
//create a new object file from the commandline
File file = new File(args[0]);
//creat a new scanner
Scanner data = null;
try{
//creates a link between the file and data
data = new Scanner(file);
}
catch(FileNotFoundException e){
//displays the error
System.out.println("ERROR: File not found");
}
//initialize a string for the
String output = new String();
//this will check for data inside the file
while(data.hasNextLine()){
//read the data per line in the file
String line = data.nextLine();
//initialize a string for name
String name;
//initialize a string for age
String scientificname;
//initialize a string for age
String color;
//initialize a string for age
String population;
//make a new scanner for token
Scanner tokenInput = new Scanner(line).useDelimiter(",");
//assign the first token as a string
name = tokenInput.next();
//assign the first token as a string
scientificname = tokenInput.next();
//assign the first token as a string
color = tokenInput.next();
//assign the second token as an string
population = tokenInput.next();
//reads the 2nd token as an integer
Integer population2 = Integer.parseInt(population);
//create a new object list from the class LinkedList
LinkedList list = new LinkedList();
//calls the add() method from the LinkedList class
list.add(name, scientificname, color, population2);
//assign the result in the output string
output = output + list;
}
//displays the output in the JOption pane
JOptionPane.showMessageDialog(null, output);
}
}//end of main
}//end of class
/****************************************
* Class LinkedList
*
*****************************************/
class LinkedListAldz{
//datafield
protected HawaiiNativeForestBirdsNode head = null;
protected Integer size = new Integer(0);
//add()method
public void add(String name2, String scientificname2, String color2, Integer population2){
if (head == null) {
//new PersonNode and store it on head if the list is empty
head = new HawaiiNativeForestBirdsNode(name2,scientificname2,color2,population2, null);
}
else {
//adds to the end of the list
HawaiiNativeForestBirdsNode previous = head;
HawaiiNativeForestBirdsNode current = head.getNext();
while (current != null) {
//next PersonNode
previous = current;
current = current.getNext();
}
//new PersonNode at the of the list.
HawaiiNativeForestBirdsNode birds = new HawaiiNativeForestBirdsNode(name2,scientificname2,color2,population2,null);
//point previous node
previous.setNext(birds);
}
size++;
}
/**
* Gets an item (address to an item) from any position in the list.
*
* @param position
* The position of an item in the list.
* @returns the address to the requested item
* @exception ListException
* if an item does not exist at that position
*/
public int get(Integer position) {
// check if empty list
if (head == null) {
System.out.println("Cannot get an item from an empty list!");
}
// if position is outside range, throw exception
if (position < 1 || position > size) {
System.out.println(position + " is outside list range!");
}
// Find node:
// counter to keep track of loops
Integer counter = new Integer(1);
// point to current node
HawaiiNativeForestBirdsNode current = head;
while (!counter.equals(position)) {
// BAD CODE: while(counter != position){
// goto next node for current pointer
current = current.getNext();
// add 1 to counter
counter++;
}
// return the data (item) stored by the node
return current.getData();
}
/**
* Removes an item at any position from the list.
*
* @param position
* The position of an item in the list.
* @exception ListException
* if an item does not exist at that position
*/
public void remove(Integer position) throws ListException {
// check if empty list
if (head == null) {
throw new ListException("cannot remove from empty list");
}
// if position is outside range, throw exception
if (position < 1 || position > size) {
throw new ListException(position + " is outside list range.");
}
// if at beginning of list
if (position.equals(1)) {
// remove 1st node
head = head.getNext();
}
// if not at beginning of list
else {
// Find node:
// point previous to 1st node
HawaiiNativeForestBirdsNode previous = head;
// point current to 2nd node
HawaiiNativeForestBirdsNode
current = head.getNext();
// loop position-2 number of times
for (int i = 2; i < position; i++) {
// goto next node for previous and current
previous = current;
current = current.getNext();
}
// Point the previous node to node after current node.
// This "skips" over one node, thus removing it!
previous.setNext(current.getNext());
}
// decrease size of list
size--;
}
//toString()method
public String toString() {
// instantiate empty string
String csvFormat = new String("");
// display position of each item to user
Integer position = new Integer(1);
// loop through all the nodes in linked list
for (HawaiiNativeForestBirdsNode current = head; current != null; current = current
.getNext()) {
// keep adding to end of string
csvFormat = csvFormat + current.toString() + "\n";
// add one to position for each loop
position++;
}
return csvFormat;
}
}//end of LinkedList
/****************************************
* Class PersonNode
* @ param name for the name
* age for the age
* next for the next PersonNode
*****************************************/
class HawaiiNativeForestBirdsNode{
//data fields are set to private
private String name;
private String scientificname;
private String color;
private Integer population;
private HawaiiNativeForestBirdsNode next;
/********************************
* constructor method
* @ param x is for the name
* y is for the age
* next2 is for the next
********************************/
public HawaiiNativeForestBirdsNode(String x, String y, String z, Integer b, HawaiiNativeForestBirdsNode next2) {
name = x;
scientificname = y;
color = z;
population = b;
next = next2;
}
/*******************************
* calls the toString method
* from the class String
* returns result
********************************/
public String toString(){
//initialize te format of the output
String result = name + " "+ scientificname + " " + color + " "+ population;
return result;
}
/*********************
* acessory method
* returns name
**********************/
public String getName(){
return name;
}
/*********************
* acessory method
* returns name
**********************/
public String getScientificname(){
return scientificname;
}
/*********************
* acessory method
* returns name
**********************/
public String getColor(){
return color;
}
/*********************
* acessory method
* returns age
**********************/
public Integer getPopulation(){
return population;
}
/*********************
* acessory method
* returns next
**********************/
public HawaiiNativeForestBirdsNode getNext(){
return next;
}
/*********************
* mutator method
* @ param x is set name
**********************/
public void setName(String x){
name = x;
}
/*********************
* mutator method
* @ param x is set name
**********************/
public void setScientificName(String y){
scientificname = y;
}
/*********************
* mutator method
* @ param x is set name
**********************/
public void setColor(String z){
color = z;
}
/*********************
* mutator method
* @ param y is set age
**********************/
public void setPopulation(Integer b){
population = b;
}
/**************************
* nutator method
* @ param next2 is set next
***************************/
public void setNext(HawaiiNativeForestBirdsNode next2){
next = next2;
}
}//end of HawaiiNativeForestBirdsNode
输入文件的格式如下:
1.akiapola'au,hemignathus munroi,yellow,800
2.akepa,loxops coccineus,red,9301
3.hawai'i creeper,oreomystis mana,yellow green,2501
4.i'iwi,vestiara coccinea,red green,30001
5.apapane,himatione sanguinea,white red,5001
6.hawai'i amakihi,hemignathus virens,yellow brown,3001
7.oma'o,myadestes obscurus,gray,170001
8.hawai'an hawk,buteo solitarius,white gray,1100
9.puaiohi,myadestes palmeri,brown,125
10.anianiau,magumma parva,light yellow,2000
我尝试运行它并收到错误:
Testing19.java:136: error: cannot find symbol
return current.getData();
^
symbol: method getData()
location: variable current of type HawaiiNativeForestBirdsNode
1 error
答案 0 :(得分:1)
首先,你的班级&#39; HawaiiNativeForestBirdsNode&#39;不包含方法&#39; getData()&#39;。这就是抛出异常的原因。要获得用户输入,您应该使用BufferedReader:
import java.io.BufferedReader;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine();
- 路易斯