Java Split Method

What Is Java Split Method?

programming Java, Java Split Method
Programming Java

There’s a method in java that is used to “split” string into pieces, the split is based on a given regular expressions and a return char array. There are 2 type of syntax that can be used for Java Split Method, (for a fun fact java split method is available since java 1.4).

Continue reading Java Split Method

Java Conversion : Float to String

In Java we can Convert from a Float value to String using String.valueOf() and Float.toString() method

When? do we need to Convert Float to String, usually when we want to insert a Float value into a TextField. Because TextField only display a String value.

String.valueOf()

This method is one of an overloaded method. This method is one of the methods that is used to convert float to string method. The valueOf() is the static method of String class. The syntax of valueOf() method for the float conversion is given as in below :

public static String valueOf(float f)  

Example for a conversion :

1st one
float f=27.4F;//F stands for Float
String s=String.valueOf(f);

2nd one
public class FloatToStringExmpl{
public static void main(String args[]){
float f=27.4F;//F stands for Float/
String s=String.valueOf(f);
System.out.println(s);
}}  

This will output :
27.4 

Float.toString()

The method of Float.toString() is another method that can convert Float value into String Text. Float.toString() method is the static method of Float class. The syntax toString() method for the float conversion can you see below :

public static String toString(float f)

here’s an example for a conversion :
1st one 
float f=168.3F;
String s=Float.toString(f);

2nd one
public class FloatToStringExmpl{
public static void main(String args[]){
float f=168.3F;
String s=Float.toString(f);
System.out.println(s);
}}

This will output :

168.3

Java Conversion : Double to String

    In java we can convert a Double value or we could also say it a “float” value, into a String value. To do that work we used 2 method which is : String.valueOf() and Double.toString().
in what occasion we should do conversion between Double to String, for example : when we’re going to input a double / float value into a textField, and since textField handle String value that’s when we need to convert to String value.

.toString() Method
.toString Method is used to convert from float value into String value. the toString() is a static method of the Double class. You can see the syntax below :
public static String toString(double d);

Exmpl :

we’re going to convert from this data :

double d= 41.3;
String s=Double.toString(d);

here is the code :

public class DoubleToStringExmpl{
public static void main(String args[]){
double d = 41.3;
String s = Double.toString(d); //in here we convert the d value to be s value.
System.out.println(s);
}}

This’ll output :

“41.3” (in a String form)

.valueOf() Method

.valueOf Method is used to convert from float value into String value. valueOf is an overloaded method and which valueOf is the static class from the String class. You can see the syntax below :

public static String valueOf(double d);

Exmpl :

we’re going to convert from this data :

double d = 9.18;
String s = String.valueOf(d);

here is the code :

public class DoubleToStringExmpl2{
public static void main(String args[]){
double d = 9.18;
String s = String.valueOf(d);
System.out.println(s);
}}

This’ll output :

“9.18” (in a String form)

Java Conversion : String to Double

How is it going everybody, good night in 2017. Well, our discussions about conversion is kinda… not done yet, because I have 1 more conversion to tell you. It is conversion from String to Double (well you kinda already know from title already). This conversion maybe is not too important because people rarely do mathematics operations with double values, but incase… you’ll never know when you need it.

 In the given circumstances we’ll need to convert from String to Double :
When we’re given a double value into a textfield or a textarea, it’s automatically turned into String value and when we want to do mathematical operations with it we need to change it to the form of double values again.


Syntax
public static double parseDouble(String s);

Simple Example :

double d = Double.parseDouble(“14.24″);

Real Example :

public class StringtoDoubleExmpl{   
public static void main(String args[]){   
String s=“14.24” 
double d=Double.parseDouble(“14.24”);   
System.out.println(d);   
}}  

This will output :
14.14

Conclusions

  • Double.parseDouble(String s);
  • public static double parseDouble(String s);

Java Conversion : Integer to String

Okay now we’re going to continue our topics about Java Conversion, last time we talk about how to convert from String to Integer value, and we found out that to do that we use the parseInt() method. Today we’ll find out how are we going to convert from Integer to String value.

In what circumstances do we use Conversion method from Integer to String? usually when we’re about to display a number in a textField because it must be in a String form. There are 2 ways that we could convert Integer to Strings,
First : String.valueOf() method.
Second : Integer.toString() method.


String.valueOf() :
valueOf() method converts Integer to String form, valueOf() is a static from the method of String. The syntax used for valueOf() method is given below :

public static String.valueOf(int i);

Simple Example :

int i = 5;
String s = String.valueOf(i); //now it returns “5” as a String

Real Example :

public class IntegertoStringExmpl{
public static void main(String args[]){

 int i = 400
 String s = String.valueOf(i);

 System.out.println(i – 300);// it becomes an integer binary operation

 System.out.println(s + 200); // it becomes a string concatenation operation
 }  
}   

This will output :
100
400200

Integer.toString() :
toString() method converts Integer to String form, toString() is a static from the method of Integer. The syntax used for toString() method is given below :

public static Integer.toString(int i);

Simple Example :

int i = 5;
String s = Integer.toString();//now it returns “5” as a String 

Real Example :

public call IntegertoStringExmpl{
public static void main(String args[]){
  
 int i = 400;
 String s = String.valueOf(i);

 System.out.println(i – 300); // it becomes an integer binary operation
 System.out.println(s + 200); // it becomes a String concatenation operation


 }
}

This will output :
100
400200

Conclusions 

  • String.valueOf();
  • Integer.toString();
  • valueOf() is static from the String class.
  • toString() is static from the Integer class.

Java Conversion : String to Integer

Do you know? that we can convert String value into Integer values, for you who don’t know… well, You can! convert String value to Integer values, and Java provides the right method for us to do it. And it is called a Conversion term, Conversions means converting one form of a value/data into another form of value/data. Java provides more than 1 method of conversions inside of it, first we’ll see how we would convert from String to Integer, let’s start!

First we need to know what method we’re dealing with, lemme tell you that the method we’re using is Integer.parseInt() method.
In what scenario does programmer deals with converting values? in one scenario, programmer need to perform a somewhat mathematical operations on the string values in form of a number, that they get from textfield or textarea, the entered data from the user is received in form of a string. If we want to convert the String value to Integer value we use the Integer.parseInt() method.

Syntax
the syntax for this method is :


public static int parseInt(String s)

Example time ! :
Now we’ll convert String value of s = 400, to Integer and we’ll do some mathematical operation.

    public class parseIntExmpl{ 
     public static void main(String args[]){ 
      String s = “400”; 
      int i=Integer.parseInt(s); 
       System.out.println(s+300);//400300 because + is string concatenation operator 
       System.out.println(i-200);//200 because + is binary plus operator 
    }

}  

This will output : 

400300
200

Conclusions 

  • Conversions means converting one form of a value/data into another form of
  • parseInt() Method, converts String into Integer.

Queues in Java

Queues in Java is an interface that’s basically orders an element in FIFO or First In First OUT manner.

Here is the methods available Queue Interface :

  • public boolean add(object);
  • public boolean offer(object);
  • public remove();
  • public poll();
  • public element();
  • public peek();

After Queues interface it comes with the PriorityQueue, and PriorityQueue is a class that provides the facillity of using the queue interface, but one thing you need to remember is that PriorityQueue class doesn’t order an element in FIFO manners not like Queue Interface.


Example time ! :
 

    import java.util.*; 
    class PriorityQueueExmpl{ 
    public static void main(String args[]){ 
     
    PriorityQueue<String> queue=new PriorityQueue<String>(); 
    queue.add(“Umat”); 
    queue.add(“Linay”); 
    queue.add(“Breane”); 
    queue.add(“Ceasar”); 
    queue.add(“Rhealdy”); 
           
    System.out.println(“iterating the queue elements:”); 
    Iterator itr=queue.iterator(); 
    while(itr.hasNext()){ 
    System.out.println(itr.next()); 
    } 
     
    queue.remove(); 
    queue.poll(); 
     
    System.out.println(“after removing two elements:”); 
    Iterator<String> itr2=queue.iterator(); 
    while(itr2.hasNext()){ 
    System.out.println(itr2.next()); 
    } 
     
    } 
    }
 

This will output :

iterating the queue elements:

Umat
Breane
Ceasar
Linay
Rhealdy
after removing two elements
Ceasar
Rhealdy


Conclusions 

  • Queues in Java is an interface that’s basically orders an element in FIFO or First In First OUT manner.
  • PriorityQueue class doesn’t order an element in FIFO manners not like Queue Interface.

LinkedList Class Java

There is a class in java that is used for managing arrays, beside arrayList can you tell me what else?.. Well you should already know the answer from the title, it is LinkedList. LinkedList is a class that is used for managing arrays just like array list, but linkedList can input not only to the end of the array like arrayList but linkedList can also input item to the end of the array.

Here is a list of what linkedList can do :

  • LinkedList class uses doubly linked list to store their elements. It needs to extend the AbstractList class and need to implement  List and Deque interface.
  • LinkedList class is non synchronized.
  • LinkedList class maintains insertion order.
  • LinkedList class can contain duplicated elements.
  • An advantage of LinkedList is that manipulation in LinkedList is fast, because there are no shifting that is required to be occurred.
  • LinkedList class can be used in as a list, stack or queue.

Gues… that’s enough theory for LinkedList, and you know what time it is? It’s example time :

    import java.util.*; 
    public class LinkedListExmpl{ 
     public static void main(String args[]){ 
      
      LinkedList<String> lst=new LinkedList<String>(); 
      lst.add(“Mauwi”); 
      lst.add(“Vanesa”); 
      lst.add(“Rafi”); 
      lst.add(“Jeay”); 
     
      Iterator<String> itr=lst.iterator(); 
      while(itr.hasNext()){ 
       System.out.println(itr.next()); 
      } 
     } 
    }  

This will output :
Mauwi
Vanesa
Rafi
Jeay 

Conclusions

  • LinkedList is a class that is used for managing arrays just like array list, but linkedList can input not only to the end of the array like arrayList but linkedList can also input item to the end of the array.
  • Manipulation in LinkedList is fast, because there are no shifting that is required to be occurred.
  • LinkedList class can be used in as a list, stack or queue.  

Java Regex

Hello… How are you? I’m back again after 1 week of silence. And now we’re going to talk about java again, the subject for today is something related to java (of course) and Strings, and it is called Java Regex. Java Regex may sound odd to you, for the first time I heard java regex I thought it must’ve been something related to registration since the word is regex. But the actual meaning is not that close to registration, just like I said above it is actually related to strings and manipulation.

For the definition itself, Java Regex (Regular Expression) is an API that is created and mainly used to define a pattern for searching and manipulating strings.
Most of the time it is used to define constraint on a strings, i.e email validation or a password. I hope after knowing java regex, you’ll be able to do and make some regular expressions or regex using the Java Regex Tester Tool.

For the record, java regex API is provided with 1 interface and 3 classes in the java.util.regex package, which is the one that are in this list :

  • Interface :
    • MatchResult interface
  • Classes :
    •  Matcher class 
    •  Pattern class
    •  PatterSyntaxException

Both of the Matcher class and the Patter class have their own methods, which are provided in this list

Matcher class
the matcher class implements the MatchResult interface. And is used to match an operation of a character. 

  1. boolean matches()   // test whether the regular expression matches the pattern.
  2. boolean find()    //finds the next expression that matches the pattern.
  3. boolean find(int start)  //  finds the next expression that matches the pattern from the given startnumber.
  4. String group()  //  returns the matched subsequence.
  5. int start()   // returns the starting index of the matched subsequence.
  6. int end()  //  returns the ending index of the matched subsequence.
  7. int groupCount()  //  returns the total number of the matched subsequence

Patter class
the pattern class is the compiled version of the regular expression (regex), it is used to define a pattern for the regex machine.

  1. static Pattern compile(String regex)   // compiles the given regex and return the instance of pattern.
  2. Matcher matcher(CharSequence input)  //  creates a matcher that matches the given input with pattern.
  3. static boolean matches(String regex, CharSequence input) //   It works as the combination of compile and matcher methods. It compiles the regular expression and matches the given input with the pattern.
  4. String[] split(CharSequence input)  //  splits the given input string around matches of given pattern.
  5. String pattern() //   returns the regex pattern. 

It’s example time !!! :

     import java.util.regex.*; 
    public class RegexExmpl{ 
    public static void main(String args[]){ 
    //the 1st way 
    Pattern p = Pattern.compile(“.s”);//the dot (.) represents single character 
    Matcher m = p.matcher(“as”); 
    boolean b1 = m.matches(); 
     
    //2nd way 
    boolean b2=Pattern.compile(“.s”).matcher(“as”).matches(); 
     
    //3rd way 
    boolean b3 = Pattern.matches(“.s”, “as”); 
     
    System.out.println(b+” “+b2+” “+b3); 
    }}
 

This will output :

true true true

All off the methods above simply work by matchin one string to the second string and return it as a boolean (true or false).

Conclusions

  • Java Regex is related to strings manipulation.
  • Java Regex (Regular Expression) is an API that is created and mainly used to define a pattern for searching and manipulating strings.
  • Most of the time it is used to define constraint on a strings, i.e email validation or a password.
  • java.util.regex package contains : 1 interface and 3 classes.
  • boolean matches() Method implements MathResult().

String Concatenation

Happy teachers day!! wait, what?? today is not the teachers day for you? well I guess because we’re in a different country, but anyway…. today we’ll talk about concatenation for strings in java, concatenation came from the word concatenate which means : linking (things) to a chain of event or series. 
Well in java, string concatenation will forms/build a new string that is the combination of other string or other multiple strings.
The way programmer concat in java consists of 2 way :

  1. By concat() method, and.. the most usual way :
  2. By + (string concatenation) operator.


Using concat() Method 
String concat() method will concatenates the user specified string to the end of current string. Here is how the syntax would look like :

public String concat(another String);

if you don’t understand it, let’s see how real life code works :

class StringConcatenationExmpl{ 
     public static void main(String args[]){ 
       String s1=”Crafting”; 
       String s2=”ez6″; 
       String s3=s1.concat(s2); 
       System.out.println(s3);//Craftingez6
      } 
    }

If the program is runned this will ouput :
Craftingez6

The way it simply works, first we create String s1 “Crafting” then we create the second String s2 “ez6”, finally when we want to create the third String s3, we want the value to be combination of s1 and s2, so we use the concat method, which add String s2 to the end of String s1.

Using + (String concatenation) operator
the String concatenation + operator add one String to another, just like adding a value of an integer to another integer. :

  class StringConcatenationExmpl{ 
     public static void main(String args[]){ 
       String s=”Crafting”+”ez6″; 
       System.out.println(s);//Craftingez6
     } 
    }  

If the program is runned this will output :
 Craftingez6

The way it works is when we create the String s, the data inside it is a combination of 2 Strings added directly to each other, in this case we add “ez6” to the end of “Crafting”.

An addition, when we concatenate a string we are not stuck with only string value to concatenate, we could also concatenate primitive value, i.e number. Here is an example :

   class StringConcatenationExmpl{ 
     public static void main(String args[]){ 
       String s=20+”Craftingez6″+03; 
       System.out.println(s);//20Craftingez603
     } 
    }  

This will output :
20Craftingez603

Conclusions

  • Concatenation came from the word concatenate which means : linking (things) to a chain of event or series. 
  • String concatenation will forms/build a new string that is the combination of other string or other multiple strings. 
  • There are 2 ways to concatenate :
    • By concat() method, and.. the most usual way :
    • By + (string concatenation) operator.
  • When we concatenate a string we are not stuck with only string value to concatenate, we could also concatenate primitive value, i.e number.