Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Ho to solve this problem using java

Posted on 2014-02-14
5
Medium Priority
?
303 Views
Last Modified: 2014-02-18
Hi Experts,
I have a requirement where I need to split a string into different lines. Max line 10, each line should have 69 characters (Including space)if the  string size is less than 680 all other lines will be left blank.  If the size is more than 680 we need to start from line 1
If less than 136
Line1  1 - 68
line2   69 – 136
Line3  blank
..
line10 blank

Each of them has a width of 68 character

Scenario #2
Total Number of Length is 1200
line      1             68
line2    69           136
line3    137        204
line4    205        272
line5    273        340
line6    341        408
line7    409        476
line8    477        544
line9    545        612
line10 613        680

Start from Line1
line      681        748
line2    749        816
line3    817        884
line4    885        952
line5    953        1020
line6    1021      1088
line7    1089      1156
line8    1157      1200
line9  blanks
line10 blanks

Thanks,
A
0
Comment
Question by:AliAjoo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39860487
A couple of for loops could handle this:

e.g.

class Splitter {
	public static void main(String[] args) {
		String input = "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890";

		for (int counter = 0; counter < input.length(); counter++) {
			for (int line = 1; line <= 10; line++) {
				System.out.print("Line " + Integer.toString(line) + " : ");

				if (counter < input.length()) {
					for (int character = 0; character < 69; character++) {
						System.out.print(input.charAt(counter));
						counter++;
	
						if (counter == input.length()) break;
					}
				}

				System.out.println();
			}
		}
	}
}

Open in new window

0
 

Author Comment

by:AliAjoo
ID: 39860877
Thanks Kufmed, it is ver helpful, I have to add it each line in an xml element.  How do I do that?

startElement("p_text");
            addNode("type", 2);
            String notes = getNotes();
            if(null!= notes){
                  System.out.println("Notes Length is " +notes);
                  for(int counter=0; counter<=notes.length();counter++){
                        for (int line = 1; line <= 10; line++) {
                              addNode("textine" + line , text will go here inside a node );
                              
                        }      
                  }

            }

            endElement("p_text");

      }


 protected void addNode(String name, int data) throws SAXException {
            addNode(name, String.valueOf(data));
    }
0
 
LVL 32

Expert Comment

by:awking00
ID: 39861647
I'm not exactly sure of your requirements. If we change the single line length limitation to 4 and the total line limitation to 40 in your example (just to make it easier for testing), then a string of 13 characters, say 'abcdefghijklm' should produce the following 10 lines?:
1-4   abcd
5-8   efgh
9-12  ijkl
13-16 m
17-20 blank
21-24 blank
25-28 blank
29-32 blank
33-36 blank
37-40 blank

And a string of 50 characters, say 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx',   should produce the following lines?:

1-4   abcd
5-8   efgh
9-12  ijkl
13-16 mnop
17-20 qrst
21-24 uvwx
25-28 yzab
29-32 cdef
33-36 ghij
37-40 klmn

and also the folloowing lines?:
41-44 opqr
45-48 stuv
49-52 wx
53-56 blank
57-60 blank
61-64 blank
65-68 blank
68-72 blank
73-76 blank
77-80 blank

If that's correct, then what do you want to do with them? Also, what is meant by including space? If that's not correct, can you provide a little more clarification. Perhaps some actual data and the anticipated output.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1600 total points
ID: 39861657
Instead of printing out the text, create new variables that hold the name and data, and assign the data to those variables. At the end of the character loop, call your node-adding code.

e.g.

class Splitter {
    public static void main(String[] args) {
        String input = "01234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890012345678900123456789001234567890";

        for (int counter = 0; counter < input.length(); counter++) {
            for (int line = 1; line <= 10; line++) {
                String name = "Line " + Integer.toString(line) + " : ";
                String data = "";

                if (counter < input.length()) {
                    for (int character = 0; character < 69; character++) {
                        data = data + input.charAt(counter);
                        counter++;
    
                        if (counter == input.length()) break;
                    }
                }

                addNode(name, data);
            }
        }
    }
}

Open in new window

0
 

Author Closing Comment

by:AliAjoo
ID: 39869323
this is exactly what i needed
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question