Java Method to Split the Comma in a Separated Line

  • Print this article

The Java programming language offers many built-in functions that allow programmers to accomplish basic tasks in everyday programming. However, these functions are sometimes not readily available to programmers for implementation. Using built-in functions to solve problems can aid production, but does not help beginning programmers learn to solve basic tasks or understand how underlying functions work. For example, while Java contains a built-in string splitting function, learning how to build one from scratch can show how to traverse and manipulate strings, concatenate strings and return arrays from methods.

  1. Basic Function Structure

    • The splitting method will take as an argument a string to split. The function will take this string argument and create a series of smaller sub strings based on the placement of commas in the original string. Since there will possibly be more than one sub string, the function stores these strings in an array of strings, which it passes back to the calling program as a return value:

      class SplitExample{

      public static String[] splitter(String arg){

      }
      }

    Traversing the String

    • The method will, as its primary operation, traverse the string looking for commas. This can be accomplished with a simple "for" loop that iterates over each character in the string. Using the string class's built-in "charAt" function, the programmer can evaluate each character as it is encountered. Each iteration can create a new sub string until encountering a comma:

      public static String[] splitter(String arg){

      String[] list = new String[10]; //sub string array
      count = 0;
      String sub = " "; //current sub string

      for(int i = 0; i < arg.length(); i++){
      sub += arg.charAt(i);
      }
      }

    Splitting the String

    • The loop must separate sub strings based on the placement of commas. Therefore, an "if" statement in the loop will check the current character against the comma character. If there is a match, that means that the current contents of "sub" hold a complete sub string, which can then be stored into the array:

      for(int i = 0; i < arg.length(); i++){

      if(arg.charAt(i) == ','){
      list[count] = sub;
      sub = " ";
      count++;
      }else{
      sub += arg.charAt(i);
      }
      }

    Returning a String Array

    • Finally, all the method needs to do is copy the last sub string from the larger string argument, which will not end with a comma since it resides at the end of the argument string. Then, the array will contain all sub strings separated by commas without including the commas. The method returns the array to the calling program:

      for(int i = 0; i < arg.length(); i++){

      if(arg.charAt(i) == ','){
      list[count] = sub;
      sub = " ";
      count++;
      }else{
      sub += arg.charAt(i);
      }
      }

      list[count] = sub;

      return list;

Related Searches

References

Comments

Related Ads

Featured
View Mobile Site