Solved

Email Client/Server : Splitting up A String

Posted on 2004-04-04
13
282 Views
Last Modified: 2008-03-03
The server stores the message as a string. When the client selects an email to read, i want to display the message but only 50 characters per line. So, a few questions.
1. Should the server send 50 characters at a time, and then the client can just readLine and print. If so, whats the best way for the server to split the string and send.
2. Should the server send the whole string, and then client can split it up and print. I dont see a real difference except that in a real world application you would want the client doing more of the work.
3. Is there a better way?

Thanks
0
Comment
Question by:tricks801
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752877
The number of characters per line belongs to the view of a data model. The data model in this case is the message string. Depending on what is inspecting this message, you can change the view to display as many characters per line you wish. Typically this is done in the real world by an email client and is implemented by line wrap in the message display area. The data model remains constant
0
 

Author Comment

by:tricks801
ID: 10752895
right...but im using the console, and would  not like to scroll right to read the long string.  
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752913
So the console view could be something like

a. Put the message into a StringBuffer
b. Insert a newline character '\n' every 50 characters
c. Turn it back into a String and print it

or

a. Iterate the char array (char[] messageChars = message.toCharArray())
b. System.out.print(......the message char or if index % 50 == 0, a newline character)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 10752932
Personally i prefer the second one:


char[] messageChars = message.toCharArray();
for(int i = 0;i < messageChars;i++) {
      if (i % 50 == 0 && messageChars[i] != '\n') {
            System.out.print('\n');
      }
      else {
            System.out.print(messageChars[i]);      
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10752938
But don't leave that literal 50 in the real thing of course
0
 

Author Comment

by:tricks801
ID: 10753077
so this should happen client side and the server is still going to send one long string? Is this the message in message.toCharArray()?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 10753080
Yes
0
 
LVL 30

Assisted Solution

by:GrandSchtroumpf
GrandSchtroumpf earned 200 total points
ID: 10753346
CEHJ, your code would break the original formatting.
I think something like this would be more appropriate:

for(int i = 0; i < messageChars.length; i++, linechars++) {
     System.out.print(messageChars[i]);
     if (messageChars[i] == '\n') {
          linechars = 0;
     }
     if (linechars >= 50) {
          linechars = 0;
          System.out.print('\n');
     }
}
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10753358
with a declaration and initialization for my linechars variable it would be even better:
int linechars = 0; // before the for loop;
0
 
LVL 92

Assisted Solution

by:objects
objects earned 100 total points
ID: 10753604
int LEN = 50;

while (line.length()>LEN)
{
    System.out.println(line.substring(0, LEN));
    line = line.substring(LEN);
}
if (line.length()>0) System.out.println(line);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 10824325
8-)
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10826081
:°)
0
 
LVL 92

Expert Comment

by:objects
ID: 10827864
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adding multiple JVM environments to RedHat 6 7 69
CSV file parsing thru Java 13 40
maven disable workspace resolution 1 41
how to debug htl and js pages 8 57
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

733 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