Solved

Email Client/Server : Splitting up A String

Posted on 2004-04-04
13
252 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

757 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now