Template by:
Free Blog Templates

Monday, August 3, 2009

Getting Input - Java



A- Getting Input from the Console - Scanner

1. Write a coding below and see the output:

(Text book: page 79- Listing 2.9)

import java.util.Scanner;

public class TestScanner {

public static void main(String args[]) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter an integer:");

int intValue=scanner.nextInt();

System.out.println("You entered the integer:"+ intValue);

System.out.print("Enter a double value:");

double doubleValue=scanner.nextDouble();

System.out.println("You entered the double value:"+ doubleValue);

System.out.print("Enter a string without space:");

String string=scanner.next();

System.out.println("You entered the string:" +string);

}

}

Output:


B- Displaying Text in a Message Dialog Box

  1. Write the code as below:

/* This application program display Welcome to Java! In a message dialog box.*/

import javax.swing.JOptionPane;

public class WelcomeInMessageDialogBox {

public static void main(String [] args) {

//Display Welcome to Java! In a message dialog box

JOptionPane.showMessageDialog (null, “Welcome to Java!”, “Display Message”, JOptionPane.INFORMATION_MESSAGE);

}

}

Output



C- Getting Input from the Console - MyInput


public class task1a

{

public static void main (String [] args)

{

System.out.print("Enter measure in inch:");

double inche = MyInput.readDouble();

double cm = 2.54 *inche;

double feet = cm * 12;

System.out.println("Converting from " + inche + " Inch to feet is " + feet);

}

}


Output


0 comments:

Post a Comment