• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to read Character input from console

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic