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);

}

}

Computer Peripherals

List of Input Devices, Output Devices and Both Input Output devices related to computer.

Here I am going to share you about list of  basic Input Devices, Output devices and  Both inputoutput devices  related to computer.

💻 Input Devices

Input devices are hardware components that help users send data or instructions to the computer for processing.

  1. Common examples of Input Devices:
  2. Graphics Tablet
  3. Camera
  4. Video Capture Hardware
  5. Trackball
  6. Barcode Reader
  7. Digital Camera
  8. Gamepad
  9. Joystick
  10. Keyboard
  11. Microphone
  12. MIDI Keyboard
  13. Mouse (Pointing Device)
  14. Scanner
  15. Webcam
  16. Touchpad
  17. Pen Input
  18. Electronic Whiteboard
  19. OMR (Optical Mark Reader)
  20. OCR (Optical Character Reader)
  21. Punch Card Reader
  22. MICR (Magnetic Ink Character Reader)
  23. Magnetic Tape Drive

🖥️ Output Devices

Output devices are used to display, project, or reproduce processed information from the computer so users can view or hear it.

Common examples of Output Devices:

  1. Monitor (LED, LCD, CRT, etc.)
  2. Printers (Inkjet, Laser, Dot Matrix, etc.)
  3. Plotters
  4. Projector
  5. LCD Projection Panels
  6. Computer Output Microfilm (COM)
  7. Speakers
  8. Headphones
  9. Visual Display Unit (VDU)
  10. Film Recorder
  11. Microfiche

🔁 Both Input and Output Devices

Some devices perform both input and output functions — they can send data to the computer as well as receive information from it.

Examples include:

  1. Modems
  2. Network Cards
  3. Touch Screen
  4. Headsets (Speakers act as output devices and microphones act as input devices)
  5. Facsimile (FAX) (Scans documents for input and prints them for output)
  6. Audio / Sound Cards (Send and receive audio signals)
Java Basic Problems

Java: How to Take input of Float type variables and then add it??

In my last post I have shared about  How to add Integer numbers in Java.

Here I have shown how to add and work with float type variables in java.

In the given example I have taken two float type variable as input and then add it.

 float variable java

Code Example

import java.io.*;

class FloatDemo

{

public static void main(String args[])

{

float floatNumber=0.0f;

float floatNumber1=0.0f;

float sumOf=0.0f;

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

try{

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

System.out.println(“************************************”);

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

System.out.println(“Enter 1st Floating point input:”);

floatNumber=Float.valueOf(br.readLine()).floatValue();

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

System.out.println(“Enter 2nd Floating point input:”);

floatNumber1=Float.valueOf(br.readLine()).floatValue();

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

sumOf=floatNumber+floatNumber1;

System.out.println(“After Addition of the 2 float  number =”+sumOf);

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

System.out.println(“************************************”);

}

catch(Exception e)

{

Exception newEx=new Exception(“Error at:”+new java.util.Date()+””,e);

newEx.printStackTrace();

}

}

}

Java Basic Problems

Java:Check Empty String and Avoid Null Pointer Exception

In my last post I have shared about the features of  Final Class,Variables and Methods.

Today I am going to share about how to check a string is Empty or not and also how to avoid Null Pointer Exception in java which occurs due to null value of  string.

Here I have used JOptionPane showInputDialog to take String Input from the user and then I checked the String entered by the user is empty or not.

If we check the string using the method given below we will be able to avoid Null Pointer Exception.

 

null pointer exception java

Here is the given example:

import javax.swing.*;

public class StringDemo

{

 StringDemo()

{

String reasonMsg;

reasonMsg=JOptionPane.showInputDialog(“Pleas Enter Your Name”,””);

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

System.out.println(“ReasonMsg:”+reasonMsg);

try

                                                {

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

                             if(reasonMsg.trim().toString().length()==0)

                                      {

          JOptionPane.showMessageDialog(null,”You Entered Nothing”);

                                      }

                                                 if (reasonMsg.trim().toString().length()!=0)

                                                 {   

                                                                   JOptionPane.showMessageDialog(null,”You Name is:”+reasonMsg);

          System.out.println(“Your name is :”+reasonMsg);

                                                 }  

                                                }

          catch (Exception ex) {

Exception newEx = new Exception(“Error at:”+new java.util.Date()+””,ex);

 newEx.printStackTrace();

                                                }

}

public static void main(String args[])

{

StringDemo x=new StringDemo();

}

}