Skip to main content

Posts

Copying files from location to another in linux using Java

import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.PrintWriter; public class TestCopyFile { public static void main(String[] args) { File file1=new File(dummy path); String path=System.getenv("HOME"); File file2=new File(path+"/Desktop/dump/CSV_branch_master.csv"); System.out.println(path); System.out.println("after line2"); BufferedReader reader; PrintWriter writer; String line; try { System.out.println("inside try"); if(file2.createNewFile() || !file2.createNewFile()) { System.out.println("inside if"); reader =new BufferedReader(new FileReader(file1)); writer=new PrintWriter(new FileWriter(file2)); while((line=reader.readLine())!=null) { writer.println(line); } reader.close(); writer.close(); } }catch(Exception e) { System.out.println(e +"couldn...

Read different types of data from standard input, process them as shown in output format and print the answer to standard output.

/* Problem Statement: Read different types of data from standard input, process them as shown in output format and print the answer to standard output. Input format: First line contains integer N. Second line contains string S. Output format: First line should contain N x 2. Second line should contain the same string S. Constraints: 0≤N≤10 1≤|S|≤15 where |S|= length of string S  */ import java.util.Scanner; public class TestClass {      public static void main(String[] args) {         Scanner sc = new Scanner(System.in);         int number = 0;         String string = null;         int firstInput = sc.nextInt();         String secondInput = sc.next();         // apply constraints         if (firstInput >= 0 && firstInput <= 1...

Even Or Odd Number!!

Such programs are too simple and requires the fundamental logic for checking whether a number is odd or ever. Any number which is divisible by 2 is even else odd. In the below code snippet, you will find two operators % and /.  x=4 y=2 x%y will computes to 0 which is the remainder!! x/y will compute to 2 which is the quotient. # Below is the code: class EvenOrOdd { public String compute(int x) { if (x % 2 == 0) { return x + " is even"; } else return x + " is odd"; } } public class TestEvenOrOdd { public static void main(String[] args) { EvenOrOdd ref = new EvenOrOdd(); System.out.println(ref.compute(5)); System.out.println(ref.compute(10)); } } #Output:

Reversing of a string[Customized Logic]

# String in java is a class!! String objects are immutable which means once created cannot be destroyed. There are various way of instantiating or creating objects of string. 1. String s1="ABC"; 2. String s2=new String("ABC"); 3. char [] abc={'A','B','C'};     String s3=new String(abc); So the problem statement is to reverse a string. Input="ABCFAD" Outpt=DAFCBA. Below is the code snippet to achieve the above problem statement. class StringReversing { public void computeStringReversal(String string){ // convert the string into character of array char [] characterArray=string.toCharArray(); int lowerIndex=0; int higherIndex=string.length()-1; char temp; while(higherIndex>lowerIndex) { temp=characterArray[lowerIndex]; characterArray[lowerIndex]=characterArray[higherIndex]; characterArray[higherIndex]=temp; higherIndex--; lowerIndex++; } System.out.prin...

Sorting an Array[Customized Logic ]

Sorting of array can be done through various logic's!! Either through using Arrays.sort() method or through customize logic. Here we are going to discuss the customized logic.  Below is the logic snippet for sorting the elements of an integer array Logic for custom sort: for(int i=0;i<size-1;i++) { for(int j=0;j<size-1;j++) { if(arr[j]>arr[j+1]) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } use of sort method: Arrays.sort(arr); Complete Code: class SortingArray { public void customSort(int [] arr) { int size=arr.length; int temp=0; // outer loop for(int i=0;i<size-1;i++) { // inner loop for(int j=0;j<size-1;j++) { if(arr[j]>arr[j+1]) { /* * Swapping logic */ temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } // displaying the elements of the array for...

Generate the Series up to N and sum it all together! N=13 [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]

Let's say N=13, then the we have to generate the series till the number(including N) and evaluate the entire expression as  [+1-2+3-4+5-6+7-8+9-1+0-1+1-1+2-1+3]. It can be solved in various way, among all i am sharing one of the approach to solve it. public class ExpressionEvaluator { public static void main(String[] args) { int temp; int rem; int sum=0; int number=10; // get the sequence of numbers starting from 1 to 13 int i=1; while(i<=number) { if(i<=9) { rem=i%10; if(rem%2==0) { rem=-(rem); sum=sum+rem; } else if(rem==1) { sum=sum+rem; } else { sum=sum+rem; } } else { rem=i%10; //+0     // +1    //+2  //3  // 0  // 1  //2 // 3 temp=i/10; // 1   //1     //1    // 1   //2  ...

Tokenize a string input except double quotes(")

Sample Input: abc def ghi "jkl mno" pqrs "tuv" wxyz Expected Output:  abc def ghi "jkl mno"  pqrs "tuv" wxyz 1. One of the easiest way to solve the problem statement is through Patters!! "([^\"]\\S*|\".+?\")\\s*" - is the pattern to parse the string except double quotes. Complete Program. // import the required packages import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; class TokenizeString {     public static void main(String [] args) {     // user input     System.out.println("Enter the string");    // Instantiate the Scanner class     Scanner scanner=new Scanner(System.in);   // store the entire line in the variable    String queryString=sc.nextLine();   // instantiating the ArrayList object    List<String> list = new ArrayList...