[JAVA] Character string comparison: I was caught in the skill check problem of Paiza

I was caught in the skill check problem "character string comparison" (* problem of code disclosure OK) published in Paiza, so I summarized it.

The content of the problem is "Compare the two entered strings"

Reason for getting caught

I knew that there was a string comparison function, but the video I referred to didn't show how to do it, so I had a preconceived notion that I wouldn't use it.

Incorrect code



import java.util.*;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String line1 = sc.nextLine();
        String line2 = sc.nextLine();
              
        if( line1 == line2 ){
           System.out.println("OK"); 
        }
        else{
           System.out.println("NG"); 
        }
        
    }
}

Even if the output result is the same character string, "NG"

Enter the code below to confirm

Try debugging



import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String line1 = sc.nextLine();
        String line2 = sc.nextLine();
        
        //Check if statement
        String line1 = "moji";
        String line2 = "moji";

        //Confirmation of input
        System.out.println(line1);
        System.out.println(line2);
        
        if( line1 == line2 ){
           System.out.println("OK"); 
        }
        else{
           System.out.println("NG"); 
        }
        
    }
}
Output result;
moji
moji
ok

There is no problem with getting the if statement and character input. After all, I think that the character string comparison is bad (condition of if statement) and confirm it, and arrive at the following site. [Introduction to Java] How to compare strings (String) ("==" and "equals") Regarding the equality comparison of String type in Java

Correct code



import java.util.*;


public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String line1 = sc.nextLine();
        String line2 = sc.nextLine();
        
        if( line1.equals(line2)){
           System.out.println("OK"); 
        }
        else{
           System.out.println("NG"); 
        }
        
    }
}

Recommended Posts

Character string comparison: I was caught in the skill check problem of Paiza
The story of low-level string comparison in Java
I read the source of String
[Ruby] I want to output only the odd-numbered characters in the character string
[Java] Comparison of String type character strings
[Java] Check if the character string is composed only of blanks (= Blank)
[Delete the first letter of the character string] Ruby
I understood the very basics of character input
The part I was addicted to in "Introduction to Ajax in Java Web Applications" of NetBeans
# 1_JAVA I want to get the index number by specifying one character in the character string.
I was addicted to the NoSuchMethodError in Cloud Endpoints
Count the number of occurrences of a string in Ruby
I was addicted to the record of the associated model
About the problem of deadlock in parallel processing in gem'sprockets' 4.0
Was done in the base year of the Java calendar week
Check the dependency of a specific maven artifact in Coursier
Put the file in the properties of string in spring xml configuration
Solved the problem that all the data in the table was displayed
I was addicted to the setting of laradock + VSCode + xdebug
I was confused because there was a split in the Array
I compared the build times of various Dockerfiles in Rust
I want to know the Method of the Controller where the Exception was thrown in the ExceptionHandler of Spring Boot