Monday, April 19, 2010

Getting Keyboard Inputs II

0 comments
In previous method for getting keyboard inputs we used buffered readers and input stream readers. There is a special class called Scanner available in java.util package which can be used to get keyboard inputs. Using a Scanner object we can read converted values directly into our program which means we haven't to convert read values explicitly. Scanner class be used to perform many other tasks. Lets see a simple Java program to understand its usage.

//© learnjavaapi.blogspot.com  
import java.util.Scanner;
class ScannerDemo{
 public static void main(String args[]){
  int num1;
  float num2,tot;
  String name;
  Scanner scn=new Scanner(System.in);
  System.out.print("Enter your name \t: " );
  name=scn.nextLine();
  System.out.print("Enter a Float value  \t: " );
  num2=scn.nextFloat();
  System.out.print("Enter an Integer value \t: " );
  num1=scn.nextInt();
  tot=num1+num2;
  System.out.println("Hi "+name+" total is \t: "+ tot );
 }
}

Output :

In above example I have used the created Scanner object to read three different data. String,int and float. See the java documentation for more details about Scanner class.
Class : Scanner
Since : Java 1.5
Scanner

Sunday, April 18, 2010

Getting Keyboard Inputs I

0 comments
In order to get inputs from the keyboard we can use standard input stream (System.in ) available in the java.lang package. Generally standard input stream contains byte stream coming from the keyboard. Of cause we can change the standard input. In this example I have used an  InputStreamReader object to read the input stream. Lets see a simple java program written to demonstrate how to get keyboard inputs in java.

//© learnjavaapi.blogspot.com  
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
class GetInput{
 public static void main(String args[]) throws IOException{
  InputStreamReader in=new InputStreamReader(System.in);
  BufferedReader br=new BufferedReader(in);
  System.out.print("Please Enter your name : ");
  String name=br.readLine();
  System.out.println("Hi "+ name +" how are you ? ");
  in.close();
  br.close();
 }
}

Output :

Let me explain important parts of this program.
In the first three lines I have imported essential classes. By using throws keyword I have thrown IOException from my program. So all input/output exceptions are ignored in our program. By creating InputStreamReader object called in our program is ready to get keyboard inputs. For better input handling BufferedReader object br is used. This joining is called wrapping InputStreamReader object within BufferedReader object. InputStreamReader reads bytes from the input stream and decodes them into characters.
JavaDoc:

Saturday, April 17, 2010

Compute Elapsed Time in Nanoseconds

0 comments
To compute some execution time we can use nanoTime() method in the System class. There are several other ways to calculate some execution time. But most accurate way is using nanoTime() method. nanoTime() method returns the current value which is a long value of the most precise system timer in Java. But this value is an approximate value.
//© learnjavaapi.blogspot.com
class ExeTime{
 public static void main(String srgs[]){
  long before=System.nanoTime();
  long sum=0;
  //some long execution
  for(int i=0;i<10000;i++){
   sum+=i;
  }
  long now=System.nanoTime();
  System.out.println("Time difference : " + (now-before) + " nS");
 }
}
Since : Java 1.5
Output :

References

Java Wrapper Classes :: Converting String Values Into Double Values

0 comments
In previous example we discussed how to convert string values into integer values. Double values and float values are used in floating-point calculations. We can use following methods available in relevant classes.

Syntax : Converting String values into Double values
double d1=Double.parseDouble("89223.4234312454");

Syntax : Converting String values into Float values
float f1=Float.parseFloat("2.32323");

Note : Remember  that the Float is not a keyword. It is a class name.

Java Wrapper Classes :: Converting String Values Into Integer Values

0 comments
What is Java API ?
Java API is a short term for Java Application Programming Interface. Java API contains thousands of classes which are very useful for java programmers. Programmers can use these already provided java classes to enhance their productivity. Java documentation is available in following site.
http://java.sun.com/javase/6/docs/

This is the first example of Learn Java API blog. I assume the readers of this blog have basic knowledge of java programming.
Integer wrapper class can be used to manipulate integer variables. We use parseInt() method in Integer class for this example.

//© learnjavaapi.blogspot.com
class ConvertInt{
 public static void main(String srgs[]){
  String n1="12";
  String n2="45";
  int num1,num2;
  //String concatenation
  System.out.println(n1+n2);
  //Integer conversion
  try{
   num1=Integer.parseInt(n1);
   num2=Integer.parseInt(n2);
   System.out.println(num1+num2);
  }catch(NumberFormatException e){
   System.out.println("Please give a valid number!");
  }
 }
}

Output :

Explanation :
n1 and n2 contain string values of given number. Statement in line 08 performs a string concatenation because of n1 and n2 are string values. Hence you can see the output as 1245 twelve and forty five .
Statement in line 13 performs numeric add operation. hence the 2nd output is 57 which is equal to 12+45 . A NumberFormatException  will be thrown if an error occur while converting numbers.

Stats

ලාංකීය සිතුවිලි

lankeeya sithuwili

Blog Catalogs

 

Let's Learn Java API. Copyright 2010 All Rights Reserved