Programming Articles - Page 2457 of 3363

Can we have an empty catch block in Java?

raja
Updated on 03-Jul-2020 07:18:31

6K+ Views

Yes, we can have an empty catch block. But this is a bad practice to implement in Java.Generally, the try block has the code which is capable of producing exceptions, if anything wrong in the try block, for instance,  divide by zero, file not found,  etc. It will generate an exception that is caught by the catch block. The catch block catches and handles the exception. If the catch block is empty then we will have no idea what went wrong within our code.Examplepublic class EmptyCatchBlockTest {    public static void main(String[] args) {       try {          int a ... Read More

How many ways to iterate a TreeSet in Java?

raja
Updated on 29-Nov-2023 10:07:15

1K+ Views

A Treeset is a subclass of AbstractSet class and implements NavigableSet Interface. By Default, a Treeset gives an ascending order of output and it will use a Comparable interface for sorting the set elements. Inside a Treeset, we can add the same type of elements otherwise it can generate a ClassCastException because by default TreeSet uses a Comparable interface. Syntax public class TreeSet extends AbstractSet implements NavigableSet, Cloneable, Serializable We can iterate a TreeSet in two ways. Using Iterator We can iterate the elements of a TreeSet using Iterator interface. Example import java.util.*; public class IteratingTreeSetTest { public static void main(String[] args) { ... Read More

How to validate if JTable has an empty cell in Java?

raja
Updated on 12-Feb-2020 08:03:55

2K+ Views

A JTable is a subclass of JComponent class for displaying complex data structures. A JTable can follow the Model View Controller (MVC) design pattern for displaying the data in rows and columns. A JTable will generate TableModelListener, TableColumnModelListener, ListSelectionListener, CellEditorListener and RowSorterListener interfaces.We can validate whether the JTable cell is empty or not by implementing the getValueAt() method of JTable class. If we click on the "Click Here" button, it will generate an action event and display a popup message like "Field is Empty" to the user.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class JTableEmptyValidateTest extends JFrame {    private JPanel panel;    private JTable table;   ... Read More

How to display "No records available" text in a JTable in Java?

raja
Updated on 12-Feb-2020 07:15:04

497 Views

A JTable is a subclass of JComponent class and it can be used to create a table with information displayed in multiple rows and columns. When a value is selected from a JTable, a TableModelEvent is generated, which is handled by implementing a TableModelListener interface.In the below program, we can display "No records available" text if the rows are not available in a JTable.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.table.*; public class NoRecordTableTest extends JFrame {    private JPanel panel;    private JTable table;    private JScrollPane scrollPane;    public NoRecordTableTest() {       panel = new JPanel();       panel.setLayout(new BorderLayout()); ... Read More

How to print the first character of each word in a String in Java?

Vivek Verma
Updated on 05-May-2025 15:10:03

7K+ Views

This article will discuss how to print the first character of each word in a given string. For example, if we have the string "Hello John", the output should be H J. In Java, the String class is used to represent character strings. All string literals in a Java program are implemented as instances of the String class. Strings in Java are immutable, which means that once a string is created, its value cannot be changed. To print the first character of each word in a String, we have the following approaches - Using split() ... Read More

What are the tools to support Data Science other than Python and R?

Pavitra
Updated on 29-Aug-2019 12:21:47

156 Views

In this article, we will learn about the tools to support Data Science other than Python and R?Here we will look at five tools that help in implementing the concept of data science.Apache HadoopJava-based free softwareLarge storage capabilityThe splitting capacity of dataNosqlMore structured orientationBetter performance efficiencyOpen-source software efficiencyHiveDistributed data management systemHighly useful in data miningTorchScientific computing frameworkIt uses Lua programming languageIt can easily implement deep learning algorithmsDomino data labUnified data science toolIncreases iteration speedRemoves deployment frictionConclusionIn this article, we learned about some of the powerful tools available in the field of data science other than Python & R.

Looping Techniques in Python

Pavitra
Updated on 29-Aug-2019 12:09:14

285 Views

In this tutorial, we will learn about looping techniques in python 3.x. Or earlier. There are many ways in which we can implement loops. Here we will be discussing four techniques in looping.Enumeration constructExample Live Demo# enumerate() type for index, value in enumerate(['Tutorial', 'point']):    print(index, value)Output0 Tutorial 1 pointZip constructExample Live Demo# zip() method arr1 = ['Tutorial', 'point'] arr2 = ['python', 'loops'] for i, j in zip(arr1, arr2):    print(i, j)OutputTutorial python point loopsMembership constructExample Live Demo# membership operator for i in ['Tutorial', 'point']:    print(i)OutputTutorial pointInfinitive constructExample# infinite loop while(True):    passStep - based constructExample# range with step incrementer For i ... Read More

What are the uses of generic collections in Java?

Vivek Verma
Updated on 18-Jun-2025 18:30:32

7K+ Views

What are Generic Collections in Java? In Java, the Generic collections were introduced in Java 5. These collections disable the type-casting, and there is no need for explicit type-casting if we use generic collections. The generic collections are type-safe and detect type-related errors at compile time. It allows the datatypes to pass as parameters to classes or interfaces. The Compiler is responsible for checking the compatibility of the types. Syntax Following is the way to create generic collections in Java: class or interface Where type specifies the type of the object, such as: Integer, String, Character, etc.. You can create generic ... Read More

K’th Non-repeating Character in Python using List Comprehension and OrderedDict

Pavitra
Updated on 29-Aug-2019 12:01:21

383 Views

In this article, we will learn about K’th Non-repeating Character in Python using List Comprehension and OrderedDict. To do so we take the help of inbuilt constructs available in Python.Algorithm1. First, we form a dictionary data from the input. 2. Now we count the frequency of each character. 3. Now we extract the list of all keys whose value equals 1. 4. Finally, we return k-1 character.Examplefrom collections import OrderedDict import itertools def kthRepeating(inp, k):    # returns a dictionary data    dict=OrderedDict.fromkeys(inp, 0)       # frequency of each character    for ch in inp:       ... Read More

Iterator Functions in Python

Pavitra
Updated on 29-Aug-2019 11:56:38

397 Views

In this article, we will learn about the four iterator functions available in Python 3.x. Or earlier namely accumulate() , chain(), filter false() , dropwhile() methods.Now let’s look on each of them in detail −Accumulate () & chain() methodAccumulate() method takes two arguments, one being iterable to operate on and another the function/operation to be performed. By default, the second argument performs the addition operation.Chain() method prints all the iterable targets after concatenating all of the iterables.The below example explains the implementation −Example Live Demoimport itertools import operator as op # initializing list 1 li1 = ['t', 'u', 't', 'o', 'r'] ... Read More

Advertisements