The Artima Developer Community
Sponsored Link

Java Answers Forum
Clearing input after readChar()

3 replies on 1 page. Most recent reply: Sep 11, 2003 2:06 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Zed Zeddedy

Posts: 1
Nickname: pahalial
Registered: Sep, 2003

Clearing input after readChar() Posted: Sep 10, 2003 8:12 PM
Reply to this message Reply
Advertisement
Note - I'm new at Java.

I'm using a function where i only want to receive one letter from the user, E or D.
I'm using answer = Stdin.readChar();
However, if the user enters more than one character, it has to ask again (evidently). But when it does the loop and fails to exit since the first letter is neither of the two, it simply assesses the next letter in the string that the user entered.

How would I clear this buffer at the end of the loop so that it waits for input?
I thought of simply using readLine(), but in this case if it was only one letter and not E or D it pauses to get the new line.

Any thoughts appreciated.


Senthoorkumaran Punniamoorthy

Posts: 335
Nickname: senthoor
Registered: Mar, 2002

Re: Clearing input after readChar() Posted: Sep 10, 2003 8:29 PM
Reply to this message Reply
Is readChar() is a method you wrote or its a library function?

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Clearing input after readChar() Posted: Sep 11, 2003 1:42 AM
Reply to this message Reply
	private static void readSystemInput()
	{
		System.out.print("Hello!  What is your name?");
		boolean first = true;
		try
		{
			while (true)
			{
				if (!first)
					System.out.println("Sorry U entered wrong input enter either E or D");
				String name = new BufferedReader(new InputStreamReader(System.in)).readLine();
				if (name!=null && name.length()==1)
				{
					
					if (name.charAt(0)=='E'||name.charAt(0)=='D')
					{
						//process ur stuff
						System.out.println("Name"+name);
						char [] chars = new char[1];
						chars[0] = name.charAt(0);
						System.out.println("Char "+new String(chars));
						break;
					}
				}
				first = false;
			}
		}
			
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: Clearing input after readChar() Posted: Sep 11, 2003 2:06 AM
Reply to this message Reply
I did too many things....
When I was already reading line why to take char of it

we can check in aboove code
if (name.equalsIgnoreCae("E") ||name.equalsIgnoreCae("D"))
//proceed... no need for char also...

Just proceedded with char in mind ..

Where is my coffee..

Flat View: This topic has 3 replies on 1 page
Topic: How do i start a bath file from a html page Previous Topic   Next Topic Topic: can anyone help me with this assignment?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use