Results 1 to 7 of 7
  1. #1
    Googly is offline Banned
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Question 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
    /code
    Last edited by Googly; 05-12-2012 at 05:34 AM.

  2. #2
    Googly is offline Banned
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default 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());

    }

  3. #3
    Googly is offline Banned
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with Reading string in text file and inserting them into a singly linke

    Quote Originally Posted by Googly View Post
    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());

    }
    Can anyone help me how to integrate this to get to my singly linked list? Please help I am really stuck :( Pondering around this for ages

  4. #4
    Googly is offline Banned
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with Reading string in text file and inserting them into a singly linke

    Quote Originally Posted by Googly View Post
    Can anyone help me how to integrate this to get to my singly linked list? Please help I am really stuck :( Pondering around this for ages
    Bump

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default 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.

    ** This space for rent **

  7. #7
    Googly is offline Banned
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Need help with Reading string in text file and inserting them into a singly linke

    Quote Originally Posted by JosAH View Post
    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,

    Jos
    cool I will do that. Ty for being patient with a newbie:)

Similar Threads

  1. Replies: 5
    Last Post: 11-17-2011, 05:45 PM
  2. Singly Linked List Java implementation
    By xayto in forum New To Java
    Replies: 1
    Last Post: 08-31-2011, 02:36 PM
  3. Trouble with circular singly linked queue
    By somewierdguy in forum New To Java
    Replies: 2
    Last Post: 12-06-2010, 02:48 AM
  4. Implementing a singly linked list
    By Onra in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 10:19 PM
  5. Trouble Developing Singly Linked Circular List
    By VinceGuad in forum New To Java
    Replies: 14
    Last Post: 02-25-2009, 05:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •