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

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

Recommended Answers

All 3 Replies

because you do

Node top, np, last;

Then, the very next time last is seen, it is in the else block and you are attempting to dereference it.

} else {
  last.next = np;
  last = np;
  bigInt = in.nextInt();
}

Yep, as masijade pointed out, if the compiler sees that you have an execution path that could lead to a variable being used before an assignment has been made (as in your "if" clause above), it will warn you of that. If you assign a default value when you declare the variable, you'll be fine. Even if you want it to remain null you will need to declare that.

//Jkd1.6.0_02

Just a very very minor point. It should be

//Jdk1.6.0_02

JDK is Java Development Kit.
Jkd is different, they are my initials!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.