Finding Float in a String

PROBLEM
Find the float in the given string and print each float value as output in new line.
INPUT
"345.6 larger than 425.3"
OUTPUT
349.6
425.3

INPUT
"this is 34 and there 478.23"
OUTPUT
478.23

JAVA Solution

import java.util.*;
public class Hello {
    public static void main(String[] args) {
        Scanner s = new Scanner (System.in);
        String str = s.nextLine();
        String rep=str.replaceAll("[^.0-9]"," ");
        String []s1=rep.split(" "); 
        System.out.println(rep);
        for(int i=0;i<s1.length;i++)
        {
            if(s1[i].contains("."))
            System.out.println(s1[i]);
        }
    }
}

If you anyone come up with better solution leave it in comment.
Keep Programming...!!!

Comments

Popular posts from this blog

Balanced Binary Tree

First Unique Character in a String

Majority Element

Smallest Range II