Two Dimensional String Array Example in Java

It is a 2D array nested loop example. Two loops require, first for row and second for column. It is a String 2D Array example.

Two Dimensional String Array Example in Java

import java.util.Scanner;

public class NestedStringArray2DExample 
{
	public static void main(String[] args)
	{
		Scanner in=new Scanner(System.in);
		System.out.println("Enter the number of rows");
		int noOfRows=in.nextInt();
		System.out.println("Enter the number of columns");
		int noOfColumns=in.nextInt();
		String [][] names=new String[noOfRows][noOfColumns];
		System.out.println("Enter String for array: ");
		//reading elements from user
		for(int rows=0;rows<names.length;rows++)
		{
			for(int cols=0;cols<names[rows].length;cols++)
			{
				names[rows][cols]=in.next();
			}
		}
		//displaying elements
		System.out.println("2D String array elements are: ");
		for(int rows=0;rows<names.length;rows++)
		{
			for(int cols=0;cols<names[rows].length;cols++)
			{
				System.out.print(names[rows][cols]+"      ");
			}
			System.out.println();
		}
	}
}

Output
Enter the number of rows
3
Enter the number of columns
2
Enter String for array:
aaa123
wwwddsf
qwe567
abcdefg
zzzzzz
wertyuio
2D String array elements are:
aaa123 wwwddsf
qwe567 abcdefg
zzzzzz wertyuio

String Array Example in Java

String Array Example in Java

import java.util.Scanner;

public class StringArrayExample 
{
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		System.out.println("Enter the size of array: ");
		int sizeOfArray=in.nextInt();
		String []names=new String[sizeOfArray];
		System.out.println("Enter "+sizeOfArray+" strings in array ");
		//Reading elements 
		for(int i=0;i<sizeOfArray;i++)
		{
			names[i]=in.next();
		}
		//Displaying elements
		for(int i=0;i<sizeOfArray;i++)
		{
			System.out.println("String at "+i+"th location is: "+names[i]);
		}
		in.close();
	}
}

Output
Enter the size of array:
3
Enter 3 strings in array
raju
manish
akash
String at 0th location is: raju
String at 1th location is: manish
String at 2th location is: akash

Integer Array Example in Java

A sample example which shows how to use integer array in Java.

Integer Array Example in Java

import java.util.Scanner;

public class IntegerArrayExample 
{
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		System.out.println("Enter the size of array: ");
		int sizeOfArray=in.nextInt();
		System.out.println("Enter "+sizeOfArray+" numbers in array ");
		int []numbers=new int[sizeOfArray];
		//Read numbers from users
		for(int i=0;i<numbers.length;i++)
		{
			numbers[i]=in.nextInt();
		}
		//Display numbers on console
		for(int i=0;i<numbers.length;i++)
		{
			System.out.println("number at "+i+"th location is: "+numbers[i]);
		}
		in.close();
	}
}

Output
Enter the size of array:
5
Enter 5 numbers in array
10
20
30
40
50
number at 0th location is: 10
number at 1th location is: 20
number at 2th location is: 30
number at 3th location is: 40
number at 4th location is: 50

switch-case example in Java

A sample program to show switch case in Java.

switch-case example in Java

import java.util.Scanner;

public class SwitchCaseExample 
{
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);
		System.out.println("Enter a day [one-seven]:  ");
		String day=in.next();
		day=day.toLowerCase();
		switch(day)
		{
			case "one":
			System.out.println("Sunday");
			break;
			case "two":
			System.out.println("Monday");
		    break;
			case "three":
			System.out.println("Tuesday");
			break;
			case "four":
			System.out.println("Wednesday");
			break;
			case "five":
			System.out.println("Thursday");
			break;
			case "six":
			System.out.println("Friday");
			break;
			case "seven":
			System.out.println("Saturday");
			break;
			default:
			System.out.println("Not a Correct Day!!! Enter only [one-seven]  ");
			break;
		}
		in.close();
	}
}

Output
Enter a day [one-seven]:
five
Thursday

Reading input from user in Java

I used two ways to taking input from user in Java.

1. Using Scanner class

import java.util.Scanner;

public class ReadInputExample1 
{
	public static void main(String[] args) 
	{
		Scanner in=new Scanner(System.in);  //create object of scanner class
		System.out.print("Enter a string: ");
		String str=in.nextLine();		//Reading string
		System.out.print("Enter a number: ");
		int numberInt=in.nextInt(); //Reading integer
		System.out.print("Enter a float number: ");
		float numberFloat=in.nextFloat(); //Reading float
		//display results
		System.out.println("User input string is: "+str);
		System.out.println("User input integer is: "+numberInt);
		System.out.println("User input float is: "+numberFloat);
	}
}

Output
Enter a string: java sample example
Enter a number: 123
Enter a float number: 123.456
User input string is: java sample example
User input integer is: 123
User input float is: 123.456

2. Using BufferedReader class

import java.io.*;

public class ReadInputExample2 
{
	public static void main(String[] args) throws IOException  
	{
		BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter a string: ");
		String str=in.readLine();		//Reading string
		System.out.print("Enter a number: ");
		int numberInt=Integer.parseInt(in.readLine()); //Reading integer
		System.out.print("Enter a float number: ");
		float numberFloat=Float.parseFloat(in.readLine()); //Reading float
		//display results
		System.out.println("User input string is: "+str);
		System.out.println("User input integer is: "+numberInt);
		System.out.println("User input float is: "+numberFloat);
	}
}

Output
Enter a string: java sample example
Enter a number: 1234
Enter a float number: 123.456
User input string is: java sample example
User input integer is: 1234
User input float is: 123.456

if-else control statement example in Java

It is a control statement. It checks if condition is true then performs particular task else it performs another task. Task perform depends on condition is right or wrong.

if-else control statement example in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//Check given number is positive or negative or zero
public class IfConditionExample 
{
	public static void main(String[] args) 
	{
		int number=10;
		if(number==0)
		{
			System.out.println(number+" is equal to zero");
		}
		else if(number>0)
		{
			System.out.println(number+" is positive number");
		}
		else
		{
			System.out.println(number+" is negative number");
		}
	}
}

Output
10 is positive number

do-while loop example in Java

It is an exit control loop. Condition is checked after first iteration. Means statements of block executed at least once, if condition is failed.

do-while loop example in Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class DoWhileLoopExample 
{
	public static void main(String[] args) 
	{
		int number=0; //initialization
		//do while loop
		do
		{
			System.out.println("number is: "+number);
			number++;		//increment
		}while(number>10);  //condition - it checked after block executed once
		//condition failed because number is not greater than 10
 
	}
}

Output
number is: 0

If you have option to select any one from while or do-while then while is more efficient than do-while loop because in do-while loop programmer does not have control from the right of beginning of execution. After the first loop programmer gets control. But in while loop condition is tested first so control from the right of beginning of execution.

For Loop Example in Java

For Loop Example in Java

1
2
3
4
5
6
7
8
9
10
11
12
public class ForLoopExample 
{
	public static void main(String[] args) 
	{
		//for loop
		//for(initialization;condition;increment/decrement)
		for(int number=10;number>0;number--)
		{
			System.out.println("number is: "+number);
		}
	}
}

Output
number is: 10
number is: 9
number is: 8
number is: 7
number is: 6
number is: 5
number is: 4
number is: 3
number is: 2
number is: 1

While Loop Example in Java

While Loop Example in Java

1
2
3
4
5
6
7
8
9
10
11
12
public class WhileLoopExample 
{
	public static void main(String[] args) 
	{
		int number=0; //number initialization
		while(number<10)  //condition
		{
			System.out.println("number is: "+number);
			number++;   //increment/decrement
		}
	}
}

Output
number is: 0
number is: 1
number is: 2
number is: 3
number is: 4
number is: 5
number is: 6
number is: 7
number is: 8
number is: 9

Java Primitive Data Types Example – Java Variables

There are 8 primitive data types in Java.
1. byte
2. short
3. int
4. long
5. float
6. double
7. boolean
8. char

Java sample example to demonstrate primitive data types

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class PrimitiveDataTypesExample 
{
 
	public static void main(String[] args) 
	{
		//declaring primitive data types: 8 types
		int intNumber=1000000000;
		short shortNumber=32767;
		long longNumber=1000000000;
		float floatNumber=100.03244f;
		double doubleNumber=100000.33432;
		char oneCharacter='A';
		boolean flag=true;
		byte byteNumber=127;
 
		//displaying primitive data types results
		System.out.println("integer number is: "+intNumber);
		System.out.println("short number is: "+shortNumber);
		System.out.println("long number is: "+longNumber);
		System.out.println("float number is: "+floatNumber);
		System.out.println("double number is: "+doubleNumber);
		System.out.println("charcter is: "+oneCharacter);
		System.out.println("boolean flag is: "+flag);
		System.out.println("byte is: "+byteNumber);
 
	}
 
}

Output
integer number is: 1000000000
short number is: 32767
long number is: 1000000000
float number is: 100.03244
double number is: 100000.33432
charcter is: A
boolean flag is: true
byte is: 127