[Java] How to use StringBurrer and Arrays.toString.

less than 1 minute read

How to use #StringBuffer and Arrays.toString. A program that puts numbers in a StringBuffer, separates them with commas, adds the numbers, displays the numbers, and outputs the numbers obtained in the process as an array display. (There is no particular meaning)

import java.util.*;

public class Main {
     public static void main(String[] args) {
         StringBuffer sb = new StringBuffer();
         for(int i = 0; i <10; i++) {
             sb.append(i+1).append(",");
         }
         String s = sb.toString();
         String[] a = s.split(",");
         int sum = 0;
         for(String i :a){
             if(Integer.parseInt(i) != a.length) {
                 System.out.print(i + "+ ");
             }
             else {
                 System.out.print(i);
             }
             sum += Integer.parseInt(i);
         }
         System.out.println(" = "+ sum);
         System.out.println(Arrays.toString(a));

     }
}

Tags:

Updated: