1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Java linklist Code

Discussion in 'Java' started by cyrow, Nov 24, 2007.

  1. cyrow

    cyrow New Member

    Joined:
    Nov 19, 2007
    Messages:
    21
    Likes Received:
    0
    Trophy Points:
    0
    Requirements
    1) read from output file
    eg: 1222222 + 2333333
    3444444 - 9999999
    6666555 * 7
    0

    2) For addition and sutraction, Number1 and Number2 should be large integers and must be stored in a link list.
    eg.output = 1222222 + 2333333 =
    454444444(answer)

    3) For multiplication, Number1 should be stored in a link list and the small integer in a variable.
    output = 66666555 * 7 =
    43343555555(answer)

    4) Write to output file

    Code
    When I try to read from the output file, this error is generated
    !!Error :Variable last might not have been initialize
    Help me to clarify the issue becuase i don't think I have to initialize "last" as it has already been declared as a node type.

    //Jkd1.6.0_02

    Code:
    import java.io.*;
    import java.util.*;
    
    class Node{
    	int bigInt;
    	Node next;
    	
           Node(){
            bigInt = 0;
    		next = null;
    	}
    	
    	public Node(int bigInt){
    		bigInt = 0;
    		next = null;
    	}
    	
    }
    public class BuildList{
    	public static void main(String[] args)throws IOException{
    	
    	Scanner in = new Scanner(new FileReader("input.txt"));	
    	FileWriter out = new FileWriter("output.txt") ;
    	
    	Node top, np, last;
    	top = null;
    	
    	//Reading data from input.txt
    	int bigInt = in.nextInt();
    	while(bigInt!=0){
    		
    		if (bigInt > 100){//only bigInt numbers are added to the list for add, subtract and multiply
    			
    			np = new Node(bigInt);
    		if (top==null){
    			top = np;
    		}else{
    			last.next = np;
    			last = np;
    			bigInt = in.nextInt();
    		}
    	}			
    	}
    			
    }//end main
    }//end class check
     
    Last edited by a moderator: Nov 25, 2007

Share This Page