By Gurpreet Singh, Department of Computer Science & Engineering, Amritsar College of Engineering & Technology
Monday, 6 July 2015
Sunday, 5 July 2015
Get Current Date in java
To get current date in java write following code:
DateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
Date date=new Date(); //get current date and time
System.out.println(""+dateFormat.format(date));
Import statements:
import java.text.*;
Friday, 26 June 2015
for each loop in java
What is for each loop?
for each loop in java is the enhanced version of for loop. It is introduced from JDK 5. It is used to iterate all elements of an array or Collection.
Syntax:
for ( data-type variableName : array or collection )
{
//statements
}
Consider the following program:
for each loop in java is the enhanced version of for loop. It is introduced from JDK 5. It is used to iterate all elements of an array or Collection.
Syntax:
for ( data-type variableName : array or collection )
{
//statements
}
Consider the following program:
public class ForEachLoop
{
public static void main(String[] args)
{
int[] array ={ 1, 2, 3, 4, 5 };
System.out.println("Array elements: ");
for(int element: array)
System.out.print(element+" ");
}
}
Write the above program in Notepad and save it as ForEachLoop.java
Compile and run it to get the following output:
Array elements:
1 2 3 4 5
the loop for(int element: array)
fetches the elements of array and assign it to variable element so we can use element to access the elements of array
Consider the following program to access elements of multi-dimensional array
Compile and run it to get the following output:
Array elements:
1 2 3 4 5
the loop for(int element: array)
fetches the elements of array and assign it to variable element so we can use element to access the elements of array
Consider the following program to access elements of multi-dimensional array
public class ForEachLoop
{
public static void main(String[] args)
{
int[][] array ={ {1, 2, 3, 4,},{ 5,6,7,8} }; //array with 2 rows and 4 columns
System.out.println("Array elements: ");
for(int[] row: array)
for(int element: row)
System.out.print(element+" ");
}
}
Write the above program in Notepad and save it as ForEachLoop.java
Compile and run it to get the following output:
Array elements:
1 2 3 4 5 6 7 8
the loop for(int[ ] row: array)
fetches one row of array and assign it to variable row so we can use row to access one row(or one dimensional array) of array
the loop for(int element: row)
fetches the elements of row and assign it to variable element so we can use element to access the elements of row
Compile and run it to get the following output:
Array elements:
1 2 3 4 5 6 7 8
the loop for(int[ ] row: array)
fetches one row of array and assign it to variable row so we can use row to access one row(or one dimensional array) of array
the loop for(int element: row)
fetches the elements of row and assign it to variable element so we can use element to access the elements of row
Command Line Arguments
public class CommandLineArguments
{
public static void main(String[] args)
{
System.out.println("Total number of Command Line Arguments: "+args.length);
System.out.println("Command Line Arguments:");
for(int i=0;i<args.length;i++)
System.out.println(args[i]);
}
}
Type the above program in Notepad and Save it as CommandLineArguments.java
Compile like this:
javac CommandLineArguments.java
Run like this:
java CommandLineArguments Arguments
For example:
java CommandLineArguments Hello World
output:
Total number of Command Line Arguments: 2
Command Line Arguments:
Hello
World
If you run like this:
java CommandLineArguments
output:
Total number of Command Line Arguments: 0
Command Line Arguments:
Note: Each argument in command line arguments is seperated by a space
Compile like this:
javac CommandLineArguments.java
Run like this:
java CommandLineArguments Arguments
For example:
java CommandLineArguments Hello World
output:
Total number of Command Line Arguments: 2
Command Line Arguments:
Hello
World
If you run like this:
java CommandLineArguments
output:
Total number of Command Line Arguments: 0
Command Line Arguments:
Note: Each argument in command line arguments is seperated by a space
Monday, 22 June 2015
Thursday, 18 June 2015
Wednesday, 17 June 2015
Program to reverse each word of a given string
//Program to reverse each word of a given string
import java.io.*; //for IOException, BufferedReader, InputStreamReader
import java.util.*; //for StringBuffer
public class WordReverse {
public static void main(String[] args)throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the string:");
String str=br.readLine();
StringTokenizer st=new StringTokenizer(str);
String temp="";
while(st.hasMoreTokens())
{
temp=temp+new StringBuffer(st.nextToken()).reverse().toString()+" ";
}
System.out.println("Reversed String is:");
System.out.println(temp);
}
}
Output:
Enter the string:
Hello this is GsbProgramming
Reversed String is:
olleH siht si gnimmargorPbsG
Hello this is GsbProgramming
Reversed String is:
olleH siht si gnimmargorPbsG
Sunday, 14 June 2015
Program to calculate how many times a word occurs in a given string in just one line
//Program to calculate how many times a word occurs in a given string in just one line
import java.io.*; //for IOException, BufferedReader, InputStreamReader
class CountWord
{
public static void main(String args[]) throws IOException
{
System.out.println("Enter the string: ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine(); //Gets a string from user
System.out.println("Enter the word to search for: ");
String word=br.readLine(); //Gets a string from user
int count=(s.length()-s.replace(word+"", "").length())/word.length();
System.out.println(word+" occurs in given string for: "+count+" times");
}
}
How to get an alert when anyone logs into your Facebook account from a new device or browser?
Follow these steps:
- Log In into your Facebook account and goto Settings.
- Then Jump to “Security Settings” Tab and You can see a Login “Login Alerts” and Click on “Edit”.
- Check the Get notifications
- If you want to receive emails alerts then check it
- If you want to receive text messages alerts then check it
- Click Save Changes and enter your current password and click Submit
- Your changes will be saved and now will get an alert when anyone logs into your Facebook account from a new device or browser
Subscribe to:
Posts (Atom)

