Archive for the ‘Java Source Code’ Category
- In: Core Java Tutorials | Java Source Code | Java Tutorials | NetBeans IDE Source Code
- Comments Off on Passing String in Switch Case in Java
Objectives:
- How to use switch case in Java?
- How to use switch case with string in Java?
- How to pass string literals in switch case in Java?
Source Code:
public class SwitchWithStringExample { public static void main(String[] args) { String str = "two"; switch(str) { case "one": System.out.println("one"); break; case "two": System.out.println("two"); break; case "three": System.out.println("three"); break; default: System.out.println("no match"); } // switch close } // main close } // class close
Steps to run above Java code:
Consider that we have saved code at C drive (c:\>) and the file is saved as SwitchWithStringExample.java
- Compilation : c:\> javac SwitchWithStringExample.java
- Interpretation : c:\> java SwitchWithStringExample
Output:
two
We conduct Java Training and Workshop for Corporates and College Students, if interested, please write to us on kloudsancyber@gmail.com
Aatul Palandurkar
International Trainer, and Author, NetBeans Dream Team Member
https://www.facebook.com/aatulpalandurkar
https://www.instagram.com/aatulpalandurkar/
Using For Each in Java
Posted April 2, 2020
on:- In: Core Java Tutorials | Java Source Code | Java Tutorials
- Comments Off on Using For Each in Java
Objective:
- How to use For Each in Java?
- How to use advance for in Java?
- How to use advance for loop in Java Programming Language?
- Types of For loop in Java
Source Code:
class ForEachExample { public static void main(String args[]) { //declaring an array int a[]={1,2,3,4,5,6}; // Old way System.out.println("Old way"); for(int i=0; i<a.length;i++) { System.out.println(a[i]); } //New way: for-each loop System.out.println("For each"); for(int i:a) { System.out.println(i); } } }
- In: Core Java Tutorials | How to's | Java Source Code | Java Tutorials
- Comments Off on Pattern Programs in Java – Pattern 3
Pattern 3 :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Java Code for above pattern :
import java.util.Scanner; public class Pattern3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); //Printing upper half of the pattern for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Printing lower half of the pattern for (int i = rows-1; i >= 1; i--) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Closing the resources sc.close(); } }
Output :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
- In: Core Java Tutorials | How to's | Java Source Code | Java Tutorials
- Comments Off on Pattern Programs in Java – Pattern 2
Pattern 2 :
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
Java Program for the above pattern:
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(i+" "); } System.out.println(); } //Close the resources sc.close(); } }
Pattern Programs In Java – Pattern 1
Posted May 21, 2018
on:- In: Core Java Tutorials | Java Source Code | Java Tutorials
- Comments Off on Pattern Programs In Java – Pattern 1
Q. Write a Java program to print given pattern.
Pattern 1 :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
Java Program:
import java.util.Scanner; public class MainClass { public static void main(String[] args) { Scanner sc = new Scanner(System.in); //Taking rows value from the user System.out.println("How many rows you want in this pattern?"); int rows = sc.nextInt(); System.out.println("Here is your pattern....!!!"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+" "); } System.out.println(); } //Close the resources sc.close(); } }