Solved

Email Client/Server : Splitting up A String

Posted on 2004-04-04
13
277 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

827 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