Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object Oriented Programming

Similar presentations


Presentation on theme: "Object Oriented Programming"— Presentation transcript:

1 Object Oriented Programming
Object Oriented Programming Lecture 04

2 Dialog Boxes A JOptionPane object represents a dialog box for several purposes: 1. Display a message (through the use of the showMessageDialog method) Ask for user's confirmation (using the showConfirmDialog method) Obtain the user's input (using the showInputDialog method) Do the combined three above (using the showOptionDialog method)

3 import javax.swing.JOptionPane; public class Addition { public static void main( String args[ ] ) String firstNumber = JOptionPane.showInputDialog( "Enter first integer" ); String secondNumber = JOptionPane.showInputDialog( "Enter second integer" ); int number1 = Integer.parseInt( firstNumber ); int number2 = Integer.parseInt( secondNumber ); int sum = number1 + number2; // add numbers JOptionPane.showMessageDialog( null, "The sum is " + sum, "Sum of Two Integers", JOptionPane.PLAIN_MESSAGE ); }

4 Scanner class(for console input)
Scanner class(for console input) The Scanner class is a class in java.util, which allows the user to read values of various types. Scanner in = new Scanner(System.in);

5 Methods in scanner class
Methods in scanner class int nextInt() Returns the next token as an int. If the next token is not an integer, InputMismatchException is thrown. String nextLine() Returns the rest of the current line, excluding any line separator at the end. long nextLong() float nextFloat() double nextDouble() String next() Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break. If not token exists, NoSuchElementException is thrown.

6 Example Scanner ob=new Scanner(System.in); System.out.println("Enter your username: "); String name=ob.nextLine(); System.out.println("Enter the value of a"); int a=ob.nextInt();


Download ppt "Object Oriented Programming"

Similar presentations


Ads by Google