One machine can do the work of fifty ordinary men. No machine can do the work of one extraordinary man

Saturday, January 22, 2011

How to get user inputs using keyboard?


01. Write a Java program to convert a given temperature in Celsius to Fahrenheit or to
convert  a  given  temperature  in  Fahrenheit  to  Celsius.  Get  all  the  inputs  as
keyboard  inputs.  Your  program  should  have  two  methods
to  perform  the  operations  mentioned  above.    When
ever you run the program main method should display  an output as given below.
          1 – To convert Celsius to Fahrenheit
          2 - To convert Fahrenheit to Celsius
         Any other number – To exit

        *  When the use enters a value 1 or 2, get the temperature in Celsius or in
             Fahrenheit convert to the other form and display.

        * Then you  must display the menu given above again.

        * You exit the program wh en the user  enter any number other than 1 or 2.

        * You may use loop and switch statements.

              Tc = (5/9)*(Tf-32);
              Tf = (9/5)*Tc+32;

Answer :

import java.util.Scanner;

class Temp{
public static void main(String args[]){
System.out.println("Enter\n1 - To convert Celsius to Fahrenheit\n2 - To convert Fahrenheit   to Celsius\nAny other number - To exit");

Scanner sc1 = new Scanner(;
int option = sc1.nextInt();

case 1 : celsiusToFahrenheit();
case 2 : fahrenheitToCelsius();
default : System.exit(0);
public static void celsiusToFahrenheit(){
System.out.println("Enter celsius value - ");

Scanner sc2 = new Scanner(;
double temp = sc2.nextDouble();

                          System.out.print("Fahrenheit value - "+((9.0/5.0)*temp+32));
public static void fahrenheitToCelsius(){
System.out.println("Enter fahrenheit value - ");

Scanner sc2 = new Scanner(;
double temp = sc2.nextDouble();

                          System.out.print("Celsius value - "+((5.0/9.0)*(temp-32)));

02. Write a Java program to calculate the numb er of digits and number of letters in a
word. Get the word as a  command line argument.
              You may use isDigit() and isLetter() methods in Character class.

Answer :


class Text{
public static void main(String args[]) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(;
System.out.println("Enter the text : ");
String text = br.readLine();

int digits=0, letters=0;
for(int i=0; i<text.length(); i++){
else if(Character.isLetter(text.charAt(i)))
System.out.println("Number of letter - "+digits);
System.out.println("Number of digits - "+letters);


  1. This is cool machan. I didn't know that Scanner thing exactly. First I read ur coding and understood the logic. Then did it on my own. It worked. Thanks macho.

  2. wlcm bro.................... :)))

  3. thnxx machn........ its working....:))