简体   繁体   中英

How to do java coding format in eclipse as stack-overflow coding format

I want to create code blocks or other formatted text, indent by four spaces in eclipse such like stack-overflow provide coding format.

In my eclipse coding format is

Map<String, Integer> amap = new HashMap<String, Integer>();
    try {
        BufferedReader buf = new BufferedReader(new FileReader("D:\\t.txt"));
        String ss = null;
        while ((ss = buf.readLine()) != null) {
            String[] pair = ss.split(":");
            for (int i = 0; i < pair.length; i += 2)
                amap.put(pair[i], Integer.parseInt(pair[1 + i]));
        }
        buf.close();
        for (Map.Entry em : amap.entrySet()) {
            System.out.println(" "+em.getKey() + " " + em.getValue());
        }
    } catch (Exception e) {
}

But I want it in the following format:

Map < String, Integer > amap = new HashMap < String, Integer > ();
try {
    BufferedReader buf = new BufferedReader(new FileReader("D:\\t.txt"));
    String ss = null;
    while ((ss = buf.readLine()) != null) {
        String[] pair = ss.split(":");
        for (int i = 0; i < pair.length; i += 2)
        amap.put(pair[i], Integer.parseInt(pair[1 + i]));
    }

    buf.close();
    for (Map.Entry em: amap.entrySet()) {
        System.out.println(" " + em.getKey() + " " + em.getValue());
    }
} catch (Exception e) {}

Is it possible to do such like,please help me.Besides have need any plugins for this type of code formatting in eclipse.

You can modify any formatter you like via Preferences->Java->Code Style->Formatter

All the things you show here, including the odd whitespace around the angle brackets, is handled there. Also, the weird indenting in your first example is definitely not the Eclipse default.

There really isn't a way to devolve what you describe here to some general style, but if you know the name of the style you are using (that is, the common or English name) you might be able to search for a Formatter that someone else has made.

If it is your own personal style, you don't have much choice but to create your own. Use one of the default ones as a basis for your changes. Just edit it and change the name before saving it.

My advice is to not get used to your own special format, but to adopt the style used by your shop. If you are doing this on your own, then adopt one of the other well-known and well supported formats out there. The Google Java Style is a good start.

You have to use the eclipse formatter. You can find this under Window -> Preferences -> Java -> Code Style -> Formatter. Here you can create a new profile. Edit the profile and find the settings you want to make. Here's what the formatter looks like: http://i.gyazo.com/3fe280902c62262b4bf036396af4965b.png

I hope this helped :D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM