How to insert data read from text file into linked list?

Reply

Join Date: Aug 2008
Posts: 11
Reputation: W@n is an unknown quantity at this point 
Solved Threads: 0
W@n W@n is offline Offline
Newbie Poster

How to insert data read from text file into linked list?

 
0
  #1
Sep 25th, 2008
Hi, i am facing a problem with storing data into linked list. I have previously store the data read from the text file into an array but now my assignment require me to store all this record into a linked list. Can anyone help? May i know how to do it? I have created the list node and linkedlist class. Below is the attachment of the text file and my code. Thank in advance.

textfile.txt

Account Id = 67
Name = John
Address = 465 Ripley Boulevard, Oscar Mansion, Singapore 7666322
DOB = 10-10-1970
Phone Number = 790-3233
Account Balance = 405600.00
Account Type = Fixed
Fixed Daily Interest = 0.05

Account Id = 80
Name = Tim
Address = 200 Hunting Street, Singapore 784563
DOB = 25-10-1968
Phone Number = 432-4579
Account Balance = 530045.00
Account Type = Saving

Account Id = 65
Name = Tracy
Address = 45 Mexican Boulevard, Hotel California, Singapore 467822
DOB = 06-04-73
Phone Number = 678-1234
Account Balance = 25.00
Account Type = Checking

Account Id = 124
Name = Helen
Address = 50 Apple Avenue, Singapore 639798
DOB = 11-08-64
Phone Number = 345-6780
Account Balance = 234.00
Account Type = Checking

Account Id = 678
Name = Ming Fong
Address = 60 Hill View Ave, Singapore 456872
DOB = 15-02-70
Phone Number = 555-1234
Account Balance = 7000.00
Account Type = Saving

RecNode.java
  1. public class RecNode {
  2.  
  3. Record rec;
  4. RecNode next;
  5.  
  6. //Constructor
  7. public RecNode(Record r){
  8.  
  9. rec = r;
  10. next = null;
  11.  
  12. }
  13.  
  14. }

RecLinkedList.java
  1. public class RecLinkedList {
  2.  
  3. //reference to first item in list
  4. private RecNode pHead;
  5.  
  6. //Constructor
  7. public RecLinkedList(){
  8. pHead = null;
  9. }
  10.  
  11. public void insertFirst(Record r){
  12.  
  13. RecNode newNode = new RecNode(r);
  14. newNode.next = pHead;
  15. pHead = newNode;
  16.  
  17. }
  18.  
  19. public void displayList(){
  20.  
  21. System.out.println("\nLinked List: ");
  22.  
  23. //start of the list
  24. RecNode current = pHead;
  25.  
  26. //loop through until end of list
  27. while (current != null){
  28.  
  29. System.out.println(current.rec+ " ");
  30.  
  31. //move to the next link
  32. current = current.next;
  33.  
  34. }
  35. System.out.println();
  36.  
  37. }
  38.  
  39. //This method returns true if list is empty,false otherwise
  40. public boolean isEmpty(){
  41.  
  42. return(pHead == null);
  43.  
  44. }
  45.  
  46. //This method removes the first node from the linked list and returns
  47. //the reference to the deleted node.
  48. public RecNode deleteFirst(){
  49.  
  50. RecNode p = null;
  51.  
  52. //check if list is not empty
  53. if(!isEmpty()){
  54.  
  55. p = pHead;
  56. pHead = pHead.next;
  57. p.next = null;
  58.  
  59.  
  60. }
  61. return p;
  62.  
  63. }
  64.  
  65. //This method searches the linked list for a node with a specified key
  66. //value and returns a reference to that node
  67. public RecNode find(Record rKey){
  68.  
  69. RecNode p = pHead;
  70.  
  71. //if list is empty
  72. if(p == null){
  73. return null;
  74. }
  75.  
  76. while(p.rec!= rKey){
  77.  
  78. //if end of list
  79. if(p.next == null)
  80.  
  81. //not found
  82. return null;
  83. else
  84. p = p.next;
  85. }
  86.  
  87. //When found
  88. return p;
  89. }
  90.  
  91. //This method will first search the linked list for the node with a
  92. //specified c value, using the find method, and insert a new node with the
  93. //given value.
  94. public RecNode insertAfter(Record rKey, Record r){
  95.  
  96. RecNode p = find(rKey);
  97.  
  98. //if list is empty or rKey not found,insert fail
  99. if(p == null)
  100. return null;
  101. RecNode newNode = new RecNode (r);
  102. newNode.next= p.next;
  103. p.next = newNode;
  104. return newNode;
  105.  
  106. }
  107.  
  108. //This method deletes the node from the linked
  109. //link with the specified key value.
  110. public RecNode delete(Record rKey){
  111.  
  112. RecNode curr = pHead;
  113. RecNode prev = pHead;
  114.  
  115. //if list is empty
  116. if(curr == null)
  117. return null;
  118.  
  119. while(curr.rec != rKey){
  120.  
  121. //if end of list
  122. if(curr.next == null)
  123. //not found
  124. return null;
  125. else{
  126.  
  127. prev = curr;
  128. curr = curr.next;
  129.  
  130. }
  131. }
  132.  
  133. //find it
  134. //if the first node
  135. if(curr == pHead)
  136. pHead = pHead.next;
  137. else
  138. prev.next = curr.next;
  139.  
  140. //set the next ref to null
  141. curr.next = null;
  142. return curr;
  143. }
  144.  
  145. }

record.java
  1. public class Record{
  2.  
  3. /**
  4.   * @param args the command line arguments
  5.   */
  6. private int accountID;
  7. public String name;
  8. private String address;
  9. private String dob;
  10. private String phoneNumber;
  11. public String balance;
  12. private String inAccType;
  13. public double inInterest;
  14.  
  15. DecimalFormat formatter = new DecimalFormat("#0.00");
  16.  
  17. public Record(){
  18.  
  19. }
  20.  
  21. public Record(String name){
  22.  
  23. setName(name);
  24.  
  25. }
  26.  
  27. public Record(String name,double balance) {
  28.  
  29. setName(name);
  30. setBalance(balance);
  31.  
  32. }
  33.  
  34. public Record(String name,double balance,double interest) {
  35.  
  36. setName(name);
  37. setBalance(balance);
  38. setDailyInterest(interest);
  39.  
  40. }
  41.  
  42. public Record(String name,int accId,String add,String dob,
  43. String phoneNum,String type, double balance,
  44. double interest) {
  45.  
  46. setName(name);
  47. setAccountID(accId);
  48. setAddress(add);
  49. setDob(dob);
  50. setPhoneNumber(phoneNum);
  51. setAccType(type);
  52. setBalance(balance);
  53. setDailyInterest(interest);
  54.  
  55. }
  56.  
  57. public void setAccountID(int accId){
  58. accountID = accId;
  59. }
  60.  
  61. public int getAccountID() {
  62. return accountID;
  63. }
  64.  
  65. public void setName(String Custname){
  66. name = Custname;
  67. }
  68.  
  69. public String getName() {
  70. return name;
  71. }
  72.  
  73. public void setAddress(String add) {
  74. address = add;
  75. }
  76.  
  77. public String getAddress() {
  78. return address;
  79. }
  80.  
  81. public void setDob(String dateOfBirth){
  82. dob = dateOfBirth;
  83. }
  84.  
  85. public String getDob() {
  86. return dob;
  87. }
  88.  
  89. public void setPhoneNumber(String phoneNum) {
  90. phoneNumber = phoneNum;
  91. }
  92.  
  93. public String getPhoneNumber() {
  94. return phoneNumber;
  95. }
  96.  
  97. public void setBalance(double accBal) {
  98.  
  99. balance = formatter.format(accBal);
  100. }
  101.  
  102. public double getBalance() {
  103. return Double.parseDouble(balance);
  104. }
  105.  
  106. public void setAccType(String accType) {
  107. inAccType = accType;
  108. }
  109.  
  110. public String getAccType() {
  111. return inAccType;
  112. }
  113.  
  114. public void setDailyInterest(double interest) {
  115. inInterest = interest;
  116. }
  117.  
  118. public double getDailyInterest() {
  119. return inInterest;
  120. }
  121.  
  122. public void display() {
  123.  
  124. if(inInterest != 0 ){
  125.  
  126. System.out.println("\nName = " + name);
  127. System.out.println("Account Balance = " + balance);
  128. System.out.println("Account Id = " + accountID);
  129. System.out.println("Address = " + address);
  130. System.out.println("DOB = " + dob);
  131. System.out.println("Phone Number = " + phoneNumber);
  132. System.out.println("Account Type = " + inAccType);
  133. System.out.println("Fixed Daily Interest = " + inInterest);
  134.  
  135. }else if(inInterest == 0){
  136.  
  137. System.out.println("\nName = " + name);
  138. System.out.println("Account Balance = " + balance);
  139. System.out.println("Account Id = " + accountID);
  140. System.out.println("Address = " + address);
  141. System.out.println("DOB = " + dob);
  142. System.out.println("Phone Number = " + phoneNumber);
  143. System.out.println("Account Type = " + inAccType);
  144.  
  145. }
  146.  
  147.  
  148. }
  149.  
  150. public void update() {
  151.  
  152. System.out.println("Name = " + name);
  153. System.out.println("Account Balance = " + balance);
  154.  
  155. }
  156.  
  157. }

My read into array code
  1. public static int readFile(String fileName) throws IOException {
  2.  
  3. BufferedReader br = null;
  4. String line,add,dob,phoneNum,read,name = null,line1,line2, data,
  5. accType,fixedInterest = "";
  6. double accBal;
  7. int accid,c = 0, count = 0, countDupRec = 0, till = 0;
  8. boolean duplicate = false, check = false;
  9.  
  10. try {
  11.  
  12. if (recSize <= 30 && recSize != 0) {
  13.  
  14. rec= new Record[recSize];
  15. br = new BufferedReader(new FileReader(fileName));
  16. line = null;
  17.  
  18. while ((line = br.readLine()) != null) {
  19.  
  20. StringTokenizer st = new StringTokenizer(line, "=");
  21.  
  22. while (st.hasMoreTokens()) {
  23. read = st.nextToken();
  24.  
  25.  
  26. if (read.equals("Account Id ")) {
  27.  
  28. rec[r] = new Record();
  29. accid = Integer.parseInt(st.nextToken().trim());
  30. rec[r].setAccountID(accid);
  31.  
  32. }
  33.  
  34. if (read.equals("Name ")) {
  35.  
  36. name = st.nextToken().trim().toUpperCase();
  37. rec[r].setName(name);
  38.  
  39. }
  40.  
  41. if (read.equals("Address ")) {
  42.  
  43. add = st.nextToken().trim();
  44. rec[r].setAddress(add);
  45.  
  46. }
  47.  
  48. if (read.equals("DOB ")) {
  49.  
  50. dob = st.nextToken().trim();
  51. rec[r].setDob(dob);
  52.  
  53. }
  54.  
  55. if (read.equals("Phone Number ")) {
  56.  
  57. phoneNum = st.nextToken().trim();
  58. rec[r].setPhoneNumber(phoneNum);
  59.  
  60.  
  61. }
  62.  
  63. if (read.equals("Account Balance ")) {
  64.  
  65. accBal = Double.parseDouble(st.nextToken().trim());
  66. rec[r].setBalance(accBal);
  67.  
  68.  
  69. }
  70.  
  71. if (read.equals("Account Type ")) {
  72.  
  73. accType = st.nextToken().trim();
  74. rec[r].setAccType(accType);
  75.  
  76. r++;
  77.  
  78. }
  79.  
  80. }
  81.  
  82. }
  83.  
  84. //Get the fixed interest rate
  85. br = new BufferedReader(new FileReader(fileName));
  86. line2 = null;
  87.  
  88. while ((line2 = br.readLine()) != null) {
  89. StringTokenizer st4 = new StringTokenizer(line2, "=");
  90. while (st4.hasMoreTokens()) {
  91. data = st4.nextToken();
  92. if(data.equals("Fixed Daily Interest ")){
  93.  
  94. String aString = st4.nextToken();
  95. fixedInterest = fixedInterest + " " + aString;
  96.  
  97. }
  98.  
  99. }
  100.  
  101. }
  102.  
  103. //Get the elment that has fixed account type
  104. String val = "";
  105. for (int i = 0; i < recSize; i++) {
  106.  
  107. if (rec[i].getAccType().equals("Fixed")) {
  108.  
  109. String aString = Integer.toString(i);
  110. val = val + " " + aString;
  111.  
  112. }
  113.  
  114.  
  115. }
  116.  
  117. //Set the value of fixed Interest into fixed account
  118. StringTokenizer st5 = new StringTokenizer(val, " ");
  119. StringTokenizer st6 = new StringTokenizer(fixedInterest, " ");
  120.  
  121. while (st5.hasMoreTokens()&& st6.hasMoreTokens()) {
  122.  
  123. rec[Integer.parseInt(st5.nextToken())].setDailyInterest(
  124. Double.parseDouble(st6.nextToken()));
  125.  
  126. }
  127. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: How to insert data read from text file into linked list?

 
0
  #2
Sep 25th, 2008
Are you allowed to use these classes provided by java:
java.util.Vector
java.util.ArrayList ?
If yes then things are much easier
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 11
Reputation: W@n is an unknown quantity at this point 
Solved Threads: 0
W@n W@n is offline Offline
Newbie Poster

Re: How to insert data read from text file into linked list?

 
0
  #3
Sep 25th, 2008
Originally Posted by javaAddict View Post
Are you allowed to use these classes provided by java:
java.util.Vector
java.util.ArrayList ?
If yes then things are much easier
Thank for the reply=D
Nope i am not allow to use that i can only use linked list
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC