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 5 of 5

Thread: Linked list insertion by console

  1. #1
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Linked list insertion by console

    Whats wrong with my code?
    While compiling:



    LinkedListInsertion.java:40: error: cannot find symbol
    arr[i]=Integer.parseInt(br.readline());
    ^
    symbol: method readline()
    location: variable br of type BufferedReader
    1 error



    code is as follos:
    import java.io.*;
    public class LinkedListInsertion{
    Node head;
    static class Node{
    Node next;
    int data;
    Node(int data,LinkedListInsertion list){
    this.data = data;
    next = null;
    }
    }
    public static LinkedListInsertion insert(LinkedListInsertion list,int data){
    Node new_node=new Node(data,list);
    Node last;
    if(list.head==null){
    list.head = new_node;
    }
    else{
    last = list.head;
    while(last.next!=null){
    last = last.next;
    }
    last.next = new_node;
    }
    return list;
    }
    public static void printList(LinkedListInsertion list){
    Node currNode = list.head;
    System.out.print("LinkedListAB: ");
    while (currNode != null){
    System.out.print(currNode.data + " ");
    currNode = currNode.next;
    }
    }
    public static void main(String[] args){
    int[] arr=new int[5];
    LinkedListInsertion mylist=new LinkedListInsertion();
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    for(int i=1; i<=5; i++){
    arr[i]=Integer.parseInt(br.readline());
    mylist = insert(mylist,arr[i]);
    }
    printList(mylist);
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked list insertion by console

    LinkedListInsertion.java:40: error: cannot find symbol
    arr[i]=Integer.parseInt(br.readline());
    ^
    symbol: method readline()
    location: variable br of type BufferedReader
    Does the BufferedReader class have the method readline?

    Note: Spelling and case are important. All letters must match.



    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2019
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Linked list insertion by console

    your advice of spell check solved half of the problem.
    But now while compiling:
    LinkedListInsertion.java:75: error: unreported exception IOException; must be caught or declared to be thrown
    arr[i]=Integer.parseInt(br.readLine());
    1 error


    import java.io.*;
    public class LinkedListInsertion{
    	Node head;
    	static class Node{
    		Node next;
    		int data;
    		Node(int data,LinkedListInsertion list){
    			this.data = data;
    			next = null;
    		}
    	}
    	public static LinkedListInsertion insert(LinkedListInsertion list,int data){
    		Node new_node=new Node(data,list);
    		Node last;
    		if(list.head==null){
    			list.head = new_node;
    		}
    		else{
    			last = list.head;
    			while(last.next!=null){
    				last = last.next;
    			}
    			last.next = new_node;
    		}
    		return list;
    	}
    	public static LinkedListInsertion deleteAtEnd(LinkedListInsertion list){  
    		Node curr_node=list.head;
    		if(curr_node.next==null){
    				list.head=curr_node.next;
    				return list;
    		}
    		Node prev_node=list.head;
    		while(curr_node.next!=null){
    			prev_node=curr_node;
    			curr_node=curr_node.next;
    		}
    		prev_node.next=null;
    		return list;
    	}
    	public static LinkedListInsertion deleteByKey(LinkedListInsertion list,int key){
    		Node tmp=list.head;
    		if(tmp!=null && tmp.data==key){
    			list.head=tmp.next;
    			return list;
    		}
    		Node prev=null;
    		while(tmp.data!=key && tmp!=null){
    			prev=tmp;
    			tmp=tmp.next;
    		}
    		if(tmp!=null){
    			prev.next=tmp.next;
    		}
    		if(tmp==null){
    			System.out.println("Key not found");
    		}
    		return list;
     
    	}
    	public static void printList(LinkedListInsertion list){
    		Node currNode = list.head; 
            System.out.print("LinkedList: "); 
    		while (currNode != null){
    			System.out.print(currNode.data + " "); 
    			currNode = currNode.next; 
    		}
    		System.out.println();
    	}
    	public static void main(String[] args){
    		int[] arr=new int[5];
    		LinkedListInsertion mylist=new LinkedListInsertion();
    		BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    		for(int i=1; i<=5; i++){
    			arr[i]=Integer.parseInt(br.readLine());
    			mylist = insert(mylist,arr[i]);
    		}
    		printList(mylist);
     
    		}
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked list insertion by console

    LinkedListInsertion.java:75: error: unreported exception IOException; must be caught or declared to be thrown
    arr[i]=Integer.parseInt(br.readLine());
    The API doc for the method public String readLine() throws IOException says it throws an exception. Your code must handle that exception.
    Two main ways:
    Put the code in a try {} catch block to handle the exception locally
    Declare the method containing the code to throws that exception to pass the responsibility to that method's caller.

    Take a look at the Java tutorial: https://docs.oracle.com/javase/tutor...ons/index.html
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Linked list insertion by console

    Also posted here: https://coderanch.com/t/719905/java/...showing-errors
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 7
    Last Post: March 8th, 2013, 09:21 PM
  2. [Linked List] Problems deleting items from a linked list
    By KLVTZ in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 07:52 PM
  3. Replies: 1
    Last Post: October 25th, 2012, 02:03 PM
  4. Insertion Sort on a Doubly Linked List
    By Kaisshau in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 7th, 2011, 11:18 PM
  5. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 09:25 AM