Networking problem

J

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();

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);
}
}
 
S

Sudsy

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.
 
J

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
 
S

Sudsy

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?
 
J

JONESEY

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

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top