Results 1 to 7 of 7
- 05-11-2012, 06:02 AM #1
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Need help with Reading string in text file and inserting them into a singly linked
Need help with Reading strings in text file and inserting them into a singly linked list
Here is the full question:
The aim of this task is to implement and manipulate the basic operations related to Singly linked list and Doubly linked list data structures.
Assume that file1.txt and file2.txt contain unknown number of strings (text format). Write Java program that prints out all the strings that are in file1.txt but not in file2.txt. You have to store all the strings in file1.txt in a doubly linked list and all the strings in file2.txt in a singly linked list.
Pseudo algorithm to do this task:
- Read the strings/names in file1.txt and insert them into a doubly linked list.
- Read the strings/names in file2.txt and insert them into a singly linked list.
- Scan the singly linked list and for each string:
o Remove all occurrences of this string from the doubly linked list.
- Print out all the strings in the doubly linked list – five strings per line.
My part of the question to start with:
First of all does how do I read strings from text file? I tried inserting them in a singly linked list, but I think what I did was wrong. May be I may have to make the Java read the whole text file.
Can someone point me in the right direction and help me find sources for the above question please :)
Here is my code: (I am pretty sure they could be wrong, which is why I am asking questions :) )
MAIN METHOD:
code
package Singlylinkedlist;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
public class SinglyLinkedList {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("This is Singly Linked List");
Singlylinkedlistconstructor sll = new Singlylinkedlistconstructor();
sll.insert("Organiser Megan Singleton said the");
sll.insert("day was a chance for");
sll.insert("people to prove New Zealand");
sll.insert("was the kindest nation on");
sll.insert("earth");
sll.printList();
}
}
/code
CLASS FILE:
code
package Singlylinkedlist;
/**
*
* @author
*/
public class Singlylinkedlistconstructor {
//declaring Nodes
public Node head;
public Node tail;
//constructor
Singlylinkedlistconstructor() {head=null;tail=null;}
//Node or itme in the singly linked list
public class Node {
//declaring string as Node and name as string
String name;
Node next;
//constructor
Node(String s) {name=s;}
}//end of Node class
//start of inserting the string method
public void insert(String s) {
Node a = new Node(s);
//if the node head is empty then both head and tail is node 'a'
if (head == null) {
head = a;
tail = a;
} else {
//if not empty then the tail is 'a'
tail.next = a;
tail = a;
}
return;
} // end of method insert
public void printList() {
Node p=head;
System.out.println("These are the strings from file1.txt: ");
while (p != null) {
System.out.println(p.name);
p = p.next;
}
}
} //end of singlylinkedlistConstructor class
/codeLast edited by Googly; 05-12-2012 at 04:34 AM.
- 05-11-2012, 06:24 AM #2
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Re: Need help with Reading string in text file and inserting them into a singly linke
I found the scanner code:
import java.util.*;
import java.io.*;
File f1 = new File("C:\\Temp\\Test\\file1.txt");
try {
Scanner sc1 = new Scanner(f1);
String w;
while (sc1.hasNext()) {
w= sc1.next();
// Do whatever you want to do.
} // end of while
} catch (IOException e) {
System.out.println("Unable to create : "+e.getMessage());
}
- 05-11-2012, 08:49 AM #3
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
- 05-11-2012, 08:51 AM #4
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
- 05-11-2012, 10:00 AM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: Need help with Reading string in text file and inserting them into a singly linke
That was two minutes before you bumped your thread; you should be more patient; about your problem: separate your list code completely from the reading part. A list shouldn't know anything about files and the format of their content; all a list should do is insert, update and delete nodes to/from the list. The separation makes it easier to write and test your code.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 05-11-2012, 10:30 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,476
- Rep Power
- 16
Re: Need help with Reading string in text file and inserting them into a singly linke
Also, when posting code please use [code] tags [/code].
It makes it a lot easier for us to follow the flow of your code.Please do not ask for code as refusal often offends.
- 05-12-2012, 04:29 AM #7
Member
- Join Date
- May 2012
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Reading a text file and creating a String from contents
By paulio2 in forum New To JavaReplies: 5Last Post: 11-17-2011, 04:45 PM -
Singly Linked List Java implementation
By xayto in forum New To JavaReplies: 1Last Post: 08-31-2011, 01:36 PM -
Trouble with circular singly linked queue
By somewierdguy in forum New To JavaReplies: 2Last Post: 12-06-2010, 01:48 AM -
Implementing a singly linked list
By Onra in forum New To JavaReplies: 2Last Post: 04-12-2010, 09:19 PM -
Trouble Developing Singly Linked Circular List
By VinceGuad in forum New To JavaReplies: 14Last Post: 02-25-2009, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks