Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Google Guava to format Strings

I am using Google guava to format Strings and write them to a text file using BufferedWriter

import com.google.common.base.Strings;
import java.io.*;

public class Test {

    public static void main(String[] args) {
        File f = new File("MyFile.txt");
        String firstName = "STANLEY";
        String secondName = "GATUNGO";
        String thirdname = "MUNGAI";
        String location = "NAIROBI";
        String school = "STAREHE BOYS CENTRE";
        String yob= "1970";
        String marital = "MARIED";
        String texture = "LIGHT";

        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(f));
            if (!f.exists()) {
                f.createNewFile();
            }
           bw.write(Strings.padStart(firstName, 0, ' '));
           bw.write(Strings.padStart(secondName, 20, ' '));
           bw.write(Strings.padStart(thirdname, 20, ' '));
           bw.write(Strings.padStart(location, 20, ' '));
           bw.newLine();
           bw.write(Strings.padStart(school, 0, ' '));
           bw.write(Strings.padStart(yob, 20, ' '));
           bw.write(Strings.padStart(marital, 20, ' '));
           bw.write(Strings.padStart(texture, 20, ' '));


            bw.close();
        } catch (Exception asd) {
            System.out.println(asd);
        }
    }
}

My Output is My output

I need the output to be

Desired Output

I am receiving the Strings from the database and the Database And the Hardcoding Above is Just an example, I need to write the Strings Such that the Length of the First String doe not affect the Beginging Position of the second String. I need to write them in Column Manner all beginging at the same Position using Google Guava. I will Appreciate examples also.

like image 587
Stanley Mungai Avatar asked Jun 12 '26 01:06

Stanley Mungai


2 Answers

String.format

You can simply use String.format() instead of Guava. It follows the C printf syntax described here. I think that it meets your needs.

String aVeryLongString="aaabbbcccdddeeefffggghh";
String aShortString="abc";
String anotherString="helloWorld";

String formattedString=String.format("%5.5s,%5.5s,%5.5s",aVeryLongString,aShortString,anotherString);

System.out.println(formattedString);

Output :

aaabb,  abc,hello

As you can see, the long String was cut and the short String was padded.

like image 131
Arnaud Denoyelle Avatar answered Jun 14 '26 13:06

Arnaud Denoyelle


I'm not sure you can do that, although I have not used Google Guava. Here is a suggestion, write the files to a CSV, a comma separated file. To do this, simply change your output to MyFile.csv then add a comma "," after every value you write. This file can be opened in any Spreadsheet program, with each value in its own cell, hence the solution will be neat.

If this doesn't work, ie, if it must be a txt file, here is a another idea. Loop thorough every single string you have to write, find the longest, and then adjust every other string you have to that length by adding white space to it.

I hope this helps!

like image 38
RaymondMachira Avatar answered Jun 14 '26 13:06

RaymondMachira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!