Advertisement
shawon_majid

StringTokenizer

Jul 19th, 2022
805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.76 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.shawon.strings;
  7.  
  8. import java.util.StringTokenizer;
  9.  
  10. /**
  11.  *
  12.  * @author shawonmajid
  13.  */
  14. public class StringTest {
  15.     public static void main(String[] args) {
  16.         String str = "12 315 1251 232 2525 end";
  17.        
  18.         StringTokenizer tok = new StringTokenizer(str);
  19.            
  20.         int ans = 0;
  21.         while(tok.hasMoreTokens()){
  22.             String cur = tok.nextToken();
  23.             if(!cur.equals("end")){
  24.                 ans += Integer.parseInt(cur);
  25.             }
  26.         }
  27.        
  28.         System.out.println(ans);
  29.        
  30.     }
  31. }
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement