java.io.InputStream represents a stream of bytes. Often when reading binary files or from a network, data is returned in an input stream format and we need to read that data and convert it to a string format so that it becomes meaningful. This post will list out various methods in which the content of an input stream can be converted to a java.lang.String. All the methods assume that you have a source input stream available. Method 1: Using Scanner class java.io.Scanner class has a constructor which takes a java.io.Inputstream as argument. Once an object of scanner is created, use its hasNext and next methods toRead More →

What is JSON? JSON stands for JavaScript Object Notation and has become the most popular format for transfer and storage of data since due to its light weight nature. Data between client and server can only be transferred in the form of a string. A json object can be easily converted to a string and vice-versa. Also, it is pretty easy to traverse across a json object for retrieving values. A json string or json object stores data in key-value format and it can also represent complex parent-child relationships thus making it a preferred way to store and transmit data. Why String to json objectRead More →

What is a prime number? A prime number is a number that is completely divisible by only 1 and itself. By completely divisible means that when divided by a number, the remainder is 0. 0 is not a prime number. In other words, a prime number is a number that has only 2 factors, 1 and the number itself. Examples of prime numbers are 2, 3, 7, 11, 13, 17, 19, 23, 29, 34 and so on. Find all prime numbers within a limit This post will show you how to create a javascript program that determines all the prime numbers less than a givenRead More →

Cloning an object means creating its exact copy. Cloned object would have the same properties and methods as the source object. There are different ways of creating a clone of an object in javascript. Method 1: Iterating over source object Iterate over the object which needs to be cloned using for–in loop and assign the key value in each iteration to the target object. Example, // source object let car = { color: ‘red’, brand: ‘BMW’, print: function() { console.log(‘Car object’); } }; // declare the target object const clone = {}; // iterate over properties of source object for(let key in car) { //Read More →

If you are generating angular component or module from angular cli using command ng g m [module name] and you get the error trace as below TypeError: core_1.PriorityQueue is not a constructor at new TaskScheduler (E:\angularapp\node_modules\@angular-devkit\schematics\src\engine\task.js:20:23) at SchematicEngine.createContext (E:\angularapp\node_modules\@angular-devkit\schematics\src\engine\engine.js:81:31) at SchematicImpl.call (E:\angularapp\node_modules\@angular-devkit\schematics\src\engine\schematic.js:35:38) at Promise (E:\angularapp\node_modules\@angular\cli\tasks\schematic-run.js:73:23) at new Promise (<anonymous>) at Class.run (E:\angularapp\node_modules\@angular\cli\tasks\schematic-run.js:72:16) at Class.run (E:\angularapp\node_modules\@angular\cli\commands\generate.js:152:33) at resolve (E:\angularapp\node_modules\@angular\cli\ember-cli\lib\models\command.js:261:20) at new Promise (<anonymous>) at Class.validateAndRun (E:\angularapp\node_modules\@angular\cli\ember-cli\lib\models\command.js:240:12) Then you are at the right location. Solution Open the package.json file of your application and search for angular-devkit/core dependency under devDependencies tag. It will look something like this. “devDependencies”: { “@angular-devkit/core”: “0.0.28”, } Error trace above is telling you thatRead More →

Assume you have a parent element with multiple child elements. On some event such as a button click, you want to hide all the children of this element at once using jQuery. Suppose the page structure is as given below. <!– parent element –> <div id=”parent”>      <!– Child elements –>      <div>Child One</div>      <div>Child Two</div>      <div>Child Three</div> </div> <button onclick=”hideChildren();”>Hide</button> Above HTML code contains a div with id parent with three child div elements. It also contains a button which when clicked shall hide all the three child elements. Note that button click calls a hideChildren function whichRead More →

If you are connecting to MySQL version 8 from your java application, there are bright chances that you might have come across the following error java.sql.SQLNonTransientConnectionException: Public Key Retrieval is not allowed and if you are accessing a MySQL database from a web or application server such as Wildfly or Tomcat, then you would have encountered this error java.sql.SQLException: Cannot create PoolableConnectionFactory (Public Key Retrieval is not allowed) Solution Add allowPublicKeyRetrieval connection option of MySQL with a value of true to the JDBC connection string. Thus, your connection string should be modified as jdbc:mysql://<database server ip>:3306/databaseName?allowPublicKeyRetrieval=true Reason Since you are connecting to a database, youRead More →

Reading a text file is a vital part in programming. Mostly when a file is read, its contents are stored in a data structure such as an array, list, map etc. This post will detail out various methods to read a text file line by line and store its contents directly into a list in python. For a deep understanding about reading and writing files in python, refer this tutorial Text file that we will be using as an example in this post is named “codippa.txt” and contains following sample content Hello thereThis is pythonLearning from codippa.com Method 1: Using readlines()Open a file in readRead More →

What is a prime number? A number greater than 1 which is divisible only by 1 and itself is called a Prime number. By being divisible means when a number is divided by another number the remainder is 0. Thus, the only factors of a prime number are 1 and the number itself. Remember that 1 is not considered a prime number. There are different methods to check for a prime number in python. This post shall cover all of those. Method 1: Using for-else block A for-else block consists of a for loop block followed by the else block where else block is onlyRead More →