Networking problem

Discussion in 'Java' started by JONESEY, Nov 30, 2003.

  1. JONESEY

    JONESEY Guest

    Im trying to get a text file from a server and load it into a 2
    dimensional array. So i came up with the follwoing method. The file is
    a table using "|" to indicate the end of a cell and "¬" to indicate
    the end of a row. I use if statements to find these but the if
    statements done seem to to execute!! Any ideas?

    J

    void getFile(String url) throws IOException {
    int x = 0;
    int y = 0;
    StreamConnection c = null;
    InputStream s = null;
    StringBuffer b = new StringBuffer();
    TextBox t = null;
    String temp;
    try {
    c = (StreamConnection)Connector.open(url);
    s = c.openInputStream();
    int ch;
    while((ch = s.read()) != -1) {
    temp = String.valueOf((char)ch);
    if (temp == "|"){
    fileArray[x][y] = b.toString();

    x++;
    b = null;
    }
    else if (temp == "¬"){
    fileArray[x][y] = b.toString();
    x = 0;
    y++;
    b = null;

    }
    else{
    b.append((char) ch);
    t = new TextBox("Fetch Page", b.toString(), 1024,
    0);
    }
    }
     
    JONESEY, Nov 30, 2003
    #1
    1. Advertising

  2. JONESEY

    Sudsy Guest

    JONESEY
    > Im trying to get a text file from a server and load it into a 2
    > dimensional array. So i came up with the follwoing method. The file is
    > a table using "|" to indicate the end of a cell and "¬" to indicate
    > the end of a row. I use if statements to find these but the if
    > statements done seem to to execute!! Any ideas?
    >
    > J
    >
    > void getFile(String url) throws IOException {
    > int x = 0;
    > int y = 0;
    > StreamConnection c = null;
    > InputStream s = null;
    > StringBuffer b = new StringBuffer();
    > TextBox t = null;
    > String temp;
    > try {
    > c = (StreamConnection)Connector.open(url);
    > s = c.openInputStream();
    > int ch;
    > while((ch = s.read()) != -1) {
    > temp = String.valueOf((char)ch);
    > if (temp == "|"){
    > fileArray[x][y] = b.toString();


    Some interesting code! First you have to decide whether you're
    going to using Strings or chars. Based on your coercion of an
    int into a String containing a single character, it appears
    that you're going the String route. But then you do the oddest
    comparison! If you're checking for String equivalance then you
    should use temp.equals( "|" ). Your code just checks whether the
    strings share the same memory location. (They don't)
    That being said, have you considered using char instead? That
    would make the if statement look like this:
    if( '|' == (char) ch ) {
    A bit less overhead than what you've got currently.
     
    Sudsy, Nov 30, 2003
    #2
    1. Advertising

  3. JONESEY

    JONESEY Guest

    Yeah sorry it is intresting code. I have now got it doing it via the
    char comparison but its not doing anything!! The program just stops!!
    Was there anything e;se that jumped out at you

    On Sun, 30 Nov 2003 13:11:39 -0500, Sudsy <>
    wrote:
    >Some interesting code! First you have to decide whether you're
    >going to using Strings or chars. Based on your coercion of an
    >int into a String containing a single character, it appears
    >that you're going the String route. But then you do the oddest
    >comparison! If you're checking for String equivalance then you
    >should use temp.equals( "|" ). Your code just checks whether the
    >strings share the same memory location. (They don't)
    >That being said, have you considered using char instead? That
    >would make the if statement look like this:
    > if( '|' == (char) ch ) {
    >A bit less overhead than what you've got currently.
     
    JONESEY, Nov 30, 2003
    #3
  4. JONESEY

    Sudsy Guest

    JONESEY
    > Yeah sorry it is intresting code. I have now got it doing it via the
    > char comparison but its not doing anything!! The program just stops!!
    > Was there anything e;se that jumped out at you


    On second visit, it appears that the very first time you encounter
    a character which isn't special (i.e. the NOT or the OR) you create
    a new TextBox. I don't know where the code for setVisible( true )
    is located but it looks like that constructor doesn't belong INSIDE
    the loop. Shouldn't it only be instantiated AFTER the loop, i.e.
    after you've read the entire input?
     
    Sudsy, Nov 30, 2003
    #4
  5. JONESEY

    JONESEY Guest

    the problem lies with the b = null; Is there anyway to delete all teh
    characters from a String buffer?
     
    JONESEY, Nov 30, 2003
    #5
  6. JONESEY

    Sudsy Guest

    JONESEY
    > the problem lies with the b = null; Is there anyway to delete all teh
    > characters from a String buffer?
    >


    b.setLength( 0 );
     
    Sudsy, Nov 30, 2003
    #6
  7. JONESEY

    JONESEY Guest

    A BIG THANKS!!!!

    J

    On Sun, 30 Nov 2003 16:04:03 -0500, Sudsy <>
    wrote:

    >JONESEY
    >> the problem lies with the b = null; Is there anyway to delete all teh
    >> characters from a String buffer?
    >>

    >
    >b.setLength( 0 );
     
    JONESEY, Nov 30, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Grazzini

    Re: Networking with Perl

    Steve Grazzini, Jul 30, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,081
    Steve Grazzini
    Jul 30, 2003
  2. mirek
    Replies:
    3
    Views:
    536
    Chris Jackson
    Dec 17, 2003
  3. nialltimpson

    networking problem

    nialltimpson, Mar 23, 2005, in forum: Java
    Replies:
    8
    Views:
    374
    nialltimpson
    Mar 24, 2005
  4. sravan_reddy001

    Problem in Networking...

    sravan_reddy001, Jan 20, 2008, in forum: Java
    Replies:
    12
    Views:
    574
    Martin Gregorie
    Jan 21, 2008
  5. Replies:
    1
    Views:
    291
Loading...

Share This Page