• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to read Character input from console

 
Manikanta Sabarish
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Being a beginner in java,I am trying to read character input from the console and want to implement switch-case based upon the character input.

I had tried searching this forum and couldn't exactly find a solution.

char c= (char) System.in.read() ,tried this,but it throws an IOexception.
I will greatly appreciate if someboody could guide me for the possible solution.

Thanks

Code:
Want to read character input like +,-,*.. from the console.

//char c = (char)System.in.read();

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c = (char)br.read();


switch(c){

case '+': add(num1,num2);
break;
case '*': multiply(num1,num2);
break;
case '-': subtract(num1,num2);
break;
case '/': divide(num1,num2);
break;
 
Manikanta Sabarish
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that.

It did worked using both ways to read a character input.
char c = (char)System.in.read();
or
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char c = (char)br.read();

you should throw IOException while using both ways.

I request the Moderator or administrator to please delete this post,unless u feel this is anything informative.

Thank you.
[ September 07, 2005: Message edited by: Manikanta Sabarish ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic