Re: Pls Fix my java code (using Linded List)

Discussion in 'Java' started by stephen, Jan 25, 2006.

  1. stephen

    stephen Guest

    Dear All!!,

    I have a problem on my java program. pls help me as i can't find error.

    below is my java coding.

    import java.io.*;

    public class Demo {

    static Node objLL;
    static BufferedReader br = new BufferedReader (new
    InputStreamReader ( System.in));

    public static void main (String args[]) throws IOException {

    int choice = 0;

    do {

    System.out.println ("\nDemo:");
    System.out.println ("-----\n");
    System.out.println ("Press (1) to ADD.");
    System.out.println ("Press (2) to VIEW.");
    System.out.println ("Press (3) to EXIT.");
    System.out.print ("Choice: ");
    choice = Integer.parseInt (br.readLine ());

    if (choice == 1)
    add ();
    else if (choice == 2)
    view ();

    } while (choice != 3);
    }

    static void add () {

    if (objLL == null)
    objLL = new Node ();
    else {

    Node temp = objLL;
    while (temp != null)
    temp = temp.next;

    temp = new Node ();
    }

    System.out.println ("\nNode added.");
    }

    static void view () {

    if (objLL == null)
    System.out.println ("\nNo nodes to display.");
    else {

    System.out.println ();

    int loopCount = 0;
    for (loopCount = 1; loopCount <=
    objLL.nodeCount; loopCount++)
    System.out.println (loopCount);

    System.out.println ();
    }
    }
    }

    class Node {

    public Node next;
    static int nodeCount;

    Node () {

    next = null;
    nodeCount++;
    }
    }


    that is program process

    when u run the program.............


    1.add
    2.view
    3.exit

    enter ur number - 1

    enter ur username - aung aung
    enter ur ID - 122333
    enter ur contact number - 313412415
    enter ur genter - m

    enter ur number - 2

    user name user ID contact number genter
    ----------------------------------------------------------------------------------------
    aungaung 12333 313412415 m

    enter ur number - 1

    enter ur username - aye
    enter ur ID - 22222
    enter ur contact number - 22222222
    enter ur genter - f

    enter ur number - 2

    user name user ID contact number genter
    ----------------------------------------------------------------------------------------
    aungaung 12333 313412415 m
    aye 22222 222222222 f

    enter ur number - 3

    program end .........

    that is all the process
    no need DB and no need array
    only simple java code
    thx 4 helping me .

    Help Me, Pls
     
    stephen, Jan 25, 2006
    #1
    1. Advertising

  2. stephen

    Venky Guest

    You need some variables to store username, id... where are you
    declaring them?? I guess you need to declare them as memeber variables
    in Node class and in the Node constructor you need to read the values
    for these variables.

    And override toString() function in Node which will print these values,
    so that you can print Node object.
     
    Venky, Jan 25, 2006
    #2
    1. Advertising

  3. stephen

    Jignesh Guest

    Hello Stephan,

    i have modified yr code and made same program that u want...

    source code:

    import java.io.*;


    public class Demo {


    static Node objLL;
    static BufferedReader br = new BufferedReader (new
    InputStreamReader ( System.in));


    public static void main (String args[]) throws IOException {


    int choice = 0;


    do {


    System.out.println ("\nDemo:");
    System.out.println ("-----\n");
    System.out.println ("Press (1) to ADD.");
    System.out.println ("Press (2) to VIEW.");
    System.out.println ("Press (3) to EXIT.");
    System.out.print ("Choice: ");
    choice = Integer.parseInt (br.readLine ());


    if (choice == 1)
    add ();
    else if (choice == 2)
    view ();


    } while (choice != 3);
    }


    static void add () {


    if (objLL == null)
    objLL = new Node ();
    else {


    Node temp = objLL;
    while (temp != null)
    temp = temp.next;


    temp = new Node ();
    }
    try{
    System.out.print ("Enter Username:");
    objLL.v1.add((String)br.readLine ());
    System.out.print ("Enter Id:");
    objLL.v2.add((String)br.readLine ());
    System.out.print ("Enter Contact Number:");
    objLL.v3.add((String)br.readLine ());
    System.out.print ("Enter genter:");
    objLL.v4.add((String)br.readLine ());
    }catch(Exception e)
    {
    e.printStackTrace();
    }

    System.out.println ("\nNode added.");
    }


    static void view () {


    if (objLL == null)
    System.out.println ("\nNo nodes to display.");
    else {


    System.out.println ();


    int loopCount = 0;
    for (loopCount = 1; loopCount <=
    objLL.nodeCount; loopCount++)
    System.out.println (loopCount);
    System.out.println("user name user ID contact
    number genter");
    System.out.println("-----------------------------------------------------------------");

    for(int i=0;i<objLL.v1.size();i++)
    {
    System.out.println((String)objLL.v1.get(i) + "\t\t\t"
    +(String)objLL.v2.get(i)+"\t\t\t"+(String)objLL.v3.get(i)+"\t\t\t"+(String)objLL.v4.get(i));
    }
    System.out.println ();
    }


    }



    }


    class Node {

    public Node next;
    static int nodeCount;
    java.util.Vector v1 = new java.util.Vector();
    java.util.Vector v2 = new java.util.Vector();
    java.util.Vector v3 = new java.util.Vector();
    java.util.Vector v4 = new java.util.Vector();

    Node () {


    next = null;
    nodeCount++;
    }



    }
     
    Jignesh, Jan 27, 2006
    #3
  4. stephen

    Jignesh Guest

    Hello Stephan,

    i have modified yr code and made same program that u want...

    source code:

    import java.io.*;


    public class Demo {


    static Node objLL;
    static BufferedReader br = new BufferedReader (new
    InputStreamReader ( System.in));


    public static void main (String args[]) throws IOException {


    int choice = 0;


    do {


    System.out.println ("\nDemo:");
    System.out.println ("-----\n");
    System.out.println ("Press (1) to ADD.");
    System.out.println ("Press (2) to VIEW.");
    System.out.println ("Press (3) to EXIT.");
    System.out.print ("Choice: ");
    choice = Integer.parseInt (br.readLine ());


    if (choice == 1)
    add ();
    else if (choice == 2)
    view ();


    } while (choice != 3);
    }


    static void add () {


    if (objLL == null)
    objLL = new Node ();
    else {


    Node temp = objLL;
    while (temp != null)
    temp = temp.next;


    temp = new Node ();
    }
    try{
    System.out.print ("Enter Username:");
    objLL.v1.add((String)br.readLine ());
    System.out.print ("Enter Id:");
    objLL.v2.add((String)br.readLine ());
    System.out.print ("Enter Contact Number:");
    objLL.v3.add((String)br.readLine ());
    System.out.print ("Enter genter:");
    objLL.v4.add((String)br.readLine ());
    }catch(Exception e)
    {
    e.printStackTrace();
    }

    System.out.println ("\nNode added.");
    }


    static void view () {


    if (objLL == null)
    System.out.println ("\nNo nodes to display.");
    else {


    System.out.println ();


    int loopCount = 0;
    for (loopCount = 1; loopCount <=
    objLL.nodeCount; loopCount++)
    System.out.println (loopCount);
    System.out.println("user name user ID contact
    number genter");
    System.out.println("-----------------------------------------------------------------");

    for(int i=0;i<objLL.v1.size();i++)
    {
    System.out.println((String)objLL.v1.get(i) + "\t\t\t"
    +(String)objLL.v2.get(i)+"\t\t\t"+(String)objLL.v3.get(i)+"\t\t\t"+(String)objLL.v4.get(i));
    }
    System.out.println ();
    }


    }



    }


    class Node {

    public Node next;
    static int nodeCount;
    java.util.Vector v1 = new java.util.Vector();
    java.util.Vector v2 = new java.util.Vector();
    java.util.Vector v3 = new java.util.Vector();
    java.util.Vector v4 = new java.util.Vector();

    Node () {


    next = null;
    nodeCount++;
    }



    }
     
    Jignesh, Jan 27, 2006
    #4
  5. stephen

    Jignesh Guest

    Hello Stephan,

    i have modified yr code and made same program that u want...

    source code:

    import java.io.*;


    public class Demo {


    static Node objLL;
    static BufferedReader br = new BufferedReader (new
    InputStreamReader ( System.in));


    public static void main (String args[]) throws IOException {


    int choice = 0;


    do {


    System.out.println ("\nDemo:");
    System.out.println ("-----\n");
    System.out.println ("Press (1) to ADD.");
    System.out.println ("Press (2) to VIEW.");
    System.out.println ("Press (3) to EXIT.");
    System.out.print ("Choice: ");
    choice = Integer.parseInt (br.readLine ());


    if (choice == 1)
    add ();
    else if (choice == 2)
    view ();


    } while (choice != 3);
    }


    static void add () {


    if (objLL == null)
    objLL = new Node ();
    else {


    Node temp = objLL;
    while (temp != null)
    temp = temp.next;


    temp = new Node ();
    }
    try{
    System.out.print ("Enter Username:");
    objLL.v1.add((String)br.readLine ());
    System.out.print ("Enter Id:");
    objLL.v2.add((String)br.readLine ());
    System.out.print ("Enter Contact Number:");
    objLL.v3.add((String)br.readLine ());
    System.out.print ("Enter genter:");
    objLL.v4.add((String)br.readLine ());
    }catch(Exception e)
    {
    e.printStackTrace();
    }

    System.out.println ("\nNode added.");
    }


    static void view () {


    if (objLL == null)
    System.out.println ("\nNo nodes to display.");
    else {


    System.out.println ();


    int loopCount = 0;
    for (loopCount = 1; loopCount <=
    objLL.nodeCount; loopCount++)
    System.out.println (loopCount);
    System.out.println("user name user ID contact
    number genter");
    System.out.println("-----------------------------------------------------------------");

    for(int i=0;i<objLL.v1.size();i++)
    {
    System.out.println((String)objLL.v1.get(i) + "\t\t\t"
    +(String)objLL.v2.get(i)+"\t\t\t"+(String)objLL.v3.get(i)+"\t\t\t"+(String)objLL.v4.get(i));
    }
    System.out.println ();
    }


    }



    }


    class Node {

    public Node next;
    static int nodeCount;
    java.util.Vector v1 = new java.util.Vector();
    java.util.Vector v2 = new java.util.Vector();
    java.util.Vector v3 = new java.util.Vector();
    java.util.Vector v4 = new java.util.Vector();

    Node () {


    next = null;
    nodeCount++;
    }



    }
     
    Jignesh, Jan 27, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page