Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

In what ways do we get out of if/else condition? java

I did this code:

private static void fix (List<String> hg){

        String k1 = hg.get(0);
        String k2 = hg.get(1);
        String k3 = hg.get(2);
        String k4 = hg.get(3);
        String k5 = hg.get(4);

        List <Character> d = new ArrayList<>();

            for (int j = 0; j < k1.length(); j++) {
                d.add(j, k1.charAt(j));
            }

Iterator <Character> sdf = d.iterator();
while (sdf.hasNext()){
  Character r = sdf.next();
  if (r.equals('g')){
    hg.add(k1);
  }
  if (r.equals('d')){
    hg.remove(k1);
  }
  if (r.equals('g') || r.equals('d')){ }// how to get out of this "if" beyond what I did?

}

The main idea is to ignore words containing letters ‘g’ and ‘d’.

I know that we can use such methods like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  1. if (r.equals('g') || r.equals('d')){ continue; }
  2. if (r.equals('g') || r.equals('d')){ break; }
  3. if (r.equals('g') || r.equals('d')){ System.out.println(); }

3 of them are useless, cause idea tells that operator continue is unnecessary as the last statement in a loop. Operator break is not appropriate, cause it literally breaks the code and it won’t work correctly.
System.out.println() fits well, but it makes a huge space in the console.

>Solution :

you should be use else if

Iterator<Character> sdf = d.iterator();
while (sdf.hasNext()) {
    Character r = sdf.next();
    if (r.equals('л')) {
        hg.add(k1);
    } else if (r.equals('р')) {
        hg.remove(k1);
    } else if (r.equals('g') || r.equals('d')) {
       continue;
    }
   
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading