Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

10 September 2014

Java Program to remove Blank lines from a given file


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;


public class RemoveBlankLines {


    public static void main(String[] args) throws Exception{
   
        Scanner s = new Scanner(System.in);
        System.out.println("Enter source file name");
        String srcfile = s.nextLine();
       
        File src = new File(srcfile);
        FileReader fr = new FileReader(src);
        BufferedReader br = new BufferedReader(fr);
       
        File trg = new File("tempfile.txt");
        FileWriter fw = new FileWriter(trg);
       
        String line;
        line = br.readLine();
       
        while(line!=null) {
            if(line.length() > 0)
                fw.write( line + "\n");
            line = br.readLine();
        }
                fw.close();
                fr.close();
                src.delete();
                trg.renameTo(src);
    }
   
}

2 September 2014

Java Program to find your age from given date of birth


// Calculate age in years for given date of birth

import java.util.Calendar;
import java.util.Scanner;

public class AgeCalculator {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        System.out.println("Enter date of birth [dd-mm-yyyy]:");
        String dobstring = s.nextLine();

       String [] parts= dobstring.split("-");
        Calendar dob = Calendar.getInstance();
        dob.set(Integer.parseInt(parts[2]),Integer.parseInt(parts[1]),Integer.parseInt(parts[0]));

        Calendar cd = Calendar.getInstance();  //get system date
        long ms = cd.getTimeInMillis() - dob.getTimeInMillis();
        long days = ms / (1000*60*60*24);
        System.out.printf("Age in years : %d\n", days/365);

  

    }

  

}


28 August 2014

Installation of Java

These are the steps you need to follow to install java 

1. Where to Download Java

Latest version can be downloaded at JAVA


2. Java Installation 

Like any other software run the (.exe) file and the installation starts.


3. Setting up the Environment Variables

Steps to set JDK Path and Classpath in Windows 7 and Windows 8

Before setting JDK Path, let's first verify Type javac in command prompt in windows 8 and see output, if you get javac is not recognized as an internal or external command, means JDK Path is not set.

1. Confirm that PATH is not set for Java by typing javac in command prompt.
2. Open Control Panel and Select System and Security
3. Select System
4. Select Advanced System Settings
5. Select Environment Variables
6. Select and Edit Path Environment variable
7. Verify Java path by typing javac in command prompt


Step 1 : Open Control Panel in Windows 8
How to set PATH in Windows 8 operating System




















Step 2 : Select System
How to set Classpath in Windows 8

Step 3 : Select Advanced System Settings in Windows 8
How to set path in Windows 7

Step 4 : Select Environment Variables
How to set Classpath in Windows 7 operating system


Step 5 : Select and Edit Path Environment variable in Windows 8
set path environment variable in windows 8 operating system

Step 6 : Verify Java path by typing javac in command prompt
just type javac command, this time you will see different output which shows various details of java command rather than getting earlier error.You can follow similar process to set Classpath in windows 8 as well.

Getting Started

Hello World Programs in Various languages 

Java :

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World");
    }

}



*********************************************************************************************************

C : 

//C hello world example
#include <stdio.h>
 
int main() {

  printf("Hello world\n");
  return 0;
}

****************************************************************************

Python :

Print "Hello World"

***************************************************************************

Php :

<?php
  Print "Hello World! ";
?>

***************************************************************************

C++ :
#include<iostream.h>
int main() {
std::cout << "Hello World!" << std::end1;
return 0;
}

***************************************************************************

Now When you have the programs ready , whats the next step ? -- just Compile and run it . For this you need softwares of that particular programming
languages to be installed in your system. 

To make it convinient for you there is other way you can compile and run without installing any softwares . All you need is an Internet Connection .

If you do have , then goto http://www.compileonline.com/ 
Select the language you want to code on and write and execute your program . your task is done .

But If you want to install the softwares into your system , then go through the next post that is how to install and run programs .



@EnjoyLearning