简体   繁体   中英

How do i compare a Long variable to null

So im making a txt file reader, and the file has a bunch of "long" lines, because it is a prime number finder program, and it writes the numbers to the txt. when i stop the program, and then restart it, i want it to start where it left off. that means that i have to have it check each line and make sure that it is reading something, and the buffered reader is listed as long. here is the code:

try{
        FileReader fileReader = new FileReader(fileLocation);
        BufferedReader bufferedReader = new BufferedReader(fileReader);

        long stringRead = bufferedReader.read();

        while (stringRead != null)
        {
            stringRead = bufferedReader.read();
            currentNum =  stringRead;
        }

        bufferedReader.close();
        }
        catch(FileNotFoundException filenotfoundexxeption){
            System.out.print("file does not exist");
        }
        catch(IOException ioexception){
            ioexception.printStackTrace();
        }
}

so the problem is that it doesn't like the stringRead != null , and i dont know how to do it otherwise. im a noob, and so please answer using the code already listed. Thanks!

You should probably be doing

String line;
while ((line = bufferedReader.readLine()) != null)
{
    currentNum = Long.parseLong(line);
}

As readLine() will return null when the end of file is reached.

A long is a primitive type, so it cannot be null . On the other hand, a Long is a reference type, so it can be null .

Either way, BufferedReader#read() returns an int representing a single character, not an entire line. You should use #readLine() combined with Long#parseLong() ,

Think about using a Scanner instead, which will significantly simplify your code.

Scanner scanner = new Scanner(new File(fileLocation));

while (scanner.hasNextLong())
{
    currentNum = scanner.nextLong();
}

A long can't be null, it is either 0 (not initialised, or has been assigned 0) or not. You probably want to use the readLine method and parse the result to a long using Long.parseLong() , rather than reading each character with read .

long is a primitive datatype , which cant be null. if its not initialized its 0 . However you can use the Long wrapper class. For example Long stringRead = new Long(bufferedReader.read()); . In your case however that would never be the case, because read() never returns null , just -1 if its the end of the file. So your check should be while(stringRead!=-1)

You have one number per line? You have to read with readLine , that's going to give you the line as a String. Then parse that with Long.parseLong . And you can check that the string you get from readLine is not null.

According to the official documentation BufferedReader#read returns int.

The character read, as an integer in the range 0 to 65535 (0x00-0xffff), or -1 if the end of the stream has been reached

Which means that you can check if the returned is not equals to -1.

long is a primitive so it can not be null and also BufferedReader.read returns int so you can simply make it work by change this line

long stringRead = bufferedReader.read();

to

Integer stringRead = bufferedReader.read();

or

 Long stringRead = new Long(bufferedReader.read());

Java 1.5 and above automatically wrap int to Integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM