Java Basic Problems

Java:How to insert a word in the middle and at the last position of a String.

Here is the solution to insert a word in the middle and at the last position of a string.

class StringManipulation
{
 public static void main(String args[])
{
StringBuffer str= new StringBuffer(“Object Language”);
System.out.println(“\n”);
System.out.println(“Original String:”+str);
System.out.println(“\n”);
System.out.println(“Length of String:”+str.length());

 for(int i=0;i<str.length();i++)
{
int p=i+1;
System.out.println(“Character at position:”+p+”:is:”+str.charAt(i));

}
String aString=new String(str.toString());
int pos=aString.indexOf(“Language”);
System.out.println(“\n”);
 
 str.insert(pos,”Oriented”);

System.out.println(“Modified string:”+str);
 str.setCharAt(6,’-‘);
System.out.println(“\n”);
System.out.println(“String now:”+str);
System.out.println(“\n”);
str.append(“improves security”);
System.out.println(“Appended string:”+str);
}
}

OUTPUT:

 insert word in the middle of string

Java Basic Problems

Java: How to use image as background icon on JButton

Here I have shared a way to use image as background icon on JButton.

 

Code Example:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 

public class simpletest extends JFrame
{

String conversion;

  public simpletest()
  {
  setSize(300,200);
  setTitle(“Button Icon”);
  setDefaultCloseOperation(EXIT_ON_CLOSE);
  
  ImageIcon iconA = new ImageIcon(“image002.jpg”);
  JPanel panel=new JPanel();
  getContentPane().add(panel);
  panel.setLayout(null);
  
  JButton beep;
  beep= new JButton(“Click”,iconA);
  beep.setRolloverIcon(iconA);
  beep.setBounds(100,60,120,50);
  
  panel.add(beep);
  
  }
          public static void main(String args[])
          {
          simpletest x=new simpletest();
          x.setVisible(true);
          }

}

OUTPUT:

button icon java

Java Basic Problems

Java: How to find out the position of a character in a String using StringBuffer.

Here, I want to share you a solution to find out the position of  characters  in a String using StringBuffer  in  java.

 

 

Code Example:

 

 

class StringManipulation

{

 public static void main(String args[])

{

StringBuffer str= new StringBuffer(“Software Engineering”);

System.out.println(“\n”);

System.out.println(“Original String:”+str);

System.out.println(“\n”);

System.out.println(“length of String:”+str.length());

System.out.println(“\n”);

 for(int i=0;i<str.length();i++)

{

int p=i+1;

System.out.println(“Character at position:”+p+”:is:”+str.charAt(i));

 

}

System.out.println(“\n”);

}

}

OUTPUT:

Character Position in a string java

Java Basic Problems

Java: How to know it is AM or PM using Time / DateFormat in java.

If you are trying to separate the AM or PM from the Time or DateFormat using java, then the following solution will be helpful for you.

Code Example:

import java.text.*;

import java.util.*;

import java.util.StringTokenizer;

class DateFormatDemo {

public static void main(String args[]) {

Date date = new Date();

DateFormat df;

df = DateFormat.getDateInstance(DateFormat.SHORT);

System.out.println(“\n”);

System.out.println(“Short form: ” + df.format(date));

df = DateFormat.getDateInstance(DateFormat.LONG);

System.out.println(“Long form: ” + df.format(date));

System.out.println();

df = DateFormat.getTimeInstance(DateFormat.SHORT);

System.out.println(“Short form: ” + df.format(date));

df = DateFormat.getTimeInstance(DateFormat.LONG);

System.out.println(“Long form: ” + df.format(date));

String daate=df.format(date).toString();

System.out.println(“\n”);

StringTokenizer st_time=new StringTokenizer(daate,” “);

                         String timeStr[] = new String[7];

                         int i=0;

                         while ( st_time.hasMoreElements()) {

                         timeStr[i++] = st_time.nextToken();   

                         }

System.out.println(“It is AM or PM ?:”+timeStr[1]);

}

}

OUTPUT:

AM or PM java

Java Basic Problems

Java Date and Time: How to show Date and Time using DateFormat in java.

Here is an example to illustrate about how to show Date and Time using DateFormat function in java.

Code Example:

import java.text.*;

import java.util.*;

class DateFormatDemo {

public static void main(String args[]) {

Date date = new Date();

DateFormat df;

df = DateFormat.getDateInstance(DateFormat.SHORT);

System.out.println(“\n”);

System.out.println(“Short form: ” + df.format(date));

df = DateFormat.getDateInstance(DateFormat.LONG);

System.out.println(“Long form: ” + df.format(date));

System.out.println();

df = DateFormat.getTimeInstance(DateFormat.SHORT);

System.out.println(“Short form: ” + df.format(date));

df = DateFormat.getTimeInstance(DateFormat.LONG);

System.out.println(“Long form: ” + df.format(date));

}

}

OutPut:

Date and Time java

Java Basic Problems

Java: Multiplication and Division of Float variables by defining and using static methods

 

In my last post I have shared about How to sort string in alphabetical order in array. 

Today I am sharing a new idea.

 

Static methods can be called without using the objects. They are also available for use by other classes.

In this example, I have used two float variables and passed them to another class for multiplication and division operation.

Here is the Code example:
class MathOperation

{

static float mul(float x, float y)

{

return x*y;

}

static float divide(float x, float y)

{

return x/y;

}

}

class MathApplication

{

public  static void main(String args[])

{

System.out.println(“\n”);

float a = MathOperation.mul(4.0f,5.0f);

System.out.println(“Result After Multiplication of Float Values=”+a);

float b = MathOperation.divide(a,2.0f);

System.out.println(“\n”);

System.out.println(“Result After Division of Float Values=”+b);

System.out.println(“\n”);

}

}

 OUTPUT:

multiplication and division float values java

Java Basic Problems

Java: How to sort string in alphabetical order.

In my last post I have tried to share about How to convert integer variable to object type in java.Today I will share with you about how to sort string in an array alphabetically.

Here compareTo() function is used to sort string in an alphabetical order in java.

According to compareTo() function  a string is less than another if it comes before the other in   dictionary order and a string is greater than another if it comes after the other in dictionary  

order.

If you want to ignore case differences when comparing two strings, use compareToIgnoreCase( ),

 String sorting in alphabetical order in java

Code Example:

class StringOrder

{

 static String name[]={“Chittagong”,”Dhaka”,”Rangpur”,”Sonargaon”,”Bangladesh”};

public static void main(String args[])

{

int size= name.length;

String temp=null;

System.out.println(“\n”);

for(int i=0;i<size;i++)

{

for(int j=i+1;j<size;j++)

{

if(name[j].compareTo(name[i])<0)

{

//System.out.println(“name[j]:”+name[j]+”compareTo(name[i]):< 0:”+name[i]);

temp=name[i];

name[i]=name[j];

name[j]=temp;

}}}

for(int i=0;i<size;i++)

{

System.out.println(name[i]);

}}}

Life Thoughts & Personal Notes

Lord Krishna: I am dedicating my 50th post to Lord Sree Krishna

To whom….I love most from my heart.

Krishna

Whenever I go to the temples….I don’t know why, my heart fills with eternal peace and pleasure.

In general view, Most of the time I  don’t like  to attend any pooja, any religious festivals, but I love to visit temples and read books on their divine advices….

What I have found, if you read their divine advices, any holy book (either it is Bible, Geeta, whatever it is )  you will discover a divine change in your mind, your depression will be vanished, your negative thinkings will be gone and your mind….will feel  a great great  pleasure.

Java Basic Problems

Wrapper class: Convert integer variable to object type in java.

In my last post I have shared about How to share and call a class from another class.Today I will share with you about type conversion process in java. For example: int to object conversion, float to object conversion etc.

By using the wrapper class primitive data types for example: int, float,long, char can be converted into object types.

 Here is the example

Integer AgeVal= new Integer(i);

Float  FltVal= new Float(f);

Double DblVal= new Double(d);

Long LngVal= new Long(l);

Here i,f,d,l  are primitive data types indicating integer, float, double and long data types.

Java Basic Problems

Java Class: How to call and use a class from another class?

In my last post I have discussed about Float Type variable and how to add two float type variable.

Here I will share you about how to use and call a class from another class in Java.

 use a class from another class java

Here is the example:

class Room

{

float length;

float breadth;

void getdata(float  a, float b)

{

length=a;

breadth=b;

}

}

class RoomArea

{

public static void main(String args[])

{

float area;

Room room1=new Room();

room1.getdata(14,10);

area=room1.length*room1.breadth;

System.out.println(“Area:”+area);

}

}