i dont know how to post like others but ... i upload my code ....
can somebody tell whats my problem in my program...
i think after finish doing a method ... the stored data is erased ...
public class Node { private Node next; private int studNo; private String fName; private String lName; public void setNo(int sn){ studNo = sn; } public int getNo(){ return studNo; } public void setfName(String fn){ fName = fn; } public String getfName(){ return fName; } public void setlName(String ln){ lName = ln; } public String getlName(){ return lName; } public void setNext(Node n){ next = n; } public Node getNext(){ return next; } }
public class SinglyLinkedList { private Node first; public void add(int v , String g , String x){ Node p = new Node(); p.setNo(v); p.setfName(g); p.setlName(x); if (first == null){ first = p; } else { Node q = first; while (q.getNext() !=null){ q=q.getNext(); } q.setNext(p); } }// end of creating a new node and storing it. public void search(int v){ Node q = first; while (q != null){ if (v == q.getNo()){ System.out.println("\nFirst Name " + q.getfName() + "\nLast Name " + q.getlName()); } else { q = q.getNext(); } }//if this is false ... not excute the next statement }// end of searching public String display(){ String strData = ""; Node temp = first; while (temp != null){ strData = temp.getNo() + "\t" + temp.getfName() + "\t" + temp.getlName(); temp = temp.getNext(); } return strData; }// end of displaying a data }
import java.util.Scanner; public class LabEx1Ruamar { public static void main(String[] args) { startPage();//calling a method System.out.println("Program Terminated..."); }// end of main method public static void startPage(){ Scanner input = new Scanner(System.in); char exit = 'Y'; do { int x = 0; operation();// calling another method System.out.print("\nTry again? (y/n): "); String exitType = input.next(); do{ if (exitType.length()==1){ char exitChar = exitType.charAt(0); exit = exitChar; if (exit == 'Y' || exit == 'y' || exit == 'N' || exit == 'n'){ x = 1; }else { System.out.println("Invalid input...\n"); System.out.print("Try again? (y/n): "); exitType = input.next(); x = 0; } }else { System.out.println(); System.out.println("Invalid input...\n"); System.out.print("Try again? (y/n): "); exitType = input.next(); x = 0; } }while(x==0); }while(exit == 'Y' || exit == 'y'); }// end of a method public static void operation(){ Scanner input = new Scanner(System.in); SinglyLinkedList happy = new SinglyLinkedList(); System.out.print("Choose Transaction\n\tA. Enter new student\n\tB. Search student\n\tC. Display all students\n\tD. Exit"); System.out.print("\nEnter Choice: "); String p = input.next(); switch(p.charAt(0)){ case 'A': System.out.print("Enter student no: "); int boy = input.nextInt(); System.out.print("Enter first name: "); String girl = input.next(); System.out.print("Enter last name: "); String kill = input.next(); happy.add(boy, girl , kill);// storing it to node System.out.print("SAVED"); break; case 'B': System.out.print("Enter student no: "); int hannah = input.nextInt(); happy.search(hannah);//searching break; case 'C': System.out.print("\t" + happy.display());//display break; //i dont do the letter "D" because something wrong in other cases.. default: System.out.println("Wrong Choice"); break; }//end of switch case }// end of operation(a method) }// end of class