Complete the following method that takes a string as a param
Complete the following method that takes a string as a parameter and converts every number in the string to a ’*’. For example, ”It costs $100” is converted to ”It costs $***” public static String Convert(String s) {
Solution
//NumtoStar.java
import java.util.Scanner;
public class NumtoStar
{
public static String Convert(String s)
{
StringBuilder result = new StringBuilder(s);
String output = result.toString();
for (int i=0; i < s.length() ;i++ )
{
char c = s.charAt(i);
if (Character.isDigit(c))
{
result.setCharAt(i,\'*\');
}
else
{
result.setCharAt(i,c);
}
}
output = result.toString();
return output;
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println(\"Enter String: \");
String str = sc.nextLine();
System.out.println(\"Result: \" + Convert(str));
}
}
/*
output:
Enter String: Can you give me 104$. urgently
Result: Can you give me ***$. urgently
*/
