Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Anyone can teach me whats wrong with my program ... (Linked List)

  1. #1
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Anyone can teach me whats wrong with my program ... (Linked List)

    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
    Attached Files Attached Files
    Last edited by darkagui23; January 11th, 2014 at 08:58 PM. Reason: wrong format


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,392
    My Mood
    Mellow
    Thanks
    143
    Thanked 599 Times in 586 Posts

    Default Re: Anyone can teach me whats wrong with my program ... (Linked List)

    Welcome to the forum. Which method do you think is erasing the list?

  3. #3
    Junior Member
    Join Date
    Jan 2014
    Posts
    2
    My Mood
    Confused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Anyone can teach me whats wrong with my program ... (Linked List)

    i think in startpage() .... but i can't identify where is it

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,392
    My Mood
    Mellow
    Thanks
    143
    Thanked 599 Times in 586 Posts

    Default Re: Anyone can teach me whats wrong with my program ... (Linked List)

    Since that's the only method called by your main() method, I suppose that's a good guess, but it isn't very helpful. Let's try to understand your question another way . . .

    What are you seeing when you run your program to cause you to conclude that "the stored data is erased?" Post a sample run that shows the problem (just copy the console output and post it) or describe the user actions necessary to duplicate what you're seeing and then describe how the resulting output should be different.

    Ahhh . . .

    Looking at your code a bit, I see a new LinkedList is created every time the operation() method is called. That means every time your program enters operation(), a new, BLANK LinkedList is being created and used by the rest of that method. Instead, you need the LinkedList to be declared and initialized outside that method so that user actions inside that method on the LinkedList are retained and have a cumulative effect.

Similar Threads

  1. Whats wrong with my program?
    By nickar1172 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2013, 08:41 PM
  2. Remove at index method for singly linked list... what's wrong?
    By EDale in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 27th, 2013, 09:12 PM
  3. Need a bit of help with Linked List program
    By slinky29 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 20th, 2013, 11:44 PM
  4. what's wrong with my linked list
    By amr in forum What's Wrong With My Code?
    Replies: 10
    Last Post: February 13th, 2011, 01:55 PM
  5. whats wrong with my program??
    By jrodriguo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 11th, 2010, 06:16 AM