Page 1 of 2 12 LastLast
Results 1 to 20 of 22

Thread: line

  1. #1
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Question line

    Dear friends

    My program gets one word from server and it supposes to write this word in 2 lines.
    Would you please guide me, which code arranges the output line? :confused:

    Thx

  2. #2
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by Shima View Post
    Dear friends

    My program gets one word from server and it supposes to write this word in 2 lines.
    Would you please guide me, which code arranges the output line? :confused:

    Thx

    what are you trying to do? to separate the word in 2 lines or writing the same word twice in 2 lines?

  3. #3
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi

    Just write the word in two lines.
    (Write the word in 2 lines in client which word comes from server)

    Thx

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    Can you show what the output would look like? For example if your program received the word: programming
    How do you want it to be written?
    line1: ???
    line2: ???

  5. #5
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi

    programmingprogrammingprogrammingprogrammingprogra mmingprogrammingprogrammingprogrammingprogrammingp rogrammingprogrammingprogrammingprogrammingprogram ming

    programmingprogrammingprogrammingprogrammingprogra mmingprogrammingprogrammingprogrammingprogrammingp rogrammingprogrammingprogrammingprogrammingprogram ming


    just in 2 line write programming (like chargen2)

    thx

  6. #6
    Join Date
    Sep 2010
    Posts
    12
    Rep Power
    0

    Default

    i think you have to copy your code at here

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    What you show as the desired output is the one word repeated n times on each line.
    To get that use a nested loop. On the inner loop use print() for the n times you want the word repeated. after the inner loop use println() to move to the next line. The outer loop for the number of lines

  8. #8
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi
    Thanks

    This code:
    for (int j = 0; j<20; j++)
    System.out.print(sentence);

    Suppose to write my sentence in sequence 20 times? Am I right?
    But it just type two time, in two lines; For example if my server received the word, programming. written:

    Programming
    Programming

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,576
    Rep Power
    23

    Default

    I don't think the code you posted would output the two lines you show.
    Are you sure that is the code that is executing?
    Change it to: System.out.print("s=" + sentence + "<");
    to be sure that it is executing.

  10. #10
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi, Thanks but still has the same problem

    The code of my client is:

    package linesclient;

    import java.net.*;
    import java.io.*;
    /**
    * @author Shima
    */
    public class Main {
    public static void main(String[] args) throws Exception {

    byte[] receivedata = new byte[512];
    byte[] senddata = new byte[512];

    DatagramSocket clientsoket = new DatagramSocket();
    InetAddress ipaddress = InetAddress.getByName("localhost");

    BufferedReader bufferreaderuser = new BufferedReader(new InputStreamReader(System.in));

    String str = bufferreaderuser.readLine();
    senddata = str.getBytes();

    DatagramPacket sendpacket= new DatagramPacket(senddata, senddata.length, ipaddress,9876);
    clientsoket.send(sendpacket);


    DatagramPacket recievepaket = new DatagramPacket(receivedata, receivedata.length);
    clientsoket.receive(recievepaket);

    String sentence= new String(recievepaket.getData());

    for (int j = 0; j<5; j++){
    System.out.print("s="+sentence+"<");
    }


    clientsoket.close();

    }

    }

    ---------------------
    and server code is:

    package lines;

    import java.net.*;
    //import java.io.*;
    /**
    * server
    * @author Shima
    */
    public class Main {
    public static void main(String[] args) throws Exception {


    byte[] recievedata = new byte[512];
    byte[] senddata = new byte[512];

    DatagramSocket serversoket = new DatagramSocket(9876);

    while (true){
    DatagramPacket recievepacket = new DatagramPacket(recievedata,recievedata.length);

    serversoket.receive(recievepacket);

    InetAddress ipaddress= recievepacket.getAddress();
    int port= recievepacket.getPort();

    senddata = recievepacket.getData();

    DatagramPacket sendpacket = new DatagramPacket(senddata,senddata.length, ipaddress, port);

    serversoket.send(sendpacket);

    }
    }

    }

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    And what does that code print?
    (And please use code tags)

  12. #12
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    run:
    hi
    s=hi

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    This code:
    Java Code:
        public static void main (String[] args) {
            String sentence= "blah";
    
            for (int j = 0; j<5; j++){
            System.out.print("s="+sentence+"<");
            }
        }
    produces this:
    s=blah<s=blah<s=blah<s=blah<s=blah<

    So that output is not from that code.
    Try it without reading the data, just assigning (as I do above) a fixed value to the sentence string.

  14. #14
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Yes, you are right, thanks. I tried such as you did. It works, but where is my problem?!!!

  15. #15
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Debug it.
    It could be the "server" is hogging all the processing.

  16. #16
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Would you please explain more detail, because I am almost new in java, thanks.

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    I can't really explain debugging...use a debugger and step through the code.

  18. #18
    Lund01's Avatar
    Lund01 is offline Senior Member
    Join Date
    Sep 2010
    Location
    The Netherlands
    Posts
    149
    Rep Power
    0

    Default

    <Wrong post>
    Last edited by Lund01; 10-08-2010 at 02:27 PM.

  19. #19
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Well, no, what I meant was debugging as in using a debugger.

  20. #20
    Shima is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Hi

    Would you please explain more about debugger?
    Where is the location and how can I use it?

    Thx

Page 1 of 2 12 LastLast

Similar Threads

  1. tracing java application line by line using netbeans
    By chandrasekhar123 in forum NetBeans
    Replies: 1
    Last Post: 08-03-2010, 02:46 PM
  2. Replies: 2
    Last Post: 07-02-2010, 02:20 PM
  3. Replies: 4
    Last Post: 09-10-2009, 10:18 AM
  4. Replies: 9
    Last Post: 04-07-2009, 03:51 AM
  5. Reading in data from file line by line
    By bluekswing in forum New To Java
    Replies: 1
    Last Post: 10-02-2007, 12:19 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •