Programming Articles - Page 2465 of 3366

How to get the string representation of numbers using toString() in Java?

Vivek Verma
Updated on 05-May-2025 14:35:24

766 Views

String representation of numbers is nothing but converting numeric values into their corresponding string values using methods like toString(). In Java, this can be done by calling the toString() method on wrapper classes such as Integer, Float, and Double. The toString() method is an important method of Object class and it can be used to return the string or textual representation of an object. The object class's toString() method returns a string as the name of the specified object's class which is followed by ?@' sign and the hashcode of the object (java.lang.String;@36f72f09) String Representation Using toString() Method To get ... Read More

How can we implement auto-complete JComboBox in Java?

raja
Updated on 12-Feb-2020 06:34:12

2K+ Views

A JComboBox is a subclass of JComponent class and it is a combination of a text field and a drop-down list from which the user can choose a value. A JComboBox can generate an ActionListener, ChangeListener, and ItemListener interfaces when the user actions on a combo box.We can implement auto-complete JComboBox when the user types an input value from a keyboard by using customization of a combo box (AutoCompleteComboBox) by extending the JComboBox class.Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.plaf.basic.*; public class AutoCompleteComboBoxTest extends JFrame {    private JComboBox comboBox;    public AutoCompleteComboBoxTest() {       setTitle("AutoCompleteComboBox");     ... Read More

Python eval()

Pradeep Elance
Updated on 23-Aug-2019 12:43:20

712 Views

The eval() method parses the expression passed on to this method and runs the expression within the program. In other words, it interprets a string as code inside a python program.SyntaxThe Syntax for eval is as below −eval(expression, globals=None, locals=None)WhereExpression − It is the python expression passed onto the method.globals − A dictionary of available global methods and variables.locals − A dictionary of available local methods and variables.In the below example we allow the user to cerate an expression and run a python program to evaluate that expression. So it helps in create dynamic code.Example Live Demo# expression to be evaluated ... Read More

How to split a string in Python

Pradeep Elance
Updated on 23-Aug-2019 12:38:50

524 Views

Manytimes we need to split a given string into multiple parts based on some delimiter. Python provides a function named split() which can be used to achieve this. It also provides a way to control the delimiter and number of characters to be considered as delimiter.ExampleIn the below example we a string containing many words and space in between. But there are two space characters between Banana and grape. Accordingly the split happens. When no parameter is supplied each space is taken as a delimiter. Live Demostr = "Apple Banana Grapes Apple"; print(str.split()) print(str.split(' ', 2))OutputRunning the above code gives us ... Read More

Help function in Python

Pradeep Elance
Updated on 23-Aug-2019 12:31:41

251 Views

Many times we need to look into the python documentation for some help on functions, modules etc. Python provides a help function that gives us this needed results.SyntaxHelp(‘term’) Where term is the word on which we want the help.ExampleIn the below example we seek to find help on the word time. The output comes from python documentation and it is quite exhaustive. Live Demoprint(help('time'))OutputRunning the above code gives us the following result −Help on built-in module time: NAME time - This module provides various functions to manipulate time values. DESCRIPTION There are two standard representations of time. One is the ... Read More

Filter in Python

Pradeep Elance
Updated on 23-Aug-2019 12:29:22

644 Views

We sometimes arrive at a situation where we have two lists and we want to check whether each item from the smaller list is present in the bigger list or not. In such case we use the filter() function as discussed below.SyntaxFilter(function_name, sequence name)Here Function_name is the name of the function which has the filter criteria. Sequence name is the sequence which has elements that needs to be filtered. It can be sets, lists, tuples, or other iterators.ExampleIn the below example we take a bigger list of some month names and then filter out those months which does not have ... Read More

factorial() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:26:17

6K+ Views

Finding the factorial of a number is a frequent requirement in data analysis and other mathematical analysis involving python. The factorial is always found for a positive integer by multiplying all the integers starting from 1 till the given number. There can be three approaches to find this as shown below.Using a For LoopWe can use a for loop to iterate through number 1 till the designated number and keep multiplying at each step. In the below program we ask the user to enter the number and convert the input to an integer before using it in the loop. This ... Read More

exec() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:15:41

1K+ Views

Exec function can dynamically execute code of python programs. The code can be passed in as string or object code to this function. The object code is executed as is while the string is first parsed and checked for any syntax error. If no syntax error, then the parsed string is executed as a python statement.Syntax for exec() Functionexec(object, globals, locals)WhereObject − A string or a code object passed onto the method.globals − A dictionary of available global methods and variables.locals − A dictionary of available local methods and variables.Passing StringIn the below example we pass a single line of ... Read More

degrees() and radians() in Python

Pradeep Elance
Updated on 23-Aug-2019 12:08:06

800 Views

The measurements of angles in mathematics are done using these two units of measurement called degree and radian. They are frequently used in math calculations involving angles and need conversion from one value to another. In python we can achieve these conversions using python functions.degrees() FunctionThis function takes radian value as parameter and return the equivalent value in degrees. The return is a float value.Example Live Demoimport math # Printing degree equivalent of radians. print("1 Radians in Degrees : ", math.degrees(1)) print("20 Radians in Degrees : ", math.degrees(20)) print("180 Radians in Degrees : ", math.degrees(180))OutputRunning the above code gives us the ... Read More

Counting the frequencies in a list using dictionary in Python

Pradeep Elance
Updated on 23-Aug-2019 11:53:22

3K+ Views

In this article we develop a program to calculate the frequency of each element present in a list.Using a dictionaryHere we capture the items as the keys of a dictionary and their frequencies as the values.Example Live Demolist = ['a', 'b', 'a', 'c', 'd', 'c', 'c'] frequency = {} for item in list:    if (item in frequency):       frequency[item] += 1    else:       frequency[item] = 1 for key, value in frequency.items():    print("% s -> % d" % (key, value))OutputRunning the above code gives us the following result −a -> 2 b -> 1 c ... Read More

Advertisements