line reader

im creating a program in j2me but i need a line reader, since j2me doesnt support this im trying to create my own method to do this...

but for some reason if i send data into the input such as

writer.write("$$$");

writer.flush();

write.write("#?);

writer.flush();

the output i get is:

$$$

$$$

#?br>

it shud be:

$$$

#?br>

y doesnt the flush empty the buffer is am i not clearing the string properly...below is all the code for the method:

privatestatic String lineRead(Reader reader)

throws IOException

{

StringBuffer stringbuffer =new StringBuffer();

int i1;

if((i1 = reader.read()) == -1)

{

returnnull;

}

char c1;

do

{

c1 = (char)i1;

if(i1 == -1 ||"\r\t\n ".indexOf(c1) == -1)

{

break;

}

i1 = reader.read();

}while(true);

for(; i1 != -1 && c1 !='\n'; c1 = (char)(i1 = reader.read()))

{

stringbuffer.append(c1);

}

return stringbuffer.toString();

}

i call this method using:

StringBuffer inputBuffer =new StringBuffer();

String line;

do

{

if((line = lineRead(ireader)) !=null)

{

inputBuffer.append(line);

inputBuffer.append('\n');

System.out.println(inputBuffer.toString());

...

[2565 byte] By [luqmana] at [2007-11-26 13:54:19]
# 1

Hi,

why don't you open a BufferedReader on the Reader you have ?

BufferedReader br = new BufferedReader(reader) ;

String sLine = null ;

while ( (sLine = br.readLine())!=null) {

// do your stuff with the line here....

}

g_magossa at 2007-7-8 1:32:49 > top of Java-index,Java Essentials,New To Java...
# 2
> Hi,> > why don't you open a BufferedReader on the Reader you> have ?like i said....j2me doesnt providebufferedreaders
luqmana at 2007-7-8 1:32:49 > top of Java-index,Java Essentials,New To Java...
# 3
luqman,There's a couple of open source buffered readers getting around, you just need to Google harder. :-) Cheers. Keith.
corlettka at 2007-7-8 1:32:49 > top of Java-index,Java Essentials,New To Java...
# 4

I think your readLine is working correctly. The problem is in the following part :

StringBuffer inputBuffer = new StringBuffer();

String line;

do

{

if((line = lineRead(ireader)) != null)

{

inputBuffer.append(line);

inputBuffer.append('\n');

System.out.println(inputBuffer.toString());

...

You read the first line, append it to inputBuffer and than you print inputBuffer using System.out.println(), this shows you line 1. After that you read the second line append it to inputBuffer (without emptying it first) and print inputBuffer, which of course shows you again line 1 (you left it in there ...) and then line 2.

Do your System.out.println after (!!!) the loop.

By the way. This is a crude implementation of BufferedReader. It is more 'object oriented' (if there is such a term), if you do it that way :

import java.io.IOException;

import java.io.Reader;

public class MyLineReader extends Reader {

private Reader m_Reader = null ;

private char LF = '\r' ;

private char CR = '\n' ;

public MyLineReader(Reader r) {

m_Reader = r ;

}

public String readLine() throws IOException {

String sReturn = null ;

int i = m_Reader.read() ;

if (i!=-1) {

sReturn = "" ;

StringBuffer sb = new StringBuffer() ;

while (i!=-1 && i!=LF ) {

if (i!=CR)

sb.append((char) i) ;

i = m_Reader.read() ;

}

sReturn = sb.toString() ;

}

return sReturn ;

}

public int read() throws IOException {

return m_Reader.read() ;

}

public boolean markSupported() {

return m_Reader.markSupported() ;

}

public boolean ready() throws IOException{

return m_Reader.ready() ;

}

public void mark(int i) throws IOException{

m_Reader.mark(i) ;

}

public void reset() throws IOException{

m_Reader.reset() ;

}

public long skip (long offset) throws IOException {

return m_Reader.skip(offset) ;

}

public int read(char[] cbuf, int off, int len) throws IOException {

return m_Reader.read(cbuf,off,len);

}

public int read(char[] cbuf) throws IOException {

return m_Reader.read(cbuf);

}

public void close() throws IOException {

m_Reader.close() ;

}

}

g_magossa at 2007-7-8 1:32:49 > top of Java-index,Java Essentials,New To Java...
# 5
thanks g_magoss thats just wot i needed. i decided to use ure approach.
luqmana at 2007-7-8 1:32:49 > top of Java-index,Java Essentials,New To Java...