Skip to main content

Posts

Showing posts from March, 2013

Java Program to delete any file or directory

The following code is to delete any file or directory and all of its contents. The deleted file or directory will be deleted permanently same as using SHIFT+DEL keys. Code:- import java.io.*; import java.nio.file.*; import java.util.*; public class Delete {     Scanner scan = new Scanner(System.in);     String pth;     char ch;     boolean empty;     public void input() {         try {             System.out.printf("PATH: ");             pth = scan.nextLine();         } catch (InputMismatchException e) {             System.err.println("Error Occur!\n" + e.getMessage());             System.exit(0);   ...

Java Program to Copy File

The following code is to copy the file from the source to destination and replace if already exists. Code:- import java.nio.file.*; import java.util.*; import java.nio.file.CopyOption.*; public class Copy {     String spth, tpth;     Path source, target;     Scanner scan = new Scanner(System.in);     public void input() {         try {             System.out.printf("SOURCE: ");             spth = scan.nextLine();             System.out.printf("TARGET: ");             tpth = scan.nextLine();         } catch (InputMismatchException e) {             System.err.println("Error Occur!\n" ...

Java Program for File writing

The following code is to create a new text file and save some text in it and then give user the choice either to open the file at the same time or not. (File will be opened using MS Word from its default directory) Code:- import java.io.*; import java.nio.file.*; import java.util.*; public class Files {     static Process Microsoft;     Path path;     @SuppressWarnings("ResultOfObjectAllocationIgnored")     public void file(String pth) throws IOException {         path = Paths.get(pth);         if (!path.toFile().exists()) {             new java.io.File(path.toString()).createNewFile();             writeData(path.toString());         } else {             S...

Java Program to count the words

The following code is to count the total words within a single string entered by the user. Code:- import java.util.*; public class TotalWords {     Scanner scan = new Scanner(System.in);     String input;     long length = 0, i = 0;     public void input() {         try {             System.out.printf("Enter Text: ");             input = scan.nextLine();         } catch (InputMismatchException e) {             System.err.println("Error Occur!\n" + e.getMessage());             System.exit(0);         }     }     public void compute() {       ...

Java Program for a Java Quiz

import java.util.*; public class Quiz{     String quizQuestions, quizOptions, answer, enter;     char ch;     int i, correct=0;     Quiz[] quiz=new Quiz[61];     public void initialise(){                  for(i=0; i<61; i++) {             quiz[i]=new Quiz();         }     }     public void quizQuestions(){         try{             quiz[0].quizQuestions="Who Developed Java?";             quiz[1].quizQuestions="What was the original name of Java?";             quiz[2].quizQuestions="What is the full form of JDK?";     ...

Bubble Sort & Selection Sort

Swapping is the main feature of both the sorting techniques. You must understand the swapping of elements from one index position to another in order to make easier for you to understand the Sorting Techniques . Example:- Before you learn about swapping the values within the Array List , you should learn the simple swapping within two or more variables. int a=10, b=20; Now, to do swapping we need an extra variable. int temp; temp=a; a=b; b=temp; Now, the identifier ‘a’ has the integer value ‘20’ and the identifier ‘b’ has the integer value ‘10’. Step 1: temp=a; By the above step, we have stored the integer value ‘10’ in the identifier ‘temp’. To make it easier just think like this: temp=10; Step 2: a=b; By the above step, we have stored the integer value ‘20’ in the identifier ‘a’. To make it easier just think like this: a=20; Step 3: b=temp; Now, the identifier ‘temp’ has the integer value ‘10’. So, by the above step we have changed the value of ‘b’ i.e., fro...