0

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.

7
  • 4
    Probably not the best site to ask this question. Look in your code analysis settings to see if there are options for it. For more intelligible answers, try asking on SuperUser. Commented Jan 9, 2015 at 20:23
  • thank you so much.but i have no superuser in my university.so i have to face problem. Commented Jan 9, 2015 at 20:29
  • 2
    superuser.com Commented Jan 9, 2015 at 20:30
  • 1
    I feel I agree with you, @MeetTitan however, close as off-topic says "Questions about general computing hardware and software are off-topic for Stack Overflow unless they directly involve tools used primarily for programming." (change in emphasis is mine) Commented Jan 9, 2015 at 20:39
  • 1
    Excuse my ignorance, I have to agree with you guys. Commented Jan 9, 2015 at 20:44

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.