Posts

Showing posts from December, 2021

Take Input from user in Java

 import java.io.*; // first line step 1 public class DemoInput2 { public void abc(String name,int age) { System.out.println("name "+name+" age "+age); } public void calc() throws IOException , NumberFormatException { BufferedReader brr = new BufferedReader(new InputStreamReader(System.in)); System.out.println("enter no1"); //String n = br.readLine();  // input 1 //String na= n.trim(); //int nn = Integer.parseInt(na); // short way int nn = Integer.parseInt(brr.readLine().trim()); System.out.println("enter no2"); //String n1 = br.readLine(); // input 2 //int nn1 = Integer.parseInt(n1); int nn1 = Integer.parseInt(brr.readLine().trim()); int sum = nn+nn1; System.out.println(sum); } public static void main(String[] args) throws IOException  // step 2 { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println(&q

DemoException java

  public class DemoException { public void calculate(int i,int j) // throws Exception { System.out.println(i+j); try { System.out.println(i/j);  // expect error can come } catch(Exception e) { // e.printStackTrace();  // system print error //System.err.println("my error message");  // user error    // alternate System.out.println("alternate code "+i/1); } finally { System.out.println(i-j);   // always execute  System.out.println(i*j); } } public static void main(String[] args) throws Exception { // TODO Auto-generated method stub DemoException d = new DemoException(); d.calculate(12, 2); } }

ArrayPyramid

  public class ArrayPyramid { public static void main(String[] args) { // TODO Auto-generated method stub String palindrome = "Devendra";         int len = palindrome.length();           System.out.println("length :"+len);         char tempCharArray[]  = new char[len];   // single dimention                  // put original string in an          // array of chars         for (int i = 0; i < len; i++) {     // add values in single dimention..             tempCharArray[i] =                  palindrome.charAt(i);         }          char c[][]=new char[len][len];    // two dimention         for (int row = 0; row < len; row++) {                  for(int col=0;col<=row;col++) { // same no of cols as row no i.e 1st row has 1 col and 2nd row has 2 cols                        // add values in two dimention       c[row][col]=tempCharArray[col]; //System.out.println("c["+(row)+"]["+(col)+"]="+tempCharArray[col]);  System.out.print(

Array In java

  public class ArrayArrange { public static void main(String[] args) { int number[]={55,40,8,65,41}; //declare         for(int i=0;i<number.length;i++)             {                   System.out.println("outer for"+i);          for(int j=i+1;j<number.length;j++)              {                     System.out.println("inner for "+j);      System.out.println(  "if("+number[i]+" > "+number[j]+")");              if(number[i] > number[j])               { System.out.println(" int temp="+number[i]);          int temp=number[i];           System.out.println("temp :"+temp);    System.out.println(number[i]+"="+number[j]);           number[i]=number[j];  // replace value        System.out.println( number[j]+"="+temp);                number[j]=temp;                       } // end of if         }  // end of inner for             }  // end of ou