Build and run OutputVariable Java program using NetBean IDE
Select File from New Project
New Project
Java Categories....Java Application .... projects
Next
,,...
ProjectName: MyOutputVariableProject
class: OutputVariable
Sunday, February 22, 2009
Lesson2-java source code
public class OutputVariable {
public static void main( String[] args ){
// Variable value is int primitive type and it is initialized to 10
int value = 10;
// Variable x is char primitive type and it is initialized to 'A'
char x;
x = 'A';
// Variable grade is a double type
double grade = 11;
// Display the value of variable "value" on the standard output device
System.out.println( value );
// Display the value of variable "x" on the standard output device
System.out.println( "The value of x=" + x );
// Display the value of variable "grade" on the standard output device
System.out.println( "The value of grade =" + grade );
}
}
public static void main( String[] args ){
// Variable value is int primitive type and it is initialized to 10
int value = 10;
// Variable x is char primitive type and it is initialized to 'A'
char x;
x = 'A';
// Variable grade is a double type
double grade = 11;
// Display the value of variable "value" on the standard output device
System.out.println( value );
// Display the value of variable "x" on the standard output device
System.out.println( "The value of x=" + x );
// Display the value of variable "grade" on the standard output device
System.out.println( "The value of grade =" + grade );
}
}
Keyboard input
Getting input from keyboard
presentation
Pdf:
Ojbectives
create an intereactive Java program that gets input from the keyboard
use the BufferedReader class to get input from the keyboard using a console
Use the JOptionPane class to get input from the keyboard
using a graphical user interface
* BufferedReader >>from keyboard >> Console
* JOptionPane >>from keybaord >> GUI
Getting input form the keyboard
Two methods of getting input
*BufferedReader class
*JOptionPane class
Using BufferedReader Class
BufferedReader class
-found inthe java.io package
-Used to get input
Steps to get input
1. add this at thetop of y our code:
import java.io.*;
2. add this statement:
BufferedReader dataIn = new BufferedReader ( new InputStreamReader(System.in));
Steps to get input
3. Declare a temporay String variable to get the input , and
invode the readLin() method to get in put from the keyboard.
You have to type it insed a try-catch block.
try{
String temp = dataIn.readLine();
}catch(IOException e){
System.out.println("Error in getting imput");
}
Sample program
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard{
public static void main(String [] args){
BufferedReader dataIn = new BufferedReader (new
InputStreamReader (System.in));
String name ="";
System.out.print("Please Enter your Name;");
try{
name = dataIn.readLine();
}catch(IOException e){
System.out.println("Error!");
}
System.out.println("Hello" + name +"!");
}
}
Sample Program
The lines,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
indicate that we want to use the classes
BufferedReader,
InputStreamReader,
IOException
>>>
java.io package
.These statements can also be written as,
import java.io.*;
The Java Application Programming interface (API) contains
hundreds of predefined classes that youc an use in yor
programs. These classes are organized into what we call packages
Packages contains classes that have related purpose..
the statement,
public class GetInputFromKeyboard{
//means we delcare a class named GetInputFromKeyboard
//The next statement declares the main method
// public static void min (String [] args){
The statments,
BufferedReader dataIn = new BufferedReader (new InputStreamReder(System.in));
declares a variable named dataIn,
with the class type BufferedReader.
delcares a String type variable name;;
The next statment,
systme.out.print("Please Enter Your name:");
output a String " please Enter your name:" on the screen
assures that the posssible exceptions that could occurs in the statement
name = dataIn.readLine();
will be caught
will cover more about exception handling in the latter part of this course..
name = dataIn.readLine();
the method call, dataln.readLine().
This value will then be saved to our name variable.
which we will use in our final statement to greet the user.
System.out.println("Hello" + name + "!");
using JOptionPane class
another way to get input from the user is by using the
JOptionPane class which is found in the
javax.swing package..
JOpionPane makes it easy to pop up a standard dialog box that prompts users
for a value or informs them of somethong.
Sample Program
import javax.swing.JOptionPane;
public class GetInputFromKeyboard{
public static void main (String [] args){
String name ="";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello" + name + "!";
JOptionPane.showMessageDialog(null,msg);
}
}
Sameple program
The statmement
import javax.swing.JOptionPane;
indicate that we want to import the class JOptionPane from
the javax.swing package.
This can also written as,
import javax.swing.*;
name =JOptionPane.showInputDialog("please enter your name");
create a JOptionPane input dialog, which will display a
dialog with a message, a textfield and an OK button as shown in the figure.
This returns a String which we will save in the name
varibale..
input
please enter your name
{ ]
ok cancel
JOptionPane.showMessageDialog(null,msg);
BufferedReader
JOptionPane..
Discuss
Java has already provide the class the make keyboard input into the program.
for the command line
BufferedReader is using
JOptionPane is for the window program...
presentation
Pdf:
Ojbectives
create an intereactive Java program that gets input from the keyboard
use the BufferedReader class to get input from the keyboard using a console
Use the JOptionPane class to get input from the keyboard
using a graphical user interface
* BufferedReader >>from keyboard >> Console
* JOptionPane >>from keybaord >> GUI
Getting input form the keyboard
Two methods of getting input
*BufferedReader class
*JOptionPane class
Using BufferedReader Class
BufferedReader class
-found inthe java.io package
-Used to get input
Steps to get input
1. add this at thetop of y our code:
import java.io.*;
2. add this statement:
BufferedReader dataIn = new BufferedReader ( new InputStreamReader(System.in));
Steps to get input
3. Declare a temporay String variable to get the input , and
invode the readLin() method to get in put from the keyboard.
You have to type it insed a try-catch block.
try{
String temp = dataIn.readLine();
}catch(IOException e){
System.out.println("Error in getting imput");
}
Sample program
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class GetInputFromKeyboard{
public static void main(String [] args){
BufferedReader dataIn = new BufferedReader (new
InputStreamReader (System.in));
String name ="";
System.out.print("Please Enter your Name;");
try{
name = dataIn.readLine();
}catch(IOException e){
System.out.println("Error!");
}
System.out.println("Hello" + name +"!");
}
}
Sample Program
The lines,
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
indicate that we want to use the classes
BufferedReader,
InputStreamReader,
IOException
>>>
java.io package
.These statements can also be written as,
import java.io.*;
The Java Application Programming interface (API) contains
hundreds of predefined classes that youc an use in yor
programs. These classes are organized into what we call packages
Packages contains classes that have related purpose..
the statement,
public class GetInputFromKeyboard{
//means we delcare a class named GetInputFromKeyboard
//The next statement declares the main method
// public static void min (String [] args){
The statments,
BufferedReader dataIn = new BufferedReader (new InputStreamReder(System.in));
declares a variable named dataIn,
with the class type BufferedReader.
delcares a String type variable name;;
The next statment,
systme.out.print("Please Enter Your name:");
output a String " please Enter your name:" on the screen
assures that the posssible exceptions that could occurs in the statement
name = dataIn.readLine();
will be caught
will cover more about exception handling in the latter part of this course..
name = dataIn.readLine();
the method call, dataln.readLine().
This value will then be saved to our name variable.
which we will use in our final statement to greet the user.
System.out.println("Hello" + name + "!");
using JOptionPane class
another way to get input from the user is by using the
JOptionPane class which is found in the
javax.swing package..
JOpionPane makes it easy to pop up a standard dialog box that prompts users
for a value or informs them of somethong.
Sample Program
import javax.swing.JOptionPane;
public class GetInputFromKeyboard{
public static void main (String [] args){
String name ="";
name = JOptionPane.showInputDialog("Please enter your name");
String msg = "Hello" + name + "!";
JOptionPane.showMessageDialog(null,msg);
}
}
Sameple program
The statmement
import javax.swing.JOptionPane;
indicate that we want to import the class JOptionPane from
the javax.swing package.
This can also written as,
import javax.swing.*;
name =JOptionPane.showInputDialog("please enter your name");
create a JOptionPane input dialog, which will display a
dialog with a message, a textfield and an OK button as shown in the figure.
This returns a String which we will save in the name
varibale..
input
please enter your name
{ ]
ok cancel
JOptionPane.showMessageDialog(null,msg);
BufferedReader
JOptionPane..
Discuss
Java has already provide the class the make keyboard input into the program.
for the command line
BufferedReader is using
JOptionPane is for the window program...
Saturday, February 21, 2009
http://www.javapassion.com/javaintro/
http://www.javapassion.com/javaintro/
http://www.javapassion.com/javaintro/
http://www.javapassion.com/javaintro/
homework 1.
Ex2. write , compile, and run Hello Java program using NetBeans IDE
1. start NetBeand IDE
2. create a NetBeans project
3. Build and run the program
Double click NetBeans IDE icon on the desktop to start the Netbeans IDE.
Observe the NetBeans IDE gets started.
Choose Project
Categories
Java
project
Java Application
Next>
Name and Location.
ProjectName: MyHelloProject
C:>>
create Main Class: Hello
1. start NetBeand IDE
2. create a NetBeans project
3. Build and run the program
Double click NetBeans IDE icon on the desktop to start the Netbeans IDE.
Observe the NetBeans IDE gets started.
Choose Project
Categories
Java
project
Java Application
Next>
Name and Location.
ProjectName: MyHelloProject
C:>>
create Main Class: Hello
java source code-hello world
class Noname1
//Noname1 is name for the class, I can chanage this for any
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
//Noname1 is name for the class, I can chanage this for any
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}
Subscribe to:
Comments (Atom)