Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

String Manipulation 1

Status
Not open for further replies.

Rhoon

Programmer
Mar 23, 2001
55
US
Hey all,

Anyone have any REALLY good web sites that deal with String Manipulation. I'm trying to pull text off of a server and format it to the screen. I'm trying to do word wrapping in my application after 80 characters. IF anyone has any tips/tricks on this, please let me know.

Thanks in advance,

Andrew
 
Hi Andrew,

Actually I don't know any good string manipulation pages, but here is a one suggestion. If I understood right, you receive some text from server and you want to print that to the clients screen. In that case, the following example should work.
________________________________

char[] buff = new char[81]; // room for newline character
BufferedReader br = ....
StringBuffer contents = new StringBuffer();
int len = 0;

while((len = br.read(buff, 0, 80)) != -1)
{
buff[len] = 10; // 10 == new Line (in decimal ascii)
contents.append(buff, 0, len);
}

System.out.println(contents);

_________________________________________

-Vepo
 
Vepo, thanks for the previous help, was hoping I could get some more help with this one. I'm having problems getting the text out to the screen. If I leave Window.WriteToScreen(contents.toString); where it is, it doesn't output anything, but if I put it inside of the while loop, it just spams the screen. Anythoughts? There's alot more code than I'm showing, just thought I'd spare all the menu's and stuff. Thanks in advance.


Rhoon


public void WriteToScreen(String s) {
output.append(s);
}

public static void main (String[] args) {


TelnetApp window = new TelnetApp();
window.setTitle("Telnet Application");
window.setSize(450, 260);
window.setVisible(true);
int port = 8000;
String server = "server name";
String fromServer = new String();

try {

Socket socket = new Socket(server, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);

char[] buff = new char[81];
StringBuffer contents = new StringBuffer();
int len = 0;
while ((len = in.read(buff,0,80)) != -1) {
buff[len] = 10;
contents.append(buff,0,80);
} //End While
window.WriteToScreen(contents);
} // End Try
 
Hi,

What is that output Object you are using at writeToScreen()?
If suppose it is e.g. TextField(?), so try this:

public class TelnetApp extends Frame
{
public void WriteToScreen(String s)
{
output.setText(s);
}

public static void main(String[] argv)
{
....
output = new TextField();
....

while(...)
{
....
}
window.WriteToScreen(contents.toString());
} // main

private TextField output;
} // class TelnetApp


... this should do at least something ;)

-Vepo
 

output.setText(s); did do something, unfortunately it did the exact same as System.out.println(); I've included all the code this time in hopes that there's something that I'm not seeing correctly or I've implemented in such a small way that the compiler and/or program wouldn't notice it until the output to the screens are called in. Let me know what you think, in case anyone is confused as to what I'm trying to do, this code will create a Telnet to a server, of which I've blocked the name out, but I'm unable to put the information from the server into the JTextField, it only goes to the command prompt from which I've run the program. Any and all help is much appreciated.

Thanks in advance,

Andrew


import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;
import java.net.*;
import java.util.*;

public class TelnetApp extends JFrame implements ActionListener {

JTextArea output;
JScrollPane scrollPane;
JTextField input;

public TelnetApp() {

JMenuBar menuBar;
JMenu menu;
JMenuItem menuItem;

//construct the frame
JFrame frame = new JFrame("Telnet Application");
addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) {System.exit(0); }});

//Add regular components to the window, using the default BorderLayout.
Container contentPane = getContentPane();
output = new JTextArea(5, 30);
output.setEditable(false);
scrollPane = new JScrollPane(output);
contentPane.add(scrollPane, BorderLayout.CENTER);
input = new JTextField(200);
input.addActionListener(this);
input.setActionCommand("Text");
contentPane.add(input);

// Menu Bar setup

menuBar = new JMenuBar();
setJMenuBar(menuBar);
menu = new JMenu("File");
menu.setMnemonic(KeyEvent.VK_F);
menuBar.add(menu);

// Menu Items under "File"

menuItem = new JMenuItem("Connect");
menuItem.addActionListener(this);
menuItem.setActionCommand("Connect");
menu.add(menuItem);
menuItem = new JMenuItem("Disconnect");
menuItem.addActionListener(this);
menuItem.setActionCommand("Connect");
menu.add(menuItem);
menuItem = new JMenuItem("Exit");
menuItem.addActionListener(this);
menuItem.setActionCommand("Exit");
menu.add(menuItem);

// Menu Setup for "Edit"

menu = new JMenu("Edit");
menu.setMnemonic(KeyEvent.VK_E);
menuBar.add(menu);
menuItem = new JMenuItem("Copy");
menuItem.addActionListener(this);
menuItem.setActionCommand("Copy");
menu.add(menuItem);
menuItem = new JMenuItem("Cut");
menuItem.addActionListener(this);
menuItem.setActionCommand("Cut");
menu.add(menuItem);
menuItem = new JMenuItem("Paste");
menuItem.addActionListener(this);
menuItem.setActionCommand("Paste");
menu.add(menuItem);



//Menu Setup for "Help"

menu = new JMenu("Help");
menu.setMnemonic(KeyEvent.VK_H);
menuBar.add(menu);
menuItem = new JMenuItem("Help");
menuItem.addActionListener(this);
menuItem.setActionCommand("Help");
menu.add(menuItem);
menu.addSeparator();
menuItem = new JMenuItem("About");
menuItem.addActionListener(this);
menuItem.setActionCommand("About");
menu.add(menuItem);


} //End Telnet App

public void WriteToScreen(String s) {
output.write(s);
}


public void actionPerformed(ActionEvent e) {
String source = (String) (e.getActionCommand());
if (source == "Exit") {
System.exit(0);}
else if (source == "Connect") {}
else if (source == "Disconnect") {}
else if (source == "Copy") {}
else if (source == "Cut") {}
else if (source == "Paste") {}
else if (source == "Help") {}
else if (source == "About") {}
else if (source == "Text") {
String text = input.getText();
/* out.println(text); */
}
}




public static void main (String[] args) {


TelnetApp window = new TelnetApp();
window.setTitle("Telnet Application");
window.setSize(450, 260);
window.setVisible(true);
int port = 8000;
String server = &quot;<Server name>&quot;;
String fromServer = new String();

try {

Socket socket = new Socket(server, port);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()),true);

char[] buff = new char[81];
StringBuffer contents = new StringBuffer();
int len = 0;
while ((len = in.read(buff,0,80)) != -1) {
buff[len] = 10;
contents.append(buff,0,80);
System.out.println(contents);
}
window.WriteToScreen(contents.toString());
}

catch (UnknownHostException e) {
window.WriteToScreen(&quot;Must be connected to the Internet.&quot;);
}
catch (IOException e) {

}


}
}
 
I'd like to try and help, but I'm a little confused about what the actual problem is!

My understanding of the problem is that the call to window.WriteToScreen( String ) does not wrap long strings being output to the console window.

I wonder if the problem with the code above is one of those irritating counting issues...

for all but the last loop, the statement:

len = in.read( buff, 0, 80 )

will set len = 80 ( the number of bytes retrieved ).

buff[80]=10 will set the 81st array element to CR due to the way arrays work.

But contents.append( buff, 0, 80 ) will only append the first 80 elements of the array, missing off the 81st one.

Perhaps using: contents.append( buff, 0, 81 ) would work better?

I might be barking completely the wrong tree...

 
My problem is that when the application is run, there's a JTextfield created, the word wrapping was solved very nicely(Much thanks Vepo). If you run this program from a DOS prompt, you'll find that the program outputs information from the server to the DOS prompt and not the JTextField that was created. Basic Telnet Application style. There in lies my problem, I'm looking at doing something with the static on main() but have found the program will not run without it and I dont think the constructor and everything else should be declared static then I'm back to procedural programming. The Answer I'm looking for answers this:

How can I print the information that comes back from the server into the JTextField.

Much thanks to anyone that gives it a go.

Andrew
 
Sorry, in my last post I Was saying JTextField, its actually a JTextArea. Appologies for the confusion.

Andrew
 
I just imported this code into VAJ 4.0 just because its late and I can't sleep. 4.0 complains that &quot;The method &quot;write&quot; invoked for type javax.swing.JTextArea with arguments (java.lang.String) is not defined. I'll see what I can come up with.
 
It's just a hunch, but perhaps your object of &quot;out&quot; should not refer to the &quot;System&quot;.out.println(contents.toString), but rather out.println(contents.toString()); I ran it within the IDE and it seems to work fine. As before I fixed that, I was getting text printed out to my console. Also as I noted about the complaint of the write method used in your WriteToScreen(String). It should be output.setText(s);

HTH

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top