DAnsermino
DAnsermino

Reputation: 383

Parsing long literals from file input/variable Java

I am trying to do some large computations from a list of numbers in a .txt file. I need to use longs, specifically literal longs and therefor I need to include 'L' or 'l' at the end each time I take a new value from the file. int litterals have been leaving me with negative answers...I have tried adding a 'L' suffix each time I read the file but Long.valueOf() throws a NumberFormatException. How can I declare a parsed long as a literal long?

import java.util.*; 
import java.io.*;
public class Test{

public static void main(String[] args) throws FileNotFoundException{
    File numFile = new File("numbers.txt");
    Scanner in = new Scanner(numFile);
    int total = 0;
    Long tmp;
    while(in.hasNextLine()){
        tmp = Long.valueOf(in.nextLine().substring(0,12));
        System.out.println(tmp);
        total += tmp;
    }
}

I appreciate all the help on here but you are all kind of ignoring my question... MY QUESTION IS: How can I declare a parsed long as a literal long?(Considering I never hard wire a value). The example above is unrelated, it doesnt need debugging, it is just a brief demo of what I am trying to do.

Upvotes: 0

Views: 106

Answers (1)

Henry
Henry

Reputation: 43778

If the individual numbers are too large for int, their sum will be as well. Use also a long for the total.

Upvotes: 2

Related Questions