import java.util.*;


/* class StringExample{
   public static void main (String[] args) {
     String s1="HeLo";
     String s2="Hello world";
    // System.out.println( "ps1 "+s1.substring(2));
    // System.out.println( "ps2 "+s2.substring(2,10));
    // System.out.println( "ps3 "+s1.toCharArray());
    //   System.out.println( "ps4 "+s1.indexOf("eLo"));
    //   System.out.println( "ps5  "+s2.split(" "));
    //   System.out.println( "ps6 "+Arrays.toString(s2.split(" ")));
      String s3="hello@world@hii@world";
      System.out.println( "ps7 "+Arrays.toString(s3.split("@")));
      String s4="hello^world^hii^world";
      String s5=Arrays.toString(s4.split("\\^"));
      System.out.println( "ps8 "+"conveted to array and splited "+s5);
      System.out.println(s5);
      String s6[]=s4.split("\\^");
       for(int i=0;i<s6.length;i+=2){
         System.out.println( "ps9 "+s6[i]);
      }
      for(int i=0;i<s6.length;i++){
        for(int j=0;j<s6[i].length();j++){
         System.out.print(" ps"+i+""+j+" "+s6[i].charAt(j));
         }
         System.out.println();
      }
      System.out.println( "ps11 "+"immutable:: "+s4);
      System.out.println( "ps12 "+"hii this is \"nithin's\" company");
     // String s7="hello""world""hii""world"; // not getting
      //System.out.println(Arrays.toString(s5.split("\)))   
      }
 }        */
/*class A extends B {
  void dumy(){
    System.out.println("class one");
  }
}
class B extends C {
  void dumy2(){
    System.out.println("class two");
  }
}
class C{
  void dumy3(){
    System.out.println("class three");
  }
}
public class Main {
    public static void main(String[] args) {
      A a=new A();
      a.dumy3();
      a.dumy2();  
      a.dumy();
  }
}*/
/*Output:
class three
class two
class one
*/

// Task-1 write a java program to count the number of characters in a string
/*class Task1{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in);
     System.out.println("Enter a String::");
     String str=sc.nextLine();
     int count=0 ;
     for(int i=0;i<str.length();i++){
       count++;
     }
     System.out.println("no of Characters in given string is = "+count);
  }
}*/

/*STDIN
java is super

Output:

Enter a String::
no of Characters in given string is = 13
*/
// Task-2 write a java program to count the number of words in a string
//task-2.1  without trailing and leading spaces (youtube)
/*class Task2{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in);
     System.out.println("Enter a String::");
     String str=sc.nextLine();
     int count=1 ;
     for(int i=0;i<str.length();i++){
            if(str.charAt(i)==' '){
            count++;
            }
     }
     System.out.println("words in a given string = "+count);
  }
}*/

    
 
/*STDIN
java is super

Output:

Enter a String::
words in a given string = 3
*/

//task-2.2.1  with trailing and leading spaces (myversion with count=1)
/*class Task221{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in);
     System.out.println("Enter a String::");
     String str=sc.nextLine();
     int count=1 ;  // here leading and trailing spaces are neglected by not considering there index number 
     int n=str.length();    // this method applicable when only one leading and one trailing space;
     for(int i=1;i<str.length()-1;i++){
            if(str.charAt(i)==' '){
            count++;
            }
     }
     System.out.println("words in a given string = "+count);
     System.out.println(n);
  }
}*/
/*STDIN
 java is super 

Output:

Enter a String::
words in a given string = 3
15*/
//task-2.2.2  with trailing and leading spaces (my version with count=-1)
/*class Task222{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in);
     System.out.println("Enter a String::");
     String str=sc.nextLine();
     int count=-1 ;  // i taken -1 because if it reads 2 blanks count will be=1 so nowords is 1 due to trailing and leading spaces
     int n=str.length();
     for(int i=0;i<str.length();i++){
            if(str.charAt(i)==' '){
            count++;
            }
     }
     System.out.println("words in a given string = "+count);
     System.out.println(n);
  }
}*/
    
 /*
STDIN
 java is super 

Output:

Enter a String::
words in a given string = 3
15*/


// Task-3 write a java program to count the total number of occurences of a given character in a string

/*class Task3{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in);
     System.out.println("Enter a String::");
     String str=sc.nextLine();
     System.out.println("Enter a character to check occurence::");
     char ch=sc.next().charAt(0);
     int count=0;
     int n=str.length();
     for(int i=0;i<str.length();i++){
            if(str.charAt(i)==ch){
            count++;
            }
     }
     System.out.println(n);
     System.out.println("given Character to check occurence is "+ch );
     System.out.println("total number of occurences of a given character in a string = "+count);
     
  }
      
}*/
 
/*STDIN
Hello world
l
Output:
11
given Character to check occurence is l
total number of occurences of a given character in a string = 3
*/

// Task-4 write a java program to reverse a String
/*class Task4{
  public static void main (String[] args) {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a string");
    String st=sc.nextLine();
    String rev="" ;
    for(int i=st.length()-1;i>=0;i--){
           rev=rev+st.charAt(i);
    }
    System.out.println("reversed String is: "+rev);
  }
}*/
/*STDIN
java is super

Output:

Enter a string
reversed String is: repus si avaj
*/

// Task-5 write a java program to remove all Starting and ending spaces from a String

/*class Task5{
  public static void main (String[] args) {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter a string");
    String st=sc.nextLine();
    System.out.println(st.trim());
  }
}*/
/*STDIN
   hello world  

Output:

Enter a string
hello world*/

// prog to reverse each wrd of a given string   ratalleduu wrk smart to get it 
/*class Task6{ 
  public static void main (String[] args) {
     Scanner sc=new Scanner(System.in);
     String str1=sc.nextLine();
     String [] str2=Arrays.toString(str1.split(" "));
      System.out.println(str2) ;
         
      }
}
*/


//TAsk-6 WAJP to reverse  each word of a given string 
  // input:: java is easy ------> output:: avaj si ysae.
/*  public class Task6{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String as input:");
    String str1=sc.nextLine();
    String[] str2=str1.split(" ");
    String str3="";
    for(int i=0;i<str2.length;i++ ){
      for(int j=str2[i].length()-1;j>=0;j--){
        str3=str3+(str2[i].charAt(j)) ;
      }
      str3=str3+" ";
  }
    System.out.print(str3) ;
  }
}      */
/*STDIN
java is super tricky
Output:
Enter String as input:
avaj si repus ykcirt */
    
  // Task-7 WAJP tochange odd words to Uppercase  and reverse the even words..
/* public class Task7{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String as input:");
    String str1=sc.nextLine();
    String[] str2=str1.split(" ");
    String str3="";
     for(int i=0;i<str2.length;i++){
          if(i%2!=0){
            for(int j=str2[i].length()-1;j>=0;j--){
            str3=str3+(str2[i].charAt(j));
             }
            str3=str3+" ";
          }
         

      else{
         str3=str3+(str2[i].toUpperCase())+" " ;
       }
  
     }
     System.out.println(str3);
  } 
}     */
/*STDIN
This is a test String!!
Output:
Enter String as input:
THIS si A tset STRING!! 
STDIN
java is super tricky
Output:
Enter String as input:
JAVA si SUPER ykcirt 

*/
// Task-8   WAJP to find the count of uppercaseLetters,lowerclassletters,digits and special characters
//in a given string 

/*public class Task8{
  public static void main (String[] args) {
     Scanner sc= new Scanner(System.in) ;
      System.out.println("enter a String with combination of letters, digits and special characters");
      String str=sc.nextLine();
      String str1="ABCDEFGHIGKLMNOPQRSTUVU" ;
      String str2="abcdefghijklmonopqrstuvwxyz" ;
      String str3="!@#$%^&*" ;
      String str4="0123456789" ;
      int uppercaseLetters=0;
      int lowercaseLetters=0;
      int specialCharacters=0;
      int digits=0;
      for(int i=0;i<str.length();i++){
        for(int j=0;j<str1.length();j++){
          if(str.charAt(i)==str1.charAt(j)){
          uppercaseLetters++;
          }
        }
        for(int k=0;k<str2.length();k++){
          if(str.charAt(i)==str2.charAt(k)){
          lowercaseLetters++;
          }
        }
        for(int l=0;l<str3.length();l++){
          if(str.charAt(i)==str3.charAt(l)){
          specialCharacters++;
          }
        }
        for(int m=0;m<str4.length();m++){
          if(str.charAt(i)==str4.charAt(m)){
               digits++;
          }
        }
        
      }
      System.out.println("uppercaseLetters = "+uppercaseLetters);
      System.out.println("lowercaseLetters = "+lowercaseLetters);
      System.out.println("specialCharacters = "+specialCharacters);
      System.out.println("digits = "+digits);
      
  }
}*/
/*STDIN
JavA5is&Su6p%eR
Output:
enter a String with combination of letters, digits and special characters
uppercaseLetters = 3
lowercaseLetters =7
specialCharacters = 2
digits = 2
*/
// not completed
//Task-9 WAP to find the first repeated and non repeated character in the given string
/*public class Task9{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String as input:");
    String str1=sc.nextLine();
    char frc=str1.charAt(2);
    char fnrc=str1.charAt(0);
    for(int i=0;i<str1.length();i++){
      for(int j=i+1;j<str1.length();j++){
        if(str1.charAt(i)!=str1.charAt(j)){
          fnrc=str1.charAt(i);
          break ;
        }
      }
      break;
    }  
    System.out.println(frc);
    System.out.println(fnrc);
    
 }
}*/
 // task-10 WAP to create an array using words at odd positions in a string 
/*public class Task10{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String as input:");
    String str1=sc.nextLine();
    String[] str2=str1.split(" ");
    String str3="";
     for(int i=1;i<str2.length;i+=2){
         str3=str3+str2[i]+" ";
     }
     System.out.println(str3);
  }
}*/
/*STDIN
java is super keka easy and awesome
Output:
Enter String as input:
is keka and 
*/


// Task-11 WAJPT find max length-word in a given string 
//(if two words are of same length then return the first occuring word of max-length)
/*public class Task11{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter String as input:");
    String str1=sc.nextLine();
     String []str2=str1.split(" ");
     String maxLengthWord=str2[0];
     for(int i=0;i<str2.length;i++){
       if(str2[0].length()<str2[i].length()){
           maxLengthWord=str2[i];
         }
      }
      System.out.println("maxlength-word:: "+maxLengthWord);
  }
}*/
/*STDIN
hello how are you moooommmmm
hello how are you reddy
Output:
Enter String as input:
maxlength-word:: moooommmmm
maxlength-word:: hello
*/

//  NOT COMPLETED
// task-12 compare two strings,if the character in string are presents in string2,then it should be
//printed as such in output,else '+' should be printed in ooutput
/*public class Task12{
  public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter input-1:");
    String str1=sc.nextLine();
    System.out.println("Enter input-2 :");
    String str2=sc.nextLine() ;
  }
}*/

// Task-13 print the last name first then followed by "," and first character of the first name::
/*public class Task13{
public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter your First Name:");
    String str1=sc.nextLine();
    System.out.println("Enter your Last Name :");
    String str2=sc.nextLine() ;
    String str3=str2+","+str1.substring(0,1);
    System.out.println("output will contain LastName followed by ',' and first char of FirstName");
    System.out.println(str3);
  }
}*/

/*STDIN
Steve
Jobs
Output:
Enter your First Name:
Enter your Last Name :
output will contain LastName followed by ',' and first char of FirstName
Jobs,S*/

//task-14 printing file extension

/*public class Task14{
public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a filename with extension:");
    String str=sc.nextLine();
    int count=0;
    int n=str.length();
    String str2="" ;
    for(int i=0;i<n;i++){
        if(str.charAt(i)=='.'){
          //int j=i+1
         str2=str.substring(i+1,n) ;
        }
        
      }
      System.out.println("file extension of "+str+" file is:: "+str2);
      
     }
}*/
/*STDIN
java is super.jpg
Output:
Enter a filename with extension:
file extension of java is super.jpg file is:: jpg
  */
         
//task-15 cheking whether a given string is palindrome or not
/*public class Task15{
public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a string :");
    String str=sc.nextLine();
    int count=0;
    int n=str.length();
   // String str2="" ;
    boolean palindrome=true;
    for(int i=0;i<n/2;i++){
         if(str.charAt(i)!=str.charAt(n-i-1)){
             palindrome=false;
          }
       }
       if(palindrome==true){
         System.out.println("the given string "+str+" is a palindrome");
       }
        else {
         System.out.println("the given string "+str+" is not a palindrome");
        }
   }
}*/

/*STDIN
hyrtutorials

Output:

Enter a string :
the given string hyrtutorials is not a palindrome
STDIN
JavaJ

Output:

Enter a string :
the given string JavaJ is a palindrome


 */
// another way palindrome         
/* public class Example{
   public static void main (String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter a string :");
    String str=sc.nextLine();
    int count=0;
    int n=str.length();
     String str2="" ;
    for(int i=n-1;i>=0;i--){
         str2=str2+str.charAt(i);
       }
       if(str2.equals(str)){
         System.out.println("the given string "+str+" is a palindrome");
       }
        else {
         System.out.println("the given string "+str+" is not a palindrome");
        }
   }
} */            

/*STDIN
the quick brown fox jumps over the lazy dog
Output:
Enter a string :
the given string the quick brown fox jumps over the lazy dog is not a palindrome
         */
         
         
 class StringExample{
   public static void main (String[] args) {
    // String s1="HeLo";
     String s2="Hello world";
    // System.out.println( "ps1 "+s1.substring(2));
    // System.out.println( "ps2 "+s2.substring(2,10));
    // System.out.println( "ps3 "+s1.toCharArray());
    //   System.out.println( "ps4 "+s1.indexOf("eLo"));
    //   System.out.println( "ps5  "+s2.split(" "));
          String s7[]=s2.split(" ") ;
          System.out.println("s7 splitted and converted to array "+s7);
          for(int i=0;i<s7.length;i++){
             System.out.println( "ps7 "+i+" "+s7[i]);
          }
          System.out.println();
      System.out.println( "ps6 "+Arrays.toString(s2.split(" ")));
      String s3="hello@world@hii@world";
      System.out.println( "ps7 "+Arrays.toString(s3.split("@")));
      String s4="hello^world^hii^world";
      String s5=Arrays.toString(s4.split("\\^"));
//arrays.to string is used to converte splitted array into string form butshows in square braces 
      System.out.println( "ps8 "+"showing in array form but nt array and splited "+s5);
      System.out.println("s5 printing.. (we cannt acces Arrays.toString form)"+s5);
      String s6[]=s4.split("\\^");
       for(int i=0;i<s6.length;i+=2){
         System.out.println( "ps9 "+s6[i]);
      }
      for(int i=0;i<s6.length;i++){
        for(int j=0;j<s6[i].length();j++){
         System.out.print(" ps"+i+""+j+" "+s6[i].charAt(j));
         }
         System.out.println();
      }
      System.out.println( "ps11 "+"immutable:: "+s4);
      System.out.println( "ps12 "+"hii this is \"nithin's\" company");
     // String s7="hello""world""hii""world"; // not getting
      //System.out.println(Arrays.toString(s5.split("\)))   
      }
 }         
         
         
         
         
         
         
         
         
         
         
         
   
by

Java online compiler

Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast. The editor shows sample boilerplate code when you choose language as Java and start coding.

Taking inputs (stdin)

OneCompiler's Java online editor supports stdin and users can give inputs to the programs using the STDIN textbox under the I/O tab. Using Scanner class in Java program, you can read the inputs. Following is a sample program that shows reading STDIN ( A string in this case ).

import java.util.Scanner;
class Input {
    public static void main(String[] args) {
    	Scanner input = new Scanner(System.in);
    	System.out.println("Enter your name: ");
    	String inp = input.next();
    	System.out.println("Hello, " + inp);
    }
}

Adding dependencies

OneCompiler supports Gradle for dependency management. Users can add dependencies in the build.gradle file and use them in their programs. When you add the dependencies for the first time, the first run might be a little slow as we download the dependencies, but the subsequent runs will be faster. Following sample Gradle configuration shows how to add dependencies

apply plugin:'application'
mainClassName = 'HelloWorld'

run { standardInput = System.in }
sourceSets { main { java { srcDir './' } } }

repositories {
    jcenter()
}

dependencies {
    // add dependencies here as below
    implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
}

About Java

Java is a very popular general-purpose programming language, it is class-based and object-oriented. Java was developed by James Gosling at Sun Microsystems ( later acquired by Oracle) the initial release of Java was in 1995. Java 17 is the latest long-term supported version (LTS). As of today, Java is the world's number one server programming language with a 12 million developer community, 5 million students studying worldwide and it's #1 choice for the cloud development.

Syntax help

Variables

short x = 999; 			// -32768 to 32767
int   x = 99999; 		// -2147483648 to 2147483647
long  x = 99999999999L; // -9223372036854775808 to 9223372036854775807

float x = 1.2;
double x = 99.99d;

byte x = 99; // -128 to 127
char x = 'A';
boolean x = true;

Loops

1. If Else:

When ever you want to perform a set of operations based on a condition If-Else is used.

if(conditional-expression) {
  // code
} else {
  // code
}

Example:

int i = 10;
if(i % 2 == 0) {
  System.out.println("i is even number");
} else {
  System.out.println("i is odd number");
}

2. Switch:

Switch is an alternative to If-Else-If ladder and to select one among many blocks of code.

switch(<conditional-expression>) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 //code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition. Usually for loop is preferred when number of iterations is known in advance.

for(Initialization; Condition; Increment/decrement){  
    //code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(<condition>){  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (<condition>); 

Classes and Objects

Class is the blueprint of an object, which is also referred as user-defined data type with variables and functions. Object is a basic unit in OOP, and is an instance of the class.

How to create a Class:

class keyword is required to create a class.

Example:

class Mobile {
    public:    // access specifier which specifies that accessibility of class members 
    string name; // string variable (attribute)
    int price; // int variable (attribute)
};

How to create a Object:

Mobile m1 = new Mobile();

How to define methods in a class:

public class Greeting {
    static void hello() {
        System.out.println("Hello.. Happy learning!");
    }

    public static void main(String[] args) {
        hello();
    }
}

Collections

Collection is a group of objects which can be represented as a single unit. Collections are introduced to bring a unified common interface to all the objects.

Collection Framework was introduced since JDK 1.2 which is used to represent and manage Collections and it contains:

  1. Interfaces
  2. Classes
  3. Algorithms

This framework also defines map interfaces and several classes in addition to Collections.

Advantages:

  • High performance
  • Reduces developer's effort
  • Unified architecture which has common methods for all objects.
CollectionDescription
SetSet is a collection of elements which can not contain duplicate values. Set is implemented in HashSets, LinkedHashSets, TreeSet etc
ListList is a ordered collection of elements which can have duplicates. Lists are classified into ArrayList, LinkedList, Vectors
QueueFIFO approach, while instantiating Queue interface you can either choose LinkedList or PriorityQueue.
DequeDeque(Double Ended Queue) is used to add or remove elements from both the ends of the Queue(both head and tail)
MapMap contains key-values pairs which don't have any duplicates. Map is implemented in HashMap, TreeMap etc.