String to CharBuffer

Hi @all i'm new in the world of Java,

but i have a problem to convert a String into CharBuffer. I wrote a programm that send info over network using Sockets. To send is no problem but to read, i became the message that i have to convert String into CharBuffer. I really don't know if it is possible. Thank you for the help.

The code is:

import java.net.*;

import java.io.*;

import java.nio.CharBuffer;

public class ADatenClient {

public static void main(String args[]){

Socket server = null;

int BallX;

int BallY;

int SchlgX;

int SchlgY;

BallX = balla.getBall_x_pos();

BallY = balla.getBall_y_pos();

Schlaeger Schl2 = new Schlaeger;

SchlgX = Schl2.getSchl_x_pos();

SchlgY = Schl2.getSchl_y_pos();

try{

server = new Socket("localhost", 50000);

InputStream in = server.getInputStream();

OutputStream out = server.getOutputStream();

OutputStreamWriter out1 = new OutputStreamWriter(out);

OutputStreamWriter out2 = new OutputStreamWriter(out);

OutputStreamWriter out3 = new OutputStreamWriter(out);

OutputStreamWriter out4 = new OutputStreamWriter(out);

out1.write("<ballx>"+Integer.toString(BallX)+"</ballx>");

out2.write("<bally>"+Integer.toString(BallY)+"</bally>");

out3.write("<schlaegerx>"+Integer.toString(SchlgX)+"</schlaegerx>");

out4.write("<schlaegery>"+Integer.toString(SchlgY)+"</schlaegery>");

InputStreamReader in1 = new InputStreamReader(in);

InputStreamReader in2 = new InputStreamReader(in);

InputStreamReader in3 = new InputStreamReader(in);

InputStreamReader in4 = new InputStreamReader(in);

/*in1.read("<ballx>"+CharBuffer.wrap(BallX)+"</ballx>");

in2.read("<bally>"+CharBuffer.wrap(BallY)+"</bally>");

in3.read("<schlaegerx>"+CharBuffer.wrap(SchlgX)+"</schaegerx>");

in4.read("<schlaegery>"+CharBuffer.wrap(SchlgY)+"</schaegery>");

*/

}catch(UnknownHostException e){

e.printStackTrace();

}catch(IOException e){

e.printStackTrace();

}

finally{

if(server != null)

try{

server.close();

} catch(IOException e){

e.printStackTrace();

}

}

}

}

[2378 byte] By [Illyriahna] at [2007-11-27 10:00:30]
# 1

you can make charbuffers from Strings or ints like so:

int myInt = 4;

String foo = "hello world";

CharBuffer myCB = CharBuffer.wrap(foo.toCharArray()); // for a string

CharBuffer myCB2 = CharBuffer.wrap(String.valueOf(myInt).toCharArray()); // for an int

By the way, your code screams to me "I need to use arrays!".

Message was edited by:

petes1234

petes1234a at 2007-7-13 0:31:49 > top of Java-index,Java Essentials,Java Programming...
# 2

> Hi @all i'm new in the world of Java,

> but i have a problem to convert a String into

> CharBuffer.

String myString = "42"

CharBuffer wrapped = CharBuffer.wrap(myString)

See also : http://java.sun.com/javase/6/docs/api/java/nio/CharBuffer.html#wrap(java.lang.CharSequence)

FAMUGamblera at 2007-7-13 0:31:49 > top of Java-index,Java Essentials,Java Programming...
# 3

Thank for answer.

I put it so:

CharBufferBalX;

in1.read("<ballx>"+BalX.wrap(String.valueOf(BallX).toCharArray() +"</ballx>");

And Eclipse says : The method read(CharBuffer) in the type Reader is not applicable for the arguments (String)

I have no idea, what can i do.

Thank you!

Illyriahna at 2007-7-13 0:31:49 > top of Java-index,Java Essentials,Java Programming...
# 4

> Thank for answer.

> I put it so:

>

> CharBufferBalX;

>

> in1.read("<ballx>"+BalX.wrap(String.valueOf(BallX).toC

> harArray() +"</ballx>");

>

> And Eclipse says : The method read(CharBuffer) in the

> type Reader is not applicable for the arguments

> (String)

>

> I have no idea, what can i do.

>

> Thank you!

what about:

in1.read(BalX.wrap(("<ballx>" + BallX + "</ballx>").toCharArray() );

but heck, I'm no expert in this area...

petes1234a at 2007-7-13 0:31:49 > top of Java-index,Java Essentials,Java Programming...
# 5

Thank you for helping.

Now i become no errors but it's not static from Eclipse:

The static method wrap(char[]) from the type CharBuffer should be accessed in a static way

But your help can make me to solve this problem. Java it's OK, but i don't have any experience in this world.

Illyriahna at 2007-7-13 0:31:50 > top of Java-index,Java Essentials,Java Programming...
# 6

You don't have to convert the String to a char[] to use the CharBuffer.wrap() method. Its overloaded to accept a CharSequence, String class implements the CharSequence interface, so:

CharBuffer cb = CharBuffer.wrap("aString");

is sufficient. To the OP, perhaps you should break that single line up into two or three lines so that its easier to understand.

FAMUGamblera at 2007-7-13 0:31:50 > top of Java-index,Java Essentials,Java Programming...
# 7
> You don't have to convert the String to a char[] to> use the CharBuffer.wrap() method. Its overloaded to> accept a CharSequence, String class implements the> CharSequence interface, To FAMUGambler. Thanks, I didn't know that.
petes1234a at 2007-7-13 0:31:50 > top of Java-index,Java Essentials,Java Programming...