You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
system.out.print("Hello World!"); //Will not move to the next line.system.out.println("Hello World!"); //Will move to the next line.// There is another type of output method, which is similar to C language.inta = 23;
system.out.printf("value of a is : %d", a);
Input Methods
importjava.util.ScannerScanners = newScanner(System.in);
// input for a integer.inta = s.nextInt();
// input for a string.Stringstr = s.nextLine();
Stringss = s.next(); // Takes input till 1st whitespace.// input for a double.doubled = s.nextDouble();
// datatype array_name[] = new datatype[Size];intnumber[] = newint[10]; // An Integer ArrayStringcharacters[] = newString[10]; // A String arrayint[] number = newint[]{ 1,2,3,4,5,6,7,8,9,10 };
For 2-D array
// datatype array_name[][] = new datatype[row][column];intnumber[][] = newint[10][10]; // An Integer Array of dimensions 10 x 10Stringcharacters[][] = newString[10][10]; // An String Array of dimensions 10 x 10
Traversal
// Traditional for loopfor(inti=0;i<number.length;i++) //length gives the size of the array
{
System.out.println(number[i]);
}
// Advanced for loopfor(inti:number)
{
System.out.println(i);
}
"In Java, string is basically an object that represents sequence of char values. An array of characters works same as Java string." : Javatpoint
Ways to Initialise a string
Using String Literal
Strings = "INPUT";
//Using the new keywordStrings = newString("INPUT");
//From a given character arraycharch[]={'I','N','P','U','T'};
Strings=newString(ch);
The above mentioned methods creates a string that are immutable, to make strings mutable, we can use StringBuilder or StringBuffer
Using StringBuffer
"The string represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences." : GFG
//Create a StringBuffer Object, i.e., empty string buffer//By default it can take up a sequence of 16 charactersStringBuffersb = newStringBuffer();
// Can be initialised with a stringStringBuffersb2 = newStringBuffer("Input");
1) append(string_data) method : Used to concatenate the entered string and the strings in the buffer
sb.append("Input"); //Now the empty string has been modified to "Input"
2) insert(beginIndex, endIndex, string_data) method : Inserts a given string literal to the specified positions
sb.insert(2," A String");
//Now String is "In A Stringput"
3) replace(beginIndex, endIndex, string_data) method : Use to replace a sequence of characters from the specified beginIndex and endIndex-1, with another sequence
sb.replace(11,14," Literal");
//Now String is "In A String Literal"
4) delete(beginIndex, endIndex) method : Use to delete a sequence of characters from the specified beginIndex and endIndex-1
sb.delete(11,19);
//Now the string is "In A String"
5) reverse() method : Reverses the current string
sb.reverse();
// Now the String is "gnirtS A nI"
Using StringBuilder
Very similiar to the StringBuffer class but is not Synchronous and neither Thread-Safe. But high in performance, i.e., speedy
//Create a StringBuilder Object, i.e., StringBuilder with no characters//By default it can take up a sequence of 16 charactersStringBuildersb = newStringBuilder();
StringBuildersb2 = newStringBuffer("Input");
append(), insert(), replace(), delete(), reverse() are used in the same way as used in the StringBuffer
To convert the objects of either StringBuilder or StringBuffer to string, use : toString()
Sets all the elements of this array in parallel, using the provided generator function.
parallelSort(originalArray)
Sorts the specified array using parallel sort.
setAll(originalArray, functionalGenerator)
Sets all the elements of the specified array using the generator function provided.
sort(originalArray)
Sorts the complete array in ascending order.
sort(originalArray, fromIndex, endIndex)
Sorts the specified range of array in ascending order.
sort(T[] a, int fromIndex, int toIndex, Comparator< super T> c)
Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
sort(T[] a, Comparator< super T> c)
Sorts the specified array of objects according to the order induced by the specified comparator.
spliterator(originalArray)
Returns a Spliterator covering all of the specified Arrays.
spliterator(originalArray, fromIndex, endIndex)
Returns a Spliterator of the type of the array covering the specified range of the specified arrays.
stream(originalArray)
Returns a sequential stream with the specified array as its source.
toString(originalArray)
Returns a string representation of the contents of this array. The string representation consists of a list of the array’s elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters a comma followed by a space. Elements are converted to strings as by String.valueOf() function.
A class in java that is used to break a string into tokens based on given delimeter(s), by default breaks at whitespaces
Initialising
// To break the string at whitespaces, use the following codeStringTokenizerst = newStringTokenizer(string_value_or_variable);
// To break the string at multiple delimeters, use the following codeStringTokenizerst = newStringTokenizer(string_value_or_variable, delimiter_string);
Functions available in the StringTokeniser Class
Command
Description
countTokens()
Returns the number of tokens present
hasMoreToken()
Checks if there are more tokens in the string
nextElement()
Return the object of the next element in the stream
hasMoreElements()
Checks if there are more elements in the string
nextToken()
Returns the next token from the StringTokenizer.
Example to break the string at whitespaces
StringTokenizerst = newStringTokenizer("Hy there, how are you? Hoping you are doing great!");
while (st.hasMoreTokens())
{
System.out.print(st.nextToken() + " ; ");
}
Output : Hy ; there, ; how ; are ; you? ; Hoping ; you ; are ; doing ; great! ;
The semi colon is used to seperate the tokens
Example to break the string at multiple delimeters
// The following scheme can be used to break a punctuated string, into wordsStringTokenizerst = newStringTokenizer("Hy there, how are you? Hoping you are doing great!", ":,!? ");
while (st.hasMoreTokens())
{
System.out.print(st.nextToken() + " ; ");
}
Output : Hy ; there ; how ; are ; you ; Hoping ; you ; are ; doing ; great ;