Answer the question
In order to leave comments, you need to log in
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;
}
}
}
}
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("");
}
}
}
}
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");
}
}
}
}
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("");
}
}
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
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");
}
}
}
public class Data {
public static String name, login;
public static int age, passworld;
}
public class Reg extends data
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question