Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java padding a string read from a file

Tags:

java

string

Ok so I have a file that reads integers, from a file that looks like this. 123456-1324563.

The file reads these numbers as a string and I'm trying to figure out how to create a method that appends the number 0 to whichever side of the number being read is less than the other side.

For example if there are less numbers on the left side of the operator than on the right it would add 0's to the string so that the two numbers become even and return the new String. So I would need the method to turn a String like 123456789-123456 into 123456789-000123456. But it would need to determine which side is shorter and pad 0's in front of it but still return the entire string.

Edit:

This is my most updated version of this method, that I'm using and when the + operator is passed in I'm getting an ArrayIndexOutOfBoundsException. But it works perfectly find with the - operator.

public String pad(String line, String operator){
    String str[] = line.split(Pattern.quote(operator));

    StringBuilder left = new StringBuilder(str[0]);
    StringBuilder right = new StringBuilder(str[1]);
    left = left.reverse();
    right = right.reverse();
    int len1 = left.length();
    int len2 = right.length();
    if(len1>len2){
        while(len1!=len2){
            right.append("0");
            len1--;
        }
    }else{
        while(len1!=len2){
            left.append("0");
            len2--;
        }
    }
    return left.reverse().toString()+operator+right.reverse().toString();
}
like image 930
CLR45 Avatar asked Oct 15 '15 22:10

CLR45


2 Answers

A simple solution:

public static String pad_num(String line){
    String[] groups = line.split("-");
    String left = groups[0],
            right = groups[1];

    if(left.length() < right.length()) {
        do {
            left = '0'+left;
        } while (left.length() < right.length());
    } else if(right.length() < left.length()) {
        do {
            right = '0'+right;
        } while (right.length() < left.length());
    }

    return left+'-'+right;
}

If you don't have fixed operator, you can pass it as an argument:

public static String pad_num(String line, String operator){
    //prevent ArrayIndexOutOfBoundsException
    if(!line.contains(operator)) {
        return line;
    }

    //prevent PatternSyntaxException by compiling it as literal
    String[] groups = Pattern.compile(operator, Pattern.LITERAL).split(line);

    //do the same
    ...

    return left+operator+right;
}
like image 50
Ron C Avatar answered Oct 01 '22 21:10

Ron C


Using java 8, more concise and compact version

public  String pad_num(String line, String operator){
    String str[] = line.split(Pattern.quote(operator));
    char []buff = new char[Math.abs(str[0].length()-str[1].length())];
    Arrays.fill(buff, '0');
    if(str[0].length()>str[1].length()){            
        return String.join(operator, str[0], new StringBuilder(String.valueOf(buff)).append(str[1]));
    }else{
        return String.join(operator, new StringBuilder(String.valueOf(buff)).append(str[0]), str[1]);
    }
}

If you don't have java8, use this implementation of your method

public  String pad_num(String line, String operator){
    String str[] = line.split(Pattern.quote(operator)); 
    StringBuilder left = new StringBuilder(str[0]);
    StringBuilder right = new StringBuilder(str[1]);
    left = left.reverse();
    right = right.reverse();
    int len1 = left.length();
    int len2 = right.length();
    if(len1>len2){
        while(len1!=len2){
            right.append("0");
            len1--;
        }
    }else{
        while(len1!=len2){
            left.append("0");
            len2--;
        }
    }
    return left.reverse().toString()+operator+right.reverse().toString();

}

And call those method as

String result = pad_num("123456+1324563", "+");
String result = pad_num("123456-1324563", "-");

Edit:

Reason of ArrayIndexOutOfBoundsException is method calling with mismatch arguments like

pad_num("123456-1324563", "+");   // - in line and + in operator

or

pad_num("123456+1324563", "-");   // + in line and - in operator
like image 44
ashiquzzaman33 Avatar answered Oct 01 '22 20:10

ashiquzzaman33