Posts

Featured post

pattern related programs in java

Simple triangle star pattern 1) Enter a number : 4     *     * *     * * *     * * * * import java.util.Scanner; public class Pattern1 { public static void main( String [] args) { Scanner scanner = new Scanner ( System . in ); System . out .println("Enter a number :"); int n = scanner.nextInt(); for ( int i = 1; i <= n; i++) { for ( int j = 1; j <= i; j++) { System . out .print("*"); } System . out .println(); } } } 2) Enter a number : 4 * * * * * * * * * *  import java.util.Scanner; public class Pattern2 { public static void main( String [] args) { Scanner scanner = new Scanner ( System . in ); System . out .println("Enter a number :"); int n = scanner.nextInt(); for ( int i = n; i > 0; i--) { for ( int j = 0; j < i; j++) { System . out .print("*"); } System . out .println(); } } } 3) Enter a number :4            *         * *      * * *   *

How to open notepad using java application

You can use java.lang.Runtime class to interact with Runtime Environment. To create instance of Runtime class you can use getRuntime() method like Runtime runtime = Runtime.getRuntime(); getRuntime() is a static factory method used to create object of Runtime class. You can not create object of Runtime class using new operator and constructor(new Runtime();) because constructor present in Runtime class is private. There is one more method present in java.lang.Runtime class called exec() method. This method is used to executes the specified system command in a separate process. You can use this method to launch external applications like notepad, browser or any media player through your java program. now java program to open notepad application. public class OpenNotepadTest { public static void main(String[] args) { Runtime runtime = Runtime.getRuntime(); try { runtime.exec("notepad.exe"); } cat

How to set path Environment variable in java

Image
First download the jdk software compatible with your system configuration. To download jdk  click here . To install double click on .exe file. and click next...next...next...finish. After successful installation open command prompt and check javac is working or not. if not follow below steps right click on MyComputer --->properties then click on Advanced System Settings click on Environment variables click on new a new window will open give variable name as path and variable value is your jdk installation directory path then click ok...ok...ok close previous command prompt and open new command prompt and check path is set or not by typing javac command.

How to compile and execute a java program with package

Image
write a program with package declaration for example: package mypack; public class Example { public static void main(String[] args) { System.out.println("Hello World !!"); } } to compile above program type following command in command prompt :\>javac -d . Example.java to execute above program type following command in command prompt :\>java mypack.Example

Different ways to reverse a string in java program

There are multiple ways to reverse a string in java progarm 1. Using for loop: import java.util.Scanner; public class JavaReverseString{ public static void main( String [] args) { Scanner sc= new Scanner ( System . in ); System . out .println("Enter the string to reverse: "); String str = sc.nextLine(); System . out .println("String after reverse: "); for ( int i = str.length()-1; i >=0; i--) { System . out .print(str.charAt(i)); } } } 2. Using StringBuffer import java.util.Scanner; public class JavaReverseString{ public static void main( String [] args) { Scanner sc = new Scanner ( System . in ); System . out .println("Enter the string to reverse: "); String str = sc.nextLine(); String reverse = new StringBuffer (str).reverse().toString(); System . out .println("String after reverse:" + reverse); } }

Java program to print the prime numbers

java program to find prime numbers from 1 to n import java.util.Scanner; public class PrimeNum { public static void main( String [] args) { Scanner sc=new Scanner ( System .in); System .out.println("Enter your limit : "); int limit=sc.nextInt(); for ( int i = 1; i < limit; i++) { boolean k = true; for ( int j = 2; j <= i/2; j++) { if (i % j == 0) { k = false; break ; } } if (k) { System .out.println(i); } } } } Java program to check a number weather is it prime or not import java.util.Scanner; public class PrimeCheck{ public static void main(String args[]) { Scanner sc = new Scanner ( System .in); System .out.println("Enter the number : "); int n = sc.nextInt(); int flag = 0; for ( int i = 2; i <= n / 2; i++) {

String Related programs in java

Java program to count the number of words in a string? solution 1: Using Split() method import java.util.Scanner; class WordsCount { public static void main( String [] args) { System . out .println("Enter the string"); Scanner sc = new Scanner( System . in ); String s = sc.nextLine(); String [] words = s.trim().split(" "); System . out .println("Number of words in the string = " + words. length ); } } solution 2: Using charAt(-) method import java.util.Scanner; class WordsCount2{ public static void main( String [] args) { System .out.println("Enter the string"); Scanner sc = new Scanner ( System . in ); String s=sc.nextLine(); int count = 1; for ( int i = 0; i < s.length()-1; i++) { if ((s.charAt(i) == ' ') && (s.charAt(i+1) != ' ')) { count++; }

Number Pattern programs in java

Simple number pattern program in java 1) Enter a number : 5 1 12 123 1234 12345 import java.util.Scanner; public class Pattern1 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System .out.println( "Enter a number :" ); int n = scanner.nextInt(); for ( int i = 1; i <= n; i++) { for ( int j = 1; j <= i; j++) { System .out.print(j); } System .out.println(); } } } 2) Enter a number : 5 12345 1234 123 12 1 import java.util.Scanner; public class Pattern2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number :"); int n = scanner.nextInt(); for (int i = n; i > 0; i--) { for (int j = 1; j <= i; j++) { System.out.print(j); } System.out.println(); } } } 3) Enter a number :5     1    1 2   1 2 3  1 2 3 4 1 2 3 4 5  import java.util.Scanner; public class Pattern3 { publ