0

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.

4
  • 3
    You don't need to add any L anywhere. That is useful only when you want a literal long value in the code. using Long.valueOf("1234") will get you a Long. Commented Mar 7, 2015 at 8:17
  • Can you print the value of in.nextLine().substring(0,12) that's failing? Commented Mar 7, 2015 at 8:17
  • What is an example of a complete line? Commented Mar 7, 2015 at 8:29
  • There is no such type as a "literal long". A long is a long is a long. Commented Mar 7, 2015 at 9:20

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.