D
D
Dmitry Mayorov2020-12-11 22:34:14
Java
Dmitry Mayorov, 2020-12-11 22:34:14

Why don't two identical strings converge?

I don't understand why two absolutely identical strings don't converge when checking "Login" (In profile.java).

Yes, the question is childish, but still.

Console.java - main class

package com.app.console;
import java.util.Scanner;
public class Console{
   
   public static void main(String[] args) throws InterruptedException {
      boolean g = true;
      boolean r = false;
      profile prof = new profile();
      Reg reg = new Reg();
      GUI GUI = new GUI();
      Scanner input = new Scanner(System.in);
      while(g == true){

         if(r == false){
            GUI.menu_anon();
            int ar = input.nextInt();
            if(ar == 1){
            GUI.loading();
            Thread.sleep(2000);
            reg.regist();
            r = true;
            if(ar == 2)g = false;
         }
         }
         
         

         
         if(r == true){
            GUI.menu();
            int aa = input.nextInt();
            if(aa == 1){
               prof.entrance();
            }
            if(aa == 2)g = false;
         }
      }
   }
}


GUI.java - graphic component
package com.app.console;


public class GUI {
   public static void menu_anon(){
      System.out.println("|=====?MENU?=====|");
      System.out.println("|1. Registration |");
      System.out.println("|2. Exit         |");
      System.out.println("|================|");
      System.out.println("");
   }
   public static void menu(){
      System.out.println("|=====?MENU?=====|");
      System.out.println("|1. Profile      |");
      System.out.println("|2. Exit         |");
      System.out.println("|================|");
      System.out.println("");
   }
   public static void loading() throws InterruptedException {
      System.out.print("Loading: <");
      for(int i = 0; i < 21; i++){
         Thread.sleep(100);
         if(i != 20){
            System.out.print('-');
         }
         else{
            System.out.println('>');
            System.out.println("");
         }
      }
   }
}


profile.java - person's profile
package com.app.console;
import java.util.Scanner;

public class profile extends data{

   public static void entrance(){
      Scanner input = new Scanner(System.in);
      System.out.println("/=====?ENTRANCE?=====/");
      System.out.print("|Login: ");
      String log = input.nextLine();
      System.out.print("|Passworld: ");
      int pass = input.nextInt();
      check(log, pass);
   }

   public static void check(String a, int b){
      //System.out.println('|'+a+'|'+'|'+login+'|'+'|'+b+'|'+'|'+passworld+'|');
      if(a != login){
         System.out.println("!ERROR login!");
         System.out.println("");
      }
      if(b != passworld){
         System.out.println("!ERROR passwolrd!");
         System.out.println("");
      }
      if(a == login){
         if(b == passworld){
            System.out.println("s");
         }
      }
      
   }

}

Reg.java - registration.
package com.app.console;
import java.util.Scanner;

public class Reg extends data{
   public static void regist(){
      Scanner input = new Scanner(System.in);
      System.out.println("|=====?REGISTRATION?=====|");
      System.out.print("|Name: ");
      name = input.nextLine();
      System.out.print("|Login: ");
      login = input.nextLine();
      System.out.print("|Age: ");
      age = input.nextInt();
      System.out.print("|Passworld: ");
      passworld = input.nextInt();
      System.out.println("|========================|");
      System.out.println("");
   }
}


data.java - database
package com.app.console;

public class data {
   public static String name, login;
   public static int age, passworld;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2020-12-11
@DmitryDDS

Read about the difference when comparing using the operator ==, as well as using the method equals()
Strings do not converge, since you are comparing references to an object, and they clearly point to different objects. For content comparison use equals()
Here is your code:

public static void check(String a, int b){
      if(!a.equals((login)){
         System.out.println("!ERROR login!");
         System.out.println("");
      }
      if(!b.equals(passworld)){
         System.out.println("!ERROR passwolrd!");
         System.out.println("");
      }
      if(a.equals(login)){
         if(b.equals(passworld)){
            System.out.println("s");
         }
      }
      
   }

Further I recommend to read about Java naming convention.
Class names should be capitalized.
public class Data {
   public static String name, login;
   public static int age, passworld;
}

Further, the meaning of this is not entirely clear: public class Reg extends data
Why do you extend the pojo Data class with a certain Reg service class.
Most likely you need to create an instance of the Data class. And in this case, the class fields do not need to be made static.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question