0

I do have some piece of code, I find unfathomable to comprehend. I save a longer string in a variable and then try to find a specific piece of string inside of it. The clue is that I can output the various substrings, but it will not find the specific string and stop the process...

The code goes like this:

import java.io.*;
import java.util.*;

public class Readin {

    public static void main(String[] args) {

        String specChar = "DISD";
        String gen = "MLRVFILYAENVHTPDTDISDAYCSAVFAGVKKRTKVIKNSVNP";

        for (int i = 0; i < gen.length() - 3; i++) {
            char char1 = gen.charAt(i);
            char char2 = gen.charAt(i + 1);
            char char3 = gen.charAt(i + 2);
            char char4 = gen.charAt(i + 3);

            String concatChar = new StringBuilder().append(char1).append(char2).append(char3).append(char4).toString();
            System.out.println(concatChar);

            if (specChar == concatChar) {
                System.out.println(specChar);
                break;
            } else {
                System.out.println("Error");
            }
        }
    }
}
Juan Carlos Mendoza
  • 5,736
  • 7
  • 25
  • 50
  • Have you tried to step through that code with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems)? – litelite Oct 11 '17 at 19:49
  • 1
    There is a substring method on String that will help you too. – cpp beginner Oct 11 '17 at 19:50

2 Answers2

0

Short answer: you need to replace (specChar == concatChar) by (Objects.equals(specChar, concatChar)) or (specChar.equals(concatChar)) and you'll come to the break operator

Detailed explanation you can find in articles how to compare strings in Java

Alex Saunin
  • 976
  • 1
  • 8
  • 15
0

After looking at your code, I thought your if should not be for string comparison rather it should be character comparison and condition should be like below in below code. Also I rewrite your code for a actual substring comparison in java as well. :-)

    import java.io.*;
    import java.util.*;

    public class Readin {

        public static void main(String[] args) {

            String specChar = "DISD";

            String gen = "MLRVFILYAENVHTPDTDISDAYCSAVFAGVKKRTKVIKNSVNP";

            for (int i = 0; i < gen.length() - 3; i++) {
                char char1 = gen.charAt(i);
                char char2 = gen.charAt(i + 1);
                char char3 = gen.charAt(i + 2);
                char char4 = gen.charAt(i + 3);

                String concatChar = new StringBuilder().append(char1).append(char2).append(char3).append(char4).toString();
                System.out.println(concatChar);
                //if (specChar == concatChar) {
               //compare each character from 1st to 4th
                if (char1 == specChar.charAt(0) && char2 == specChar.charAt(1) && char3 == specChar.charAt(2) && char4 == specChar.charAt(3)) {
                    System.out.println(specChar);
                    break;
                } else {
                    System.out.println("Error");
                }
            }
        }
    }

Now see the modified one below:-

    public class Readin {

        public static void main(String[] args) {
            String specChar = "DISD";
            String gen = "MLRVFILYAENVHTPDTDISDAYCSAVFAGVKKRTKVIKNSVNP";

            if (gen.contains(specChar)) {
                System.out.println(specChar);
            } else {
                System.out.println("Error");
            }

        }
    }
Abhijit Pritam Dutta
  • 5,521
  • 2
  • 11
  • 17