Using a text editor, create a file that contains a list of five 4-digit account numbers. Read in each account number, and alter it to add a dash and a check digit, which is calculated by taking the remainder when the original number is divided by 7. Write the new account numbers to an output file.

Please give me some ideas on how can i do the string manipulation wherein i will put a DASH/" - " in between of the 4-digit numbers?



4 Years
Discussion Span
Last Post by stultuske

So far, i got the concatenation. But how can i write this on a new output file "account_numbers2.dat"?

And what is this for "and a check digit, which is calculated by taking the remainder when the original number is divided by 7."?

import java.io.*;
import java.util.*;
public class AddCheckDigit{
private static Scanner x;
    public static void main(String[]args){


    public static void openFile(){
            x = new Scanner(new File("account_numbers.dat"));
        catch(Exception e){
            System.out.println("Couldn't find file");

    public static void readFile(){
            String a = x.next();
            a = a.charAt(0) + "-" + a.charAt(1) + "-" + a.charAt(2) + "-" + a.charAt(3);
            String b = x.next();
            b = a.charAt(0) + "-" + b.charAt(1) + "-" + b.charAt(2) + "-" + b.charAt(3);
            String c = x.next();
            c = a.charAt(0) + "-" + c.charAt(1) + "-" + c.charAt(2) + "-" + c.charAt(3);
            String d = x.next();
            d = a.charAt(0) + "-" + d.charAt(1) + "-" + d.charAt(2) + "-" + d.charAt(3);
            String e = x.next();
            e = a.charAt(0) + "-" + e.charAt(1) + "-" + e.charAt(2) + "-" + e.charAt(3);

            System.out.printf("%s %s %s \n", a, b, c, d, e);

    public static void closeFile(){

Edited by techxaidz: re-edit


The check digit is used to confirm the number is correctly read/written or just valid. Think of a credit card number.

You could also use the substring() method.

Assuming you wanted just a hyphen in the middle.

Split the number into two.

        String firstTwo= number.substring(0, 2);//digits 0 and 1
        String secondTwo= number.substring(2, 4);//digits 2 and 3

Then concatenate them into a new string as you did before.

String newNum = firstTwo + "-" + secondTwo;

Edited by Starstreak


So where will i put the check digit on my program? How can i confirm if the number is valid using the check digit?

BTW, thanks for giving another idea on how to put a hypen.


A check digit is usually some sort of formula based on the digits in the number.
Here they just want you to divide the 4-digit number by 7 and get the remainder.

So you need first to change the string to a number.

Then divide it. Clue: use % to get remainder.

Edited by Starstreak


Congratulations! You're no longer a DaniWeb newbie.<br /> <br />
Your DaniWeb account has just been upgraded from newbie status and now you have the ability to take advantage of everything the community has to offer.<br /> <br />
You can now enjoy an advertisement-free DaniWeb by ticking the checkbox to Disable Ads in your profile. You will no longer have to fill out the human verification check when you post. You can also now send unlimited private messages, contribute new code snippets, and tag articles with never-before-used tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.