Skip to main content

Posts

Reverse a Number In Java

  You can copy below program directly and execute it in editor : package com.crtr4u.www; public class ReverseNumber { public static void main(String[] args) { int originalNumber =3456789, reverseNumber = 0, remainder; while (originalNumber != 0) { remainder = originalNumber % 10; reverseNumber = reverseNumber * 10 + remainder; originalNumber /= 10; }; System.out.println ("reverse Number: " + reverseNumber); } } Output : reverse Number: 9876543

Create Microservice and Register with Eureka Server Step By Step

 Step 1 : File -> New -> Spring Starter Project -> Add Configuration as below and Next : Step 2 :   Add Dependencies and click Finish : Step 3 : Edit properties file as : server.port=0 spring.application.name=AccountService eureka.client.register-with-eureka=true eureka.client.fetch-registry=true eureka.client.serviceUrl.defaultZone=http://localhost:8001/eureka/ Step 4 : add @EnableDiscoveryClient annotation at main class : package com.crtr4u.www; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class AccounMicroserviceApplication { public static void main(String[] args) { SpringApplication.run(AccounMicroserviceApplication.class, args); } } Step 5 : create a simple restcontroller as below : package com.crtr4u.www; import org.spring...

Start Up Eureka Service Discovery In Spring Boot

Today , we are going to see Spring Boot Eureka Server Configuration  in STS  : Step 1: Create Spring Starter Project in Spring Tool Suite : Step 2: Do Java,Maven Configuration as below : Step 3 : select Dependencies Eureka Discovery Client and Eureka Server Step 4 : click Next - > Finish Step 5 : Once all dependencies downloaded successfully , then add annotation  @EnableEurekaServer on main class as below : Step 6 : Edit Properties file as below : server.port=8001 spring.application.name=discoveryservice eureka.client.registerWithEureka=false eureka.client.fetchRegistry=false eureka.client.serviceUrl.defaultZone= http://localhost:8001/eureka Step 7: Run main class as spring boot app : Step 8 : Once Application started succesfuly check in browser :                  now visit url :  http://localhost:8001/ it will show UI as below  : Finally our Eureka Server started as shown above ,lets understa...

sort list of objects using comparator in java

 sort list of objects using comparator in java one of the most important question for interviews of java programmer is how to sort list of objects in java , lets understand the solution using scenario of Patients , we will create a class Patient with two properties patientId and patientName and then we will provide options to sort list of patients by patientId and PatientName. Step 1 :  create Patient.java class as below : package com.sorting.sv; public class Patient { private int patientId; private String patientName; public Patient() { } public Patient(int patientId, String patientName) { this.patientId = patientId; this.patientName = patientName; } public int getPatientId() { return patientId; } public void setPatientId(int patientId) { this.patientId = patientId; } public String getPatientName() { return patientName; } public void setPatientName(String patientName) { this.patientName = patientName; } @Override public String toString() { retu...

count frequency of characters in string using java

count frequency of characters in string using java  In most of the interviews , we always get this question from interviewer : program to count frequency of characters in string ? program to count number of times character occurred in given String? program to count number of times character repeated in statement ? today we are going to see the solution for this , using java we will print count of frequency of characters in string : import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class CountFrequency { public static void main(String[] args) { Scanner scn = new Scanner(System.in); String input = null; int option=0; try { do { System.out.println("Enter 1 to test new String: "); System.out.print("\nEnter 2 for exit : "); try { option = scn.nextInt(); } catch(Exception e) { System.out.println("Wrong input ,Please Enter 1 or 2 "); e.printStackTrace(); ...

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Today we will see solution for Error In Spring Boot : ***************************  APPLICATION FAILED TO START ***************************  Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.  Reason: Failed to determine a suitable driver class. --->My Application.yml : server: port: 8081 spring: datasource: driver-class-name: oracle.jdbc.driver.OracleDriver url: jdbc:oracle:thin:@localhost:1521:xe username: sys as sysdba password: root Oracle Version is 21 C Oracle Database 21c Express Edition Release 21.0.0.0.0 - Production Version 21.3.0.0.0. check my pom.xml here : <? xml version = " 1.0 " encoding = " UTF-8 " ?> < project xmlns = " http://maven.apache.org/POM/4.0.0 " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation = " http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/m...

Sorting Program Using Stream API and sorted() method in java Stream Interface

Sorting Using Stream API sorted() method in java : copy below code and study , to understand different sorting techniques using java Stream API :  I know , this program is very big , but its enough to practice whole sorting using Stream API in java  ,due to time limitation , i am skipping theory part we will see theory in depth in upcoming articles , till that time use this program and try to understand sorting using stream in java :  Step 1: write Student class as below : package com.crtr4u.www; public class Student { int id; String name; String department; public Student() { super(); } public Student(int id, String name, String department) { super(); this.id = id; this.name = name; this.department = department; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDepartment() { return department;...