• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using and reusing buffered stream reader.

 
Adrian Mears
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created one class to open an input stream then feed this into a buffered reader. The class has two methods. readString() which use readLine() and the second readChar which uses read().

In my main body I use readString() method and it is fine.
Then I use readChar() and it appears to already have input.

Being new to this am I missing sothing basic.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Show us the code.
 
Adrian Mears
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class containing main method :-

public class Startagain
{

private static boolean anotherGo = true;
private static String word = null;

public static void main (String args [])
{
while (anotherGo)
{
message();
FromKeyboard rcls = new FromKeyboard();
word = null;
word = rcls.getWord();
reverseWord();
offerAnotherGo();
}
System.out.println ("Goodbye");
}

static void message()
{
System.out.println();
System.out.println("Type in a word or string");
}

static void reverseWord()
{
int len = word.length();
for (int i = --len; i >= 0; i--)
{
System.out.print(word.charAt(i));
}
System.out.println();
}

static void offerAnotherGo()
{
boolean again = true;
while (again)
{
System.out.println("Would you like another go (y/n)?");

FromKeyboard rcls1 = new FromKeyboard();
char repl = rcls1.getChar();

if (repl == 'y' || repl == 'Y')
{
again = false;
anotherGo = true;
}
else
{
if (repl == 'n' || repl =='N')
{
again = false;
anotherGo = false;
}
else
{
System.out.println("Huh!! lets try that again!");
again = true;
} // end else
} // end else
} // end while
} // end method

Class with buffered reader :-

import java.io.*;

public class FromKeyboard
{
private static InputStreamReader isr = new InputStreamReader(System.in);
private static BufferedReader br = new BufferedReader(isr);

static String getWord()
{
String in = null;

try
{
in = br.readLine();
}
catch (IOException e)
{
System.out.println("Sorry I don't like the string you have typed in.");
System.exit(1);
}

try
{
isr.close();
br.close();
}
catch (Exception e)
{
System.out.println("Problem closing the readers");
System.exit(1);
}

return in;
}

static char getChar()
{

char retChar = ' ';

try
{
retChar = (char) br.read();
}
catch (IOException e)
{
System.out.println("Sorry I don't like the character you have typed in.");
System.out.println(e.toString());
System.exit(1);
}

try
{
isr.close();
br.close();
}
catch (Exception e)
{
System.out.println("Problem closing the readers");
System.exit(1);
}

return retChar;
}
}
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You close the streams in getWord() then try to read from them in getChar().
[ June 16, 2004: Message edited by: Joe Ess ]
 
Adrian Mears
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. Sorry it was something so simple.
 
Adrian Mears
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have taken out the close statments. Now the next iteration when getWord() is called there still seams to be some unwanted input?
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Printing out the exceptions would help you figure it out. You can invoke e.getMessage() to get an error message and e.printStackTrace() to print out where the exception occurred.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!