Write a java program to validate a string

Write a java program to validate a string. Write a program to read two strings and check whether or not they have dashes in the same places. Print “Yes” if the condition satisfies, else print “No”.

The return type (Integer) should return 1 if all dashes are placed correctly, else return 2.

Note: The strings must have exactly the same number of dashes in exactly the same positions. The strings might be of different length.

Input and Output Format:

Input consists of two strings.

Output consists of a string (“Yes” or “No”).

Refer sample output for formatting specifications.

Sample Input 1:

hi—there-you.

12–(134)-7539

Sample Output 1:

Yes

Sample Input 2:

-15-389

-xyw-zzy

Sample Output 2:

No

java program to validate a string

package com.demo;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
getvalues(s1, s2);
}
public static void getvalues(String s1, String s2) {
ArrayList<Integer> l1 = new ArrayList<Integer>();
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) == '-') {
l1.add(i);
}
}
ArrayList<Integer> l2 = new ArrayList<Integer>();
for (int i = 0; i < s2.length(); i++) {
if (s2.charAt(i) == '-') {
l2.add(i);
}
}
if (l1.equals(l2)) {
System.out.println("Yes");
} else
System.out.println("No");
}
}

 

Add a Comment

Your email address will not be published. Required fields are marked *

3 + seven =