my dog learned polymorphism*
The moose likes Beginning Java and the fly likes how to read Character input from console Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to read Character input from console" Watch "how to read Character input from console" New topic
Author

how to read Character input from console

Manikanta Sabarish
Greenhorn

Joined: Jul 18, 2005
Posts: 14
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

Joined: Jul 18, 2005
Posts: 14
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 ]
 
Consider Paul's rocket mass heater.
 
subject: how to read Character input from console
 
Similar Threads
Switch Case Problem
interface example
user input
[HOMEWORK] Please need help with java project
Loops