DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

MVI interview Questions - 2

Question -2

Get the 10 input from the user and Put the map
key-Long-> number should even and also each digit should be even
value-String -> string should be in palindrome And this length should not greater than 6.
Then only add into this map, Then write them into the "file.txt"
In a comma sepretaion values.

public class Two {
    public static <K> void main(String[] args) throws Exception {
        BufferedWriter br = new BufferedWriter(new FileWriter(new File("/home/vignesh/Desktop/data.txt")));
        Map<Long, String> map = new HashMap<>();
        Scanner sc = new Scanner(System.in);
        int c = 0;
        long key;
        String value;
        while (c <= 1) {
            System.out.println(" enter the key");
            key = sc.nextLong();
            System.out.println("enter the value");
            value = sc.next();
            if (num(key) && rev(value))
                map.put(key, value);
            c++;
        }
        int c1 = 1;
        for (Entry<Long, String> m : map.entrySet()) {
            br.write(m.getKey() + "=");
            br.write(m.getValue() + "");
            if (c1 < map.size() - 1) {
                br.write(",");
            } else {
                br.write(".");
            }
            br.write("\n");
            c1++;
        }
        br.flush();
        br.close();
        System.out.println(map + "" + map.size());
    }

    public static boolean num(long key) {
        if (key % 2 != 0)
            return false;
        while (key > 0) {
            if ((key % 10) % 2 != 0)
                return false;
            key /= 10;
        }
        return true;
    }

    public static boolean rev(String value) {
        String s = "";
        if (value.length() <= 6) {
            for (int i = value.length() - 1; i >= 0; i--) {
                s += value.charAt(i);
            }
            return s.equals(value) ? true : false;
        } else {
            return false;
        }
    }
}

Enter fullscreen mode Exit fullscreen mode
 output: 
enter the key
262
enter the value
adda
 enter the key
44
enter the value
sdds
{262=adda, 44=sdds}2
// in the file this data succusfully writen.
Enter fullscreen mode Exit fullscreen mode

Top comments (0)