Search code examples
javaarraylistiobufferedreaderfilereader

Java: Keeping leading zeros while reading and formatting a text file


I am having trouble figuring out how to read in a file, line by line, and then format it. The big issue is that I have a text file with a "number" assigned to a "person". The first number is "001". When I try to read it, it only saves as "1". It does the same thing with all leading zeros in my numbers. "002" becomes "2", "030" becomes "30", and so on. Each number has three digits and I want to keep it that way.

Here is the bit of code where I read from the file:

do {
    next = Breader.readLine();
    sNumber = Integer.parseInt(next);
    nameArray.add(sNumber);
    sName = Breader.readLine();
    nameArray.add(sName);
    end = Breader.ready();
} while(end);

It must be something in the Integer.parseInt(); function.

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;

public class FinalProgram {
    public static void main(String[] args)  IOException {
        nameReader();
    }

    private static void nameReader() throws IOException {
        String nameFile = " ";
        String next = " ";
        int sNumber = 0;
        String sName = " ";
        Scanner input = new Scanner(System.in);
        ArrayList nameArray = new ArrayList();

        try {
            System.out.print("Enter the Name file(c:filename.txt): ");
            nameFile = input.nextLine();
        } catch(IllegalArgumentException e) {
            System.out.printf("Invalid input. Please enter"
                    + " filename in the form of "
                    + "c:filename.txt\n", e.getMessage());
        }

        //Instantiate FileReader and BufferedReader
        FileReader freader = new FileReader(nameFile);
        BufferedReader Breader = new BufferedReader(freader);
        boolean end = Breader.ready();

        do {
            next = Breader.readLine();
            sNumber = Integer.parseInt(next);
            nameArray.add(sNumber);
            sName = Breader.readLine();
            nameArray.add(sName);
            end = Breader.ready();
        } while(end);

        System.out.print(nameArray);
    }
}

Solution

  • If you parse a string to an integer like in int sNumber = Integer.parseInt(next);, leading zeros will be ignored, since an integer does not have leading zeros, it is superfluous. Why should it store the zeros, would you write 030 instead of 30 on a piece of paper, too? How many zeros should be stored, one, two, ten? How would you compare 0030, 030 and 30, are they the same?

    As you can see, what you mean is how to format as string, an integer is never stored with leading zeros. You can do so, by using String.format(...);:

    int yourInteger = 30;
    String formattedNumber = String.format("%03d", yourInteger);
    
    // Prints 30 as 030
    System.out.println(formattedNumber);
    

    The format string "%03d" means: Print an integer, print at least three digits and if there are less than three digits, pad them with leading zeros. Consequently, 30 is printed as 030, 5 as 005 and 3000 as 3000, since there are more than three digits.

    Another more advanced method to format numbers in general, is NumberFormat.