How to get the length of the 2D array in Java

Two dimensional array in java

To declare an 2D array in java we use the following syntax

Datatype [][] arrayName=new Datatype[rows][cols]

If you want to declare any primitive 2D array.

int [][] ages=new int[3][4];

double [][] prices=new double[4][4]

Array ages is a two-dimensional int array, it can store 3 rows and each row can contain 4 values (columns) total of 3*4=12 values.

similar price is a two-dimensional double array, it can store 4 rows and 4 columns total (4*4) 20 elements.

Two-dimensional array initialization

above are the two-dimension array declared and initialized.

Length of an array in Java

Java array provides a variable length to find the length of a variable.4

to find the length of one dimension array we use following code

One dimension array length is number of elements in array

Length of the 2D array in Java

In a two-dimensional array, java length is the number of rows in 2D array.

To access each row length we can use the following code

Bootstrap Sizing

Bootstrap has a separate set of utilities for controlling the size of the element.

You can manage the width and height of the element as per the requirement.

Here, we will talk about the Boostrap Sizing utilities and discuss the various Bootstrap classes for the same.

, we will talk about the Bootstrap Sizing utilities and discuss the various Bootstrap classes for the same.

To understand the working better, we can first have a look at the CSS sizing properties.

These properties are used to control the normal height and width as well as maximum height and width.

So let’s have a look at the CSS sizing properties before we dive in to the Bootstrap concepts.

CSS Properties

CSS has following properties to control the sizing of the components:

  • height: For controlling the height of the element
  • max-height: For setting the maximum height of the element
  • max-width: For setting the maximum width of the element
  • min-height: For setting the minimum height of the set the element
  • min-width: For setting the minimum width of the element
  • width: For controlling the width of the element

Check the code below to get a good understanding of these properties:

HTML body contains

Now that, you understand the basic sizing features in CSS. Let us get back to Bootstrap.

Bootstrap Sizing

Bootstrap lets you easily control the elements for its width or its height.

You can set these parameters relative to the parent components. Let’s check out some points that you need to know for Bootstrap Sizing utilities:

  • You can manage the width and height of the components using the $sizes Sass map present in the _variables.scss.
  • For width, we have the w-* classes and height we have the h-* classes. Where the * is replaceable by appropriate numbers
  • By default, Bootstrap supports the sizing for 25%, 50%, 75%, and 100%. Therefore, the * in the above point can be replaced with 25, 50, 75 or 100.

Modify those values as you need to generate different utilities here

Max-Width and Max-Height

Similarly, the max-width and max-height properties of CSS can also be controlled by following bootstrap clases:

  • mw-*: Sets the maximum width with the * value.
  • mh-*: Sets the maximum height with the * value.

The below code will illustrate the use of these classes to control the maximum width and height for the value of 100%

Java program to print vowels in a String

In Java, String is represented as an Object.

String characters are represented in Unicode encoding.

In the English language, there are five vowels a, e, i, o, and u.

In any string, vowels can be in small or in large letters.

Here using the Java program we will see how to find vowels in the given string.

Vowels program in java

Vowel program in java are discussed as follows

We created many programs to check and count vowels and consonants from String, StringBuffer, and from files.

Java program to print vowels in a String is as below

Java program to print vowels in a String

Steps to develop the program

  1. Take a String
  2. Create a loop to iterate each character of the string
  3. Inside loop check characters for vowel, both for upper and lower case
  4. If the character is a vowel then print its position and character

In the above program, we created for loop and it runs up to string length.

charAt() is used to access characters at specified index position.

The above program is simple we can make some improvements to it.

We are comparing both uppercase and lower case, we can convert complete string in lower case and compare it will reduce all uppercase comparison.

How to find vowels in a string in java

Java program to count number of vowels in a string from File

test.txt file is stored in d drive and file content is as below.

The Scanner class is used to read test.txt file.

Vowels program in java using switch case

Q: write a program to determine whether the input character is a vowel or consonant or not an alphabet

program to count the number of vowels and consonants in a given string in java

Character class contains method isAlphabetic.

This method is checking whether a number is an alphabet or not.

If the number is alphabet then match the character using for each loop with vowel characters stored in the array.

If matched then the character is vowel else consonant.

Java program to replace vowels in a string

Read More

  1. Duplicate Number between 1 to n numbers
  2. Sum of two numbers using command line arguments
  3. Program to reverse a string
  4. Prime number program in java using while loop
  5. Print vowels in a String
  6. Square Star Pattern

Linux and Unix Cal command tutorial with examples

Command “cal” stands for calendar.
This command is used to see the calendar of a specific month/ year or both.
The basic syntax of command “cal”.

[month]- is used for specify month
[year] – is used for specify year

cal command to display calendar of current month

type “cal” command on terminal and press enter and output will be calendar of the current month

Output:

To see the calendar of specific month/Year [for example May 2021]

type below command in terminal.

Output:

To see the calendar of the specific year (all 12 months)


To see the calendar of the year 2012. Type “cal” command and attach desired year as shown below

Output

In output calendar of 2012(all 12 months) will display in the terminal.

cal command to  display the days of the year
Fig: Calendar for the year 2021

Display the calander for current year

Q: Display the previous, current and next month calendar of the year 1987 August

cal command to show  previous, current and next month calendar

Q: write a command to display the previous, current and next month calendar of the year 2015 december.

Q: What is the command to display the days of the year 2016

Read More

Linux and Unix rm command tutorial with examples
Linux and Unix mv command tutorial with examples
Linux and Unix tail command tutorial with examples

Reference

cal(1) – Linux man page
cal(1) — Linux manual page

Exception Handling try catch finally blocks in Java

What is try block

A try block is used to surround a set of statements where exceptions may occur.

A try block used with catch and finally block

Also Read

Exception Handling in Java: Hierarchy Example and Types

What is a catch block

A catch block is created followed by a try block.

Whenever an exception is thrown from try block it catches in the appropriate catch block

There can be more than one catch block for a try block.

Syntax of try catch block

Java finally block

Java finally block is also associated with a try block.

Finally block is used to release resources that are used in a try block.

like closing a file. closing database connection

Finally block gets always executed when there is exception or not in try block.

We can use try and finally or try catch and finally.

Syntax of try-catch and finally

1 try and finally

2 try catch and finally

try catch java examples

ArithmeticException Example in Java

Output

Exception occurs at the line “a = 42 / d; “ , then this exception is thrown, program control transfer from  try block to catch block.

Once  catch block has executed program control continue with the next line in a program following entire try/catch statement.

In the line “catch (ArithmeticException e) “  e is a reference of exception “ArithmeticException”.

When we print the value of “e “ it will print  ( java.lang.ArithmeticException:/by zero                                                                    at  Test.main(Test.java:6) )

  • ArithmeticException is an exception class which resides in java.lang package.
  • “/by zero”  is a type of arithmetic exception (divided by zero).
  • “Test” is the name of the class.
  • “main” is a method in which an exception occurs.
  • 6 is line number in which exception occured.

Java try catch multiple exceptions

1 Try and multiple catch blocks

In java programming, when within a try block multiple exceptions occur then for handling those multiple exceptions we use multiple catches with a single try block.

When we pass no command line argument (during run time) than the length of command line argument (args.length) is zero means value of variable “a” is 0.

Than the output is:

When we pass one or more command line argument ( in this program one command line argument is passed “test” during run time) than the length of command line argument (args.length) is one means value of variable “a” is  1.

Than the output is:

2 Java try catch multiple exceptions

Unreachable Code in Exception Handling

In Java programming, in a sequence of catch statement exception subclass must come before its super class.

In a series, if exception super class comes before  its subclass than exception sub class would never be reached.

It is known as unreachable code. In Java programming, unreachable code is an compile-time error.    

Example unreachable code in Exception Handling

If we will try to compile this program we will receive an error message stating that the second catch statement is unreachable because ArithmeticException is a subclass of the Exception class. 

     Then the first catch statement will handle all exception-based errors, including Arithmetic Exception therefore second catch statement will never execute.

 To solve the problem, reverse the order of the catch statement.  

Nested try catch Java statements

In java programming, it is possible to create a try block within another try block. Such try block is known as nested try.

Nested try catch Example

If there is no command line argument present then the output.

If there is no command line argument present then the output.

Read More

User-Defined Exception in Java with Examples

Q : When a statement within a try block causes an exception, the remaining statements in the try block

Ans: Will Not be executed

Q: In a try/catch construct, after the catch statement is executed:

Ans: Statement just after the catch is executed

Q: Can you have multiple catch blocks on a try statement

Ans: Yes, as per the need we can have multiple catch blocks on a try statement

Q: To catch an exception, the code that might throw the exception must be enclosed in a ____________

Ans: try block

Q: If an exception occurs in a try-catch block, the code in the finally clause ________.

Ans: Get Executed. If with try block finally is associated then it always gets executed no matter it throws an exception or not.

Q: A try block may be followed by a finally block, without a catch block (Java try without catch).

Ans: Yes we can use try and finally without a catch block.

Q: Exceptions can be thrown by ________.

Ans: Throw statement is used to throw an Exception

Q: When is the code within a catch block executed?

Ans: If an exception occurs within the code in the try block then matched exception in the catch block gets executed.

Java unreported exception How to Handle it

The unreported exception is due to you have checked exception in your program and you are not handling it.

You can handle it

  1. By using try catch block
  2. By using throws keyword in method

unreported exception

Which of the following Throwables needs to be declared?

  1. Error
  2. RuntimeException
  3. CheckedException 
  4. Exception

Answer is 3

More MCQ

Read More

  1. Exception Handling in Java: Hierarchy Example and Types
  2. Exception Handling in Java with Examples
  3. Throw and Throws Keywords in Java
  4. How To Fix Internal Exception: java.io.ioexception In Minecraft

Throw and Throws Keywords in Java

In java programming, If the exception occurs within the try block it is thrown.

a throw keyword is used to throw an exception

A system-generated exception is automatically thrown by the java run time system but if we want to manually throw an exception, we can do so by using the throw keyword. 

Exception Handling try catch finally blocks in Java

Syntax of throw

           throw    ThrowableInstance;

here, ThrowableInstance must be an object of type Throwable or subclass of Throwable.

Example of throw keyword

In this program new is used to create an instance of NullPointerException and we get two chance to deal with the same error.

In the line  “throw new NullPointerException(“demo”);” 

throw keyword is used to throw  NullPointerException(“demo”). Here we set “demo” is a name of exception.

This NullPointerException(“demo”) catch by catch() method which resides in a demo() method.

 Line “throw e;”  re-throw the exception which is catch by catch() method which resides in a main() method.

Throws Keyword in Java

In a java programming, throws keyword list the type of exception that a method might throw.

throws keyword inform that method throws mentioned exceptions

In the throws clause, we declare all the exceptions that a method can throw.

  Syntax of throws:         

                                               

Here, exception1, exception2 are comma, list of the exception that a method can throw.

Example of throws

Explaination: We throws NullPointerException in the line “throw new NullPointerException(“demo”);” .

Here we set “demo” is a name of thread.

This line “throw new NullPointerException(“demo”);” throw the exception which is handled by catch block associated with try within main().

Difference between throw and throws in Java (throw vs throws)

throw keyword is used to throw an exception from method or from a block

throw is used inside method

throw new NullPointerException("demo");

throws keyword is used to inform that a method throws exception(s)

thorws keyword i s used in method header

public Scanner​(File source) throws FileNotFoundException

What is the purpose of the throw statement?

Throw is used to throw an exception from a method or from block of code

what is the difference between throwing an exception and catching an exception?

Throwing an exception can use to throw an exception using the throw keyword.

this can be implicit or explicit

45/0 <- throw ArithmeticException 

throw ArithmeticException(“Denominator can not be zero”);

To catch an exception one or more catch blocks can be used so that program executes properly.

The ______ keyword is used to manually throw an exception in java

The Throw keyword is used to manually throw an exception.

Read More

  1. Exception Handling in Java: Hierarchy Example and Types
  2. Exception Handling in Java with Examples
  3. Exception Handling try catch finally blocks in Java

Reference

How to Throw Exceptions