I have a String which has been splitted like this

String companyinforstring ="Company name.:Company profile no 1.:Company profile no 2.:Company profile no3.:---------------------------------------------------------------:Company address, :Street number,  :Area,Area no2,  :City. ";




for (String retval: companyinforstring.split(":"))
        {
             System.out.println(retval);
             graphics.drawString(retval.toUpperCase(), 250, 200);               
          }

The Problem is that String which has been splitted, overlaping each String,How can this be solved?

Recommended Answers

All 5 Replies

what are you talking about, overlaping each String? what exactly is it you try to get?

You are drawing them all at the same position 250,200
You need to increment that position's x or y value after each print so the next string goes to a different position

String[] str1 = companyinforstring.split(":");
        for(int i=0;i<str1.length;i++)
        {
            //System.out.println("Split1 Element "+i+" :"+str1[i]);
            graphics.drawString(str1[0].toUpperCase(), 360, 50);
            graphics.drawString(str1[1].toUpperCase(), 360, 60);
            graphics.drawString(str1[2].toUpperCase(), 360, 70);
            graphics.drawString(str1[3].toUpperCase(), 360, 80);
            graphics.drawString(str1[4].toUpperCase(), 360, 90);
            graphics.drawString(str1[5].toUpperCase(), 360, 100);
            graphics.drawString(str1[6].toUpperCase(), 360, 110);
            graphics.drawString(str1[7].toUpperCase(), 360, 120);
            graphics.drawString(str1[8].toUpperCase(), 360, 130);
        }

Is there any better/simple way of doing it?
Please let me know.

Yes. Whenever you see code repeated like that, with one value changing from line to line, you just know it should be a loop. Eg::

for(int i=0;i<str1.length;i++) {
   graphics.drawString(str1[i].toUpperCase(), 360, 50 + i*10);
}

Thank you a lot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.