0

I am beginner in java, and I have this question :

**Q01 [ 7 marks] Write a java program that take string as input and using the method EvenPairs(str) to check whether there's even pair exists or not for each character (i.e. Alphabet). Sample Test Cases

Input:"3gy41d21y363"

Output:

  • 3 – False
  • g – false
  • y – true
  • 4 – false
  • 1 – true
  • d – false

as you seen in the output, each character occurrence printed only one time even if it is duplicated, I solved the problem until this step I can not find a solution to print the character only 1 time with the result if it is true or false

this is my code:

    package evenpairornot;

import java.util.Scanner;
public class EvenPairOrNot {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {

    System.out.print("Enter a string: ");
    String s1=input.nextLine();

    EvenPairs(s1);
}

public static void EvenPairs(String s){

    char [] chs=s.toCharArray();
    int count=0;

    for (int i = 0; i <chs.length; i++) {
        for (int j = 0; j <chs.length; j++) {
            if (chs[i]==chs[j]){
                count++; 
            }    
        } 

        if(count%2==0)
            System.out.println(s.charAt(i)+"- true");
            else
            System.out.println(s.charAt(i)+"- False");

        count=0; 
    }

}

}

and this is the output:

Enter a string: 3gy41d21y363

3- False

g- False

y- true

4- False

1- true

d- False

2- False

1- true

y- true

3- False

6- False

3- False

waiting for your help!! thank you

  • See [How to count frequency of characters in a string?](https://stackoverflow.com/q/6712587/5221149) – Andreas Oct 05 '18 at 23:26
  • Shouldn't the `if (count % 2)` part be outside the for loop? – John3136 Oct 05 '18 at 23:26
  • then how it will access the s.charAt(i)? I think it needs a loop, if you can show me the solution without the loop I will be so thankful – Zahraa Maher Oct 05 '18 at 23:34
  • @Andreas most of the solution here are using somethings I should not use as objects... etc, while I am studying now the basics of java so I can't use them – Zahraa Maher Oct 05 '18 at 23:37
  • Why not store the characters you already printed out some where and check that list before you print? or even better you can look through all the characters before to make sure the current one is not a duplicate. – Charles Oct 05 '18 at 23:43
  • @Charles How? can you show me the code? – Zahraa Maher Oct 05 '18 at 23:51

1 Answers1

0

Here is the code. Basically after all the characters are counted. I look backward to make sure it wasn't a duplicate before printing. There is a lot of room for optimization here like doing the check before you count or instead of counting all characters you can just count the ones after i.

public static void EvenPairs(String s) {

    char[] chs = s.toCharArray();
    int count = 0;

    for (int i = 0; i < chs.length; i++) {
        for (int j = 0; j < chs.length; j++) {
            if (chs[i] == chs[j]) {
                count++;
            }
        }

        boolean shouldPrint = true;  
        for (int k = i - 1; k >= 0; k--) {  //loop though each character before the current one to check if it was already printed. 
            if (chs[k] == chs[i]) {         //if we it was already printed don't print.
                shouldPrint = false;
                break;
            }
        }

        if (shouldPrint) {
            if (count % 2 == 0)
                System.out.println(s.charAt(i) + "- true");
            else
                System.out.println(s.charAt(i) + "- False");
        }

        count = 0;
    }

}
Charles
  • 944
  • 5
  • 9