Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
stephen - 25 Jan 2006 10:49 GMT
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
Venky - 25 Jan 2006 12:40 GMT
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.
Jignesh - 27 Jan 2006 06:27 GMT
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 - 27 Jan 2006 06:28 GMT
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 - 27 Jan 2006 06:29 GMT
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++;
      }

}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.