2017-02-28 9 views
0

ファイルから読み込み、1つのリンクリストにデータを格納しようとしています。ファイルから読み込んだデータとリンクされたデータの格納方法

データ型longid、タイプstringname、及びタイプintthreat level含むユーザに関する情報を有していなければなりません。さらに、ファイルから読み込んでリンクリストに格納する方法を知り、いくつかの操作を行うことができます。

私の試み:

クラスPOI

public class POI { 

    private long id; 
    private String name; 
    private int level; 

    public POI(){ 

    } 

    public POI(long n, String s, int l){ 
     id = n; 
     name = s; 
     level = l; 
    } 

    public void setID (long n){ 
     id = n; 
    } 

    public void setName (String s){ 
     name = s; 
    } 

    public void setLevel (int l){ 
     level = l; 
    } 

    public long getID(){ 
     return id; 
    } 

    public String getName(){ 
     return name; 
    } 

    public int getLevel(){ 
     return level; 
    } 
} 

クラスPOIList

public class POIList { 
    static private class Node { 
     int data; 
     Node next; 
     Node() { 
      data = 0; 
      next = null; 
     } 
     Node(int data, Node next) { 
      this.data = data; 
      this.next = next; 
     } 
    } 

    public static void print(Node head) { 
     while (head != null) { 
      System.out.print(head.data + ", "); 
      head = head.next; 
     } 
     System.out.println(); 
    } 

    public Node insertNode(Node head, Node insertee, int position) { 
     if (position < 0) { 
      System.out.println("Invalid position given"); 
      return head; 
     } 
     if (head == null) { 
      return insertee; 
     } 
     if (position == 0) { 
      insertee.next = head; 
      return insertee; 
     } 
     int i = 0; 
     Node current=head;   
     while (i < position - 1 && current.next != null) { 
      current = current.next; 
      i++; 
     } 
     if (i == position - 1) { 
      insertee.next = current.next; 
      current.next = insertee; 
     } else { 
      System.out.println("Position was not found."); 
     } 
     return head; 
    } 

    static Node swapNode(Node head, 
      int position1, int position2) { 
     if(position1 < 0 || position2 < 0) 
      System.out.println("InvalidPos"); 
     Node n1 = null; 
     Node n2 = null; 
     Node prev1=null; 
     Node prev2=null; 
     int maxPosition = Math.max(position1, position2); 
     if (position1==maxPosition){ 
      position1=position2; 
      position2=maxPosition; 
     } 
     Node temp=head; 
     for (int i = 0;i <= maxPosition; i++) { 
      if (temp == null) { 
       System.out.println("InvalidPos"); 
       return head; 
      } 
      if (i==position1-1) prev1=temp; 
      if(i == position1) n1 = temp; 
      if (i==position2-1) prev2=temp; 
      if(i == position2) n2 = temp; 
      temp = temp.next; 
     } 
     temp = n2.next; 
     if (prev1!=null){ 
      prev1.next=n2; 
     }else{ 
      head=n2; 
     } 
     if (position2-position1==1){ 
      n2.next=n1; 
     }else{ 
      n2.next=n1.next; 
     } 
     if (prev2!=null){ 
      prev2.next=n1; 
     }else{ 
      head=n1; 
     } 
     n1.next=temp; 
     return head; 
    } // End of swapNode 

    public static Node removeNode(Node head, int position) { 
     if (position < 0 || head == null) { 
      System.out.println("Invalid position given"); 
      return head; 
     } 
     if (position == 0) { 
      return head.next; 
     } 
     int i = 0; 
     Node current = head; 
     while (i < position - 1 && current.next != null) { 
      current = current.next; 
      i++; 
     } 
     if (current.next != null) { 
      current.next = current.next.next; 
     } else { 
      System.out.println("Position was not found."); 
     } 
     return head; 
    } 
} 

クラスAnalyzePOI

public class AnalyzePOI { 

    public static void main (String [] args) throws FileNotFoundException, IOException{ 
     Scanner scan = new Scanner(System.in); 
     int choice; 

     System.out.print("Input filename:"); 
     String filename = scan.nextLine(); 
     File file = new File(filename); 
     Scanner reader = new Scanner(file); 

     POIList list = new POIList(); 
     System.out.println("What operation would you like to implement? "); 
     choice = scan.nextInt(); 

     switch (choice) { 
     case 1 : print(list) ; 
      break ; 
     case 2 : search(reader, list) ; 
      break ; 
     case 3 : insert(scan, list); 
      break ; 
     case 4 : swap(reader, list); 
      break ; 
     case 5 : remove1(reader, list); 
      break; 
     case 6 : remove2(reader, list); 
      break; 
     case 7 : output(); 
      break; 
     case 8 : 
      System.out.println ("Program Terminated"); 
      System.exit(0); 
      break; 
     } 

     start(scan,file, reader); 
    } 

    public static void start (Scanner scan, File file, Scanner reader){ 
     String content = new String(); 
     int count=1; 
     File file1 = new File("abc.txt"); 
     LinkedList<String> list = new LinkedList<String>(); 

     try { 
      Scanner sc = new Scanner(new FileInputStream(file)); 
      while (sc.hasNextLine()){ 
       content = sc.nextLine(); 
       list.add(content); 
      } 
      sc.close(); 
     }catch(FileNotFoundException fnf){ 
      fnf.printStackTrace(); 
     } 
     catch (Exception e) { 
      e.printStackTrace(); 
      System.out.println("\nProgram terminated Safely..."); 
     } 

     Collections.reverse(list); 
     Iterator i = (Iterator) list.iterator(); 
     while (((java.util.Iterator<String>) i).hasNext()) { 
      System.out.print("Node " + (count++) + " : "); 
      System.out.println(); 
     } 
    } 

    public static void print(POIList list) { 
     list.print(null); 
    } 

    public static void search(Scanner scan, POIList list) { 
     int id; 
     String name; 

     System.out.println("Do you want to enter id or name to search for record: "); 
     String answer = scan.next(); 
     if (answer == "id"){ 
      System.out.println ("Enter the id to find the record: "); 
      id = scan.nextInt(); 
     } 
     else if (answer == "name"){ 
      System.out.println("Enter the name to find the record: "); 
      name = scan.nextLine(); 
     } 
     else{ 
      System.out.println("Invalid input"); 
     } 
    } 

    public static void insert(Scanner scan, POIList list) { 
     System.out.println("Enter the the location index "); 
     int index = 0; 
     long p1; 
     int level; 
     String name; 

     try { 
      System.out.println("Index: ") ; 
      index= scan.nextInt() ; 
      System.out.println("ID: ") ; 
      p1=scan.nextLong() ; 
      System.out.println("Name: "); 
      name = scan.nextLine(); 
      System.out.println("Threat Level ") ; 
      level=scan.nextInt() ; 
     } 

     catch (InputMismatchException e) { 
      System.out.print("Invalid Input") ; 
     } 
     list.insertNode(null, null, index); 
    } 

    public static void swap(Scanner scan, POIList list) { 
     System.out.println("Enter index 1 to swap record: "); 
     int index1 = scan.nextInt(); 
     System.out.println("Enter index 2 to swap record: "); 
     int index2 = scan.nextInt(); 
     list.swapNode(null, index1, index2);  
    } 

    public static void remove1(Scanner scan, POIList list) { 
     int index= 0; 
     try{ 
      System.out.println("Enter ID to remove a record: ") ; 
      index=scan.nextInt() ; 
     } 
     catch (InputMismatchException e) { 
      System.out.print("Invalid Input") ; 
     } 

     list.removeNode(null, index) ; 
    } 

    public static void remove2(Scanner scan, POIList list){ 
     int index = 0; 
     try{ 
      System.out.println("Enter threat level to remove a record: "); 
      index=scan.nextInt() ; 
     } 
     catch (InputMismatchException e){ 
      System.out.println("Invalid Input"); 
     } 
     list.removeNode(null, index) ; 
    } 
    public static void output() { 

    } 
} 
+0

は、私はあなたが求めているものに関してはわかりませんよ。あなたの方法は機能していませんか?または、プロセスを改善するための提案をいくつか求めています。 – ShankarDaruga

+0

はいstartというメソッドが動作しません。ファイルを読み込んでリンクリストに渡す方法はわかりません。ファイルには長い文字列と整数が含まれていますが、このデータ型を格納してリンクされたリストに渡す方法はわかりません。 – Alan

+0

Hmm。はい。下の私の答えをチェックしてください。しかし、その前に、startメソッド以外のコードの他の部分にいくつか問題があると思います。私はそのすべてを通過しなかった。しかし、あなたはPOIListクラスがintとnextではなくNodeクラスでPOIとnextを持つべきだと思いませんか? POIクラスをどこに使って保管していますか? – ShankarDaruga

答えて

0

"String" "long"と "int"値を含むファイルの読み方を知りたいと思っています。入力ファイルが以下のようになるとしましょう。

まず、データがどのように見えるかを知る必要があります。私はI/Pは今、この

1 OneString 10 
2 TwoSTring 20 

ようになりますことを仮定している、私は仮定しているため、各ラインの間にスペースを持つ「長い」「文字列」「INT」です。 BufferedReaderを使用すると、より高速な読み取りが可能です。

FileReader fr = new FileReader("yourFile.txt"); 
BufferedReader br = new BufferedReader(fr); 
String curLine = br.next(); 
while(curLine!=null) 
{ 
String[] tempLine = curLine.split(" "); 
int threat = Integer.parseInt(tempLine[0]); 
String user = tempLine[1]; 
long ID = Long.parseLong(tempLine[2]); 
addPOI(ID,user,threat); 
} 

addPOIメソッドは次のようになります。

public void addPOI(long ID, String user, int threatLevel) 
{ 
list.addAtLast(new POI(ID,user,threadLevel)); 
} 

そして、リストはこのような何かを宣言することができ、

List<POI> list = new List<POI>(); 
+0

助けてくれてありがとうが、私はついに自分の解決策を見つけました。 – Alan

関連する問題