Posts

Showing posts with the label java

Odd numbers in given range of values in java with example

Read this article to know how to find the ODD number in given range of values. The below example, we have taken two Integer values from where to where we need to find the ODD numbers. In general, will identify the EVEN/ODD numbers by modulo(%) operator with 2. If the modulo returned value will be equal to 0 the given number is an EVEN number otherwise ODD number. if (i % 2 == 1 ) The above condition will be true when i value is an ODD number only. First will write the small program to find the number is EVEN/ODD in the given range of the values and the total count of EVEN and ODD numbers. public class EvenOrAOdd { public static void main ( String [] args ) { int l = 2 ,r = 7 ,evenCount = 0 ,oddCount = 0 ; for ( int i = l;i <= r;i ++ ) { if (i % 2 == 1 ) { System . out . println(i + " is ODD number" ); oddCount ++ ; } else { System . o

Duplicate occurrences of characters in String without Collections

I have searched multiple websites to find the answer how to identify the duplicate characters in a given string " AMBERROADSOFTWARE " without using Collections API. But, still no luck. So, finally me and my team member Ramanjulu has found the way to solve this problem. This is also one of the question which is asking multiple companies interviews. package com . javatbrains . practice ; public class FindDuplicatesFromString { public static void main ( String [] args ) { findUniqueChars (); findDuplicateChar (); findDuplicateWords (); } private static void findUniqueChars () { String str = "NALLAMACHU" ; int count = 0 ; String finalString = "" ; if ( str != null ) { for ( int i = 0 ; i < str . length (); i ++) { char initialChar = str . charAt ( i ); int length = str . length (); str = str . replaceAll ( str . charAt ( i ) + "" , "" ); count =

JSP interview questions and answers

This page contains the most frequently asking JSP interview questions and answers.This will helps for freshers as well as experienced. 1. What are the advantages of JSP over Servlet?                               OR By using Servlet we can do UI implementation, then why to use JSP in any of the applications? A: JSP is a server side technology to make content generation a simple appear.  The advantages of JSP is that they are document-centric. Servlet on the other hand, look and act like programs. A Java Server Page(JSP) can contain Java program fragments that instantiate and execute java classes, but these occurs inside an HTML template file and are primarily used to generate dynamic content. The power of JSP is that it is server-based and provides a framework for web application development. 2. What is the life cycle of JSP?                        OR Can explain about JSP life cycle? A: When a request is mapped to a JSP page for the first time, It translates the JS

Java Networking Connection Refused: Connect

Java Networking “Connection Refused: Connect” problem will get in normal production environments. Here, we are mentioning our best to problem with answer and steps to solve the problem. In the below mentioned source code what it is running in the client and server instances. Server: 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 29 30 import java.io.* ; import java.net.* ; public class Test { public static void main ( String [] args ) { final int PORT_NUMBER = 44574 ; while ( true ) { try { //Listen on port ServerSocket serverSock = new ServerSocket ( PORT_NUMBER ); System . out . println ( "Listening..." ); //Get connection Socket clientSock = serverSock . accept (); System . out . println ( "Connected client" ); //Get input BufferedReader br = new BufferedReader ( new InputStreamReader ( clientSock

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException error we got in Hibernate 4.3 to work with my MySQL database. We are using hibernate code generation tool in Eclipse and we are able to connect to the database using the Hibernate configuration file. When we try to run code in my Main class application is connecting to the database, but transaction is failing and we got the following error: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 Exception in thread "main" java . lang . NoClassDefFoundError : javax / transaction / SystemException at java . lang . Class . forName0 ( Native Method ) at java . lang . Class . forName ( Unknown Source ) at org . jboss . logging . Logger . getMessageLogger ( Logger . java : 2248 ) at org . jboss . logging . Logger . getMessageLogger ( Logger . java : 2214 ) at org . hibernate . cfg . Configuration .< clinit >( Configuration . java : 184 ) at be . comp . permanenti

Types of hibernate bean states

As of we know hibernate follows the ORM features, Java bean class maps with database table. While mapping java bean class with database table it traverse through different types of states. That are Transient, Persistent and Detached states.   Different states of POJO class object: 1. Transient state: Object is created by programmer with data. But is doesn't represent any table row. This object does not contains any primary key value. The object which is created for POJO class and which is not under control of hibernate application resides in transient state. 2. Persistent state: The object that represents table row with primary key and managed under control of hibernate software is called as persistent state. This object will be in synchronization with table row.  H ibernate application developer's uses this kind of object in persistent logic development. 3. Detached state: when session is closed persistent context will be destroyed and all the obje

How to sort list of objects in java with examples

We could achieve list of objects sorting in java by using sort() method which is available under Collections class as well as by using Comparable and Comparator  interfaces. Comparable and Comparator are two interfaces in java used to sort list of objects. These interfaces are available under java.util package along with Collections  class and other classes and interfaces. Comparable interface contains the method of       public int compareTo(Object obj) Comparator interface contains the method of       public int compare(Object obj1, Object obj2) The compare() and compareTo() methods will return negative integer(-1), or zero(0) or positive integer(1). When the first obj1 is less than obj2 method will return negative integer. When obj1 is equals to obj2 will return zero and when obj1 is grater than obj2 it will return positive integer. If it's objects are stored in any Collection classes like ArrayList, HashSet in Array and that time we need to use compare(