Posts

Showing posts from 2016

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 Pa...

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           ...

Hello world program in java

class hello{          public static void main(String [] args){                  System.out.println("Hello world !");          } }
Welcome to the java world