2016-10-21 5 views
-4

私は、リストされたリストのインデックスに格納されている入力ファイルから行を削除するための入力をユーザーに求めるコードを記述しようとしています。さらに、ユーザーが範囲外になった場合、または行が存在しない/範囲にない場合は、エラー例外が発生します。リンクリストから行を削除するには、ユーザーに入力を求める方法を教えてください。 (getおよびsetメソッドの使用)

インデックスを取得して戻すにはgetメソッドを使用し、次にremoveメソッドを使用する必要があります。最後に、追加された変更を含むファイルを出力し、それをユーザーに表示します。 私の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{ 
 
\t 
 
    public static void main(String[]args){ 
 
    \t //checks to see if there's an input file in the commandline 
 
     if(args.length != 1) 
 
     \t //displays an error if there's none 
 
     System.out.println("ERROR: NO INPUT FOUND"); 
 
     else{ 
 
     \t //create a new object file from the commandline 
 
     File file = new File(args[0]); 
 
     \t //creat a new scanner 
 
     Scanner data = null; 
 
     try{ 
 
      \t //creates a link between the file and data 
 
      data = new Scanner(file); 
 
      
 
     } 
 
     catch(FileNotFoundException e){ 
 
      \t //displays the error 
 
      System.out.println("ERROR: File not found"); 
 
     } 
 
     \t //initialize a string for the 
 
     String output = new String(); 
 
     \t //this will check for data inside the file 
 
     while(data.hasNextLine()){ 
 
      \t //read the data per line in the file 
 
      String line = data.nextLine(); 
 
      \t //initialize a string for name 
 
      String name; 
 
      \t //initialize a string for age 
 
      String scientificname; 
 
      //initialize a string for age 
 
      String color; 
 
      //initialize a string for age 
 
      String population; 
 
      \t //make a new scanner for token 
 
      Scanner tokenInput = new Scanner(line).useDelimiter(","); 
 
      \t //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(); 
 
      \t //assign the second token as an string 
 
      population = tokenInput.next(); 
 
      \t //reads the 2nd token as an integer 
 
      Integer population2 = Integer.parseInt(population); 
 
      \t //create a new object list from the class LinkedList 
 
      LinkedList list = new LinkedList(); 
 
      \t //calls the add() method from the LinkedList class 
 
      list.add(name, scientificname, color, population2); 
 
      \t //assign the result in the output string 
 
      output = output + list; 
 
     } 
 
     \t \t //displays the output in the JOption pane 
 
     JOptionPane.showMessageDialog(null, output); 
 
     \t 
 
     } 
 
    \t 
 
    }//end of main 
 
}//end of class 
 

 
/**************************************** 
 
* Class LinkedList 
 
* 
 
*****************************************/ 
 
class LinkedListAldz{ 
 
\t //datafield 
 
    protected HawaiiNativeForestBirdsNode head = null; 
 
    protected Integer size = new Integer(0); 
 
\t //add()method 
 
    public void add(String name2, String scientificname2, String color2, Integer population2){ 
 
     if (head == null) { 
 
     \t //new PersonNode and store it on head if the list is empty 
 
     head = new HawaiiNativeForestBirdsNode(name2,scientificname2,color2,population2, null); 
 
     } 
 
     else { 
 
     \t //adds to the end of the list 
 
     HawaiiNativeForestBirdsNode previous = head; 
 
     HawaiiNativeForestBirdsNode current = head.getNext(); 
 
     while (current != null) { 
 
      \t //next PersonNode 
 
      previous = current; 
 
      current = current.getNext(); 
 
     } 
 
     \t //new PersonNode at the of the list. 
 
     HawaiiNativeForestBirdsNode birds = new HawaiiNativeForestBirdsNode(name2,scientificname2,color2,population2,null); 
 
     \t //point previous node 
 
     previous.setNext(birds); 
 
     } 
 
     size++; 
 
    } 
 
    
 
    /** 
 
\t * Gets an item (address to an item) from any position in the list. 
 
\t * 
 
\t * @param position 
 
\t *   The position of an item in the list. 
 
\t * @returns the address to the requested item 
 
\t * @exception ListException 
 
\t *    if an item does not exist at that position 
 
\t */ 
 
    public int get(Integer position) { 
 
    \t // check if empty list 
 
     if (head == null) { 
 
     System.out.println("Cannot get an item from an empty list!"); 
 
     } 
 
    \t // if position is outside range, throw exception 
 
     if (position < 1 || position > size) { 
 
     System.out.println(position + " is outside list range!"); 
 
     } 
 
    \t // Find node: 
 
    \t // counter to keep track of loops 
 
     Integer counter = new Integer(1); 
 
    \t // point to current node 
 
     HawaiiNativeForestBirdsNode current = head; 
 
     while (!counter.equals(position)) { 
 
     \t // BAD CODE: while(counter != position){ 
 
     \t // goto next node for current pointer 
 
     current = current.getNext(); 
 
     \t // add 1 to counter 
 
     counter++; 
 
     } 
 
    \t // return the data (item) stored by the node 
 
     return current.getData(); 
 
     
 
    } 
 
    
 
    /** 
 
\t * Removes an item at any position from the list. 
 
\t * 
 
\t * @param position 
 
\t *   The position of an item in the list. 
 
\t * @exception ListException 
 
\t *    if an item does not exist at that position 
 
\t */ 
 
    public void remove(Integer position) throws ListException { 
 
    \t // check if empty list 
 
     if (head == null) { 
 
     throw new ListException("cannot remove from empty list"); 
 
     } 
 
    \t // if position is outside range, throw exception 
 
     if (position < 1 || position > size) { 
 
     throw new ListException(position + " is outside list range."); 
 
     } 
 
    \t // if at beginning of list 
 
     if (position.equals(1)) { 
 
     \t // remove 1st node 
 
     head = head.getNext(); 
 
     } 
 
     // if not at beginning of list 
 
     else { 
 
     \t // Find node: 
 
     \t // point previous to 1st node 
 
     HawaiiNativeForestBirdsNode previous = head; 
 
     \t // point current to 2nd node 
 
     HawaiiNativeForestBirdsNode 
 
      current = head.getNext(); 
 
     \t // loop position-2 number of times 
 
     for (int i = 2; i < position; i++) { 
 
      \t // goto next node for previous and current 
 
      previous = current; 
 
      current = current.getNext(); 
 
     } 
 
     \t // Point the previous node to node after current node. 
 
     \t // This "skips" over one node, thus removing it! 
 
     previous.setNext(current.getNext()); 
 
     } 
 
    \t // decrease size of list 
 
     size--; 
 
    } 
 
    
 
    
 
\t \t //toString()method 
 
    public String toString() { 
 
    \t // instantiate empty string 
 
     String csvFormat = new String(""); 
 
    \t // display position of each item to user 
 
     Integer position = new Integer(1); 
 
    \t // loop through all the nodes in linked list 
 
     for (HawaiiNativeForestBirdsNode current = head; current != null; current = current 
 
     \t \t .getNext()) { 
 
     \t // keep adding to end of string 
 
     csvFormat = csvFormat + current.toString() + "\n"; 
 
     \t // add one to position for each loop 
 
     position++; 
 
     } 
 
     return csvFormat; 
 
    } 
 
    
 
    
 
}//end of LinkedList 
 

 

 
/**************************************** 
 
* Class PersonNode 
 
* @ param name for the name 
 
* \t \t \t age for the age 
 
* \t \t \t next for the next PersonNode 
 
*****************************************/ 
 
    
 

 
class HawaiiNativeForestBirdsNode{ 
 
    \t //data fields are set to private 
 
     private String name; 
 
     private String scientificname; 
 
     private String color; 
 
     private Integer population; 
 
     private HawaiiNativeForestBirdsNode next; 
 
    \t 
 
    \t /******************************** 
 
    \t * constructor method 
 
    \t * @ param x is for the name 
 
    \t * \t \t \t y is for the age 
 
    \t * \t \t \t next2 is for the next 
 
    \t ********************************/ 
 
     public HawaiiNativeForestBirdsNode(String x, String y, String z, Integer b, HawaiiNativeForestBirdsNode next2) { 
 
      name = x; 
 
      scientificname = y; 
 
      color = z; 
 
      population = b; 
 
      next = next2; 
 
     } 
 
    \t 
 
    \t /******************************* 
 
    \t * calls the toString method 
 
    \t * from the class String 
 
    \t * returns result 
 
    \t ********************************/ 
 
     public String toString(){ 
 
     \t //initialize te format of the output 
 
      String result = name + " "+ scientificname + " " + color + " "+ population; 
 
      return result; 
 
     } 
 
    \t 
 
    \t /********************* 
 
    \t * acessory method 
 
    \t * returns name 
 
    \t **********************/ 
 
     public String getName(){ 
 
      return name; 
 
     } 
 
     
 
     /********************* 
 
    \t * acessory method 
 
    \t * returns name 
 
    \t **********************/ 
 
     public String getScientificname(){ 
 
      return scientificname; 
 
     } 
 
      
 
      /********************* 
 
    \t * acessory method 
 
    \t * returns name 
 
    \t **********************/ 
 
     public String getColor(){ 
 
      return color; 
 
     } 
 

 
    \t 
 
    \t /********************* 
 
    \t * acessory method 
 
    \t * returns age 
 
    \t **********************/ 
 
     public Integer getPopulation(){ 
 
      return population; 
 
     } 
 
    \t /********************* 
 
    \t * acessory method 
 
    \t * returns next 
 
    \t **********************/ 
 
     public HawaiiNativeForestBirdsNode getNext(){ 
 
      return next; 
 
     } 
 
    \t 
 
    \t /********************* 
 
    \t * mutator method 
 
    \t * @ param x is set name 
 
    \t **********************/ 
 
     public void setName(String x){ 
 
      name = x; 
 
     } 
 
     
 
     
 
    \t /********************* 
 
    \t * mutator method 
 
    \t * @ param x is set name 
 
    \t **********************/ 
 
     public void setScientificName(String y){ 
 
      scientificname = y; 
 
     } 
 
     
 
     
 
    \t /********************* 
 
    \t * mutator method 
 
    \t * @ param x is set name 
 
    \t **********************/ 
 
     public void setColor(String z){ 
 
      color = z; 
 
     } 
 

 

 
    \t 
 
    \t /********************* 
 
    \t * mutator method 
 
    \t * @ param y is set age 
 
    \t **********************/ 
 
     public void setPopulation(Integer b){ 
 
      population = b; 
 
     } 
 
    \t 
 
    \t /************************** 
 
    \t * nutator method 
 
    \t * @ param next2 is set next 
 
    \t ***************************/ 
 
     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

一般的なコメント:あなたは、例えば、あなたに何か他のものに名前を付けたい場合がありますので、Javaコレクションは、 'LinkedList'と呼ばれるクラスを公開します'LinkedListAldz' –

+0

もっと一般的なコメント:記事にコードを提供することに関する[MCVE]ガイダンスをお読みください。 –

+0

は今リンクリストを変更します – aldz24

答えて

1

まず、あなたのクラスのHawaiiNativeForestBirdsNodeは 'メソッド 'のgetDataを()' が含まれていません。そのため、例外がスローされます。ユーザー入力を取得するには、代わりにBufferedReaderのを使用する必要があります。

import java.io.BufferedReader; 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 
String input = br.readLine(); 

- ルイス

+0

私のコードのどの部分を置き換えるべきですか? getメソッドまたはremoveメソッド? – aldz24

+0

私のバッファリーダーはどこに置くべきですか? – aldz24

+0

ああ、ちょうど私のremoveメソッドをそのlemmeで置き換えてください – aldz24

関連する問題