If the two names are same the program will error












1















Error
My Codes



I made a program that asks the fullname, address etc, i want the program to detect if two full names are same. i have already the if else statement that detect if the fullname is same but i keep getting error on the return part. i want to return in the name input process again but the program keep going to the address input i want.



example output



1.Your first name: Jerico



1.Your middle name: Manarang



1.Your last name: Navarro



input barangay....etc



2.Your first name: Jerico



2.Your middle name: Manarang



2.Your last name: Navarro



(error please try again)



2.Your first name:



public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("How many names do you like to enter? ");
int user = input.nextInt();
String DATA = new String[user][4][3];

for(int x=0;x<user;x++){
System.out.print("n" + (x+1) +".Enter your first name: ");
DATA[x][0][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your midle name: ");
DATA[x][0][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your last name: ");
DATA[x][0][2] = new Scanner(System.in).nextLine();
for(int y=0;y<user;y++){
if(x == y){
}
else if(DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])){
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
x-=1;
break;

}
}

System.out.print("n" +(x+1) +".Enter your barangay: ");
DATA[x][1][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your city: ");
DATA[x][1][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your province: ");
DATA[x][1][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your mailing address: ");
DATA[x][2][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your contact number: ");
DATA[x][2][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your email address: ");
DATA[x][2][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your elementary school: ");
DATA[x][3][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your secondary school: ");
DATA[x][3][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your tertiary: ");
DATA[x][3][2] = new Scanner(System.in).nextLine();


}
}


}










share|improve this question




















  • 4





    So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

    – GhostCat
    Nov 21 '18 at 9:41








  • 3





    Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

    – GhostCat
    Nov 21 '18 at 9:41













  • And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

    – GhostCat
    Nov 21 '18 at 9:42











  • Is DATA[y][y][0] a typo?

    – Mark Jeronimus
    Nov 21 '18 at 9:42











  • Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

    – Jerico M Navarro
    Nov 21 '18 at 9:45
















1















Error
My Codes



I made a program that asks the fullname, address etc, i want the program to detect if two full names are same. i have already the if else statement that detect if the fullname is same but i keep getting error on the return part. i want to return in the name input process again but the program keep going to the address input i want.



example output



1.Your first name: Jerico



1.Your middle name: Manarang



1.Your last name: Navarro



input barangay....etc



2.Your first name: Jerico



2.Your middle name: Manarang



2.Your last name: Navarro



(error please try again)



2.Your first name:



public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("How many names do you like to enter? ");
int user = input.nextInt();
String DATA = new String[user][4][3];

for(int x=0;x<user;x++){
System.out.print("n" + (x+1) +".Enter your first name: ");
DATA[x][0][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your midle name: ");
DATA[x][0][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your last name: ");
DATA[x][0][2] = new Scanner(System.in).nextLine();
for(int y=0;y<user;y++){
if(x == y){
}
else if(DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])){
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
x-=1;
break;

}
}

System.out.print("n" +(x+1) +".Enter your barangay: ");
DATA[x][1][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your city: ");
DATA[x][1][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your province: ");
DATA[x][1][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your mailing address: ");
DATA[x][2][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your contact number: ");
DATA[x][2][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your email address: ");
DATA[x][2][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your elementary school: ");
DATA[x][3][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your secondary school: ");
DATA[x][3][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your tertiary: ");
DATA[x][3][2] = new Scanner(System.in).nextLine();


}
}


}










share|improve this question




















  • 4





    So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

    – GhostCat
    Nov 21 '18 at 9:41








  • 3





    Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

    – GhostCat
    Nov 21 '18 at 9:41













  • And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

    – GhostCat
    Nov 21 '18 at 9:42











  • Is DATA[y][y][0] a typo?

    – Mark Jeronimus
    Nov 21 '18 at 9:42











  • Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

    – Jerico M Navarro
    Nov 21 '18 at 9:45














1












1








1








Error
My Codes



I made a program that asks the fullname, address etc, i want the program to detect if two full names are same. i have already the if else statement that detect if the fullname is same but i keep getting error on the return part. i want to return in the name input process again but the program keep going to the address input i want.



example output



1.Your first name: Jerico



1.Your middle name: Manarang



1.Your last name: Navarro



input barangay....etc



2.Your first name: Jerico



2.Your middle name: Manarang



2.Your last name: Navarro



(error please try again)



2.Your first name:



public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("How many names do you like to enter? ");
int user = input.nextInt();
String DATA = new String[user][4][3];

for(int x=0;x<user;x++){
System.out.print("n" + (x+1) +".Enter your first name: ");
DATA[x][0][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your midle name: ");
DATA[x][0][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your last name: ");
DATA[x][0][2] = new Scanner(System.in).nextLine();
for(int y=0;y<user;y++){
if(x == y){
}
else if(DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])){
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
x-=1;
break;

}
}

System.out.print("n" +(x+1) +".Enter your barangay: ");
DATA[x][1][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your city: ");
DATA[x][1][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your province: ");
DATA[x][1][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your mailing address: ");
DATA[x][2][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your contact number: ");
DATA[x][2][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your email address: ");
DATA[x][2][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your elementary school: ");
DATA[x][3][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your secondary school: ");
DATA[x][3][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your tertiary: ");
DATA[x][3][2] = new Scanner(System.in).nextLine();


}
}


}










share|improve this question
















Error
My Codes



I made a program that asks the fullname, address etc, i want the program to detect if two full names are same. i have already the if else statement that detect if the fullname is same but i keep getting error on the return part. i want to return in the name input process again but the program keep going to the address input i want.



example output



1.Your first name: Jerico



1.Your middle name: Manarang



1.Your last name: Navarro



input barangay....etc



2.Your first name: Jerico



2.Your middle name: Manarang



2.Your last name: Navarro



(error please try again)



2.Your first name:



public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("How many names do you like to enter? ");
int user = input.nextInt();
String DATA = new String[user][4][3];

for(int x=0;x<user;x++){
System.out.print("n" + (x+1) +".Enter your first name: ");
DATA[x][0][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your midle name: ");
DATA[x][0][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your last name: ");
DATA[x][0][2] = new Scanner(System.in).nextLine();
for(int y=0;y<user;y++){
if(x == y){
}
else if(DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])){
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
x-=1;
break;

}
}

System.out.print("n" +(x+1) +".Enter your barangay: ");
DATA[x][1][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your city: ");
DATA[x][1][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your province: ");
DATA[x][1][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your mailing address: ");
DATA[x][2][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your contact number: ");
DATA[x][2][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your email address: ");
DATA[x][2][2] = new Scanner(System.in).nextLine();
System.out.print("n" +(x+1) +".Enter your elementary school: ");
DATA[x][3][0] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your secondary school: ");
DATA[x][3][1] = new Scanner(System.in).nextLine();
System.out.print((x+1) +".Enter your tertiary: ");
DATA[x][3][2] = new Scanner(System.in).nextLine();


}
}


}







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 9:38









GhostCat

91k1688147




91k1688147










asked Nov 21 '18 at 9:32









Jerico M NavarroJerico M Navarro

23




23








  • 4





    So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

    – GhostCat
    Nov 21 '18 at 9:41








  • 3





    Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

    – GhostCat
    Nov 21 '18 at 9:41













  • And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

    – GhostCat
    Nov 21 '18 at 9:42











  • Is DATA[y][y][0] a typo?

    – Mark Jeronimus
    Nov 21 '18 at 9:42











  • Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

    – Jerico M Navarro
    Nov 21 '18 at 9:45














  • 4





    So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

    – GhostCat
    Nov 21 '18 at 9:41








  • 3





    Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

    – GhostCat
    Nov 21 '18 at 9:41













  • And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

    – GhostCat
    Nov 21 '18 at 9:42











  • Is DATA[y][y][0] a typo?

    – Mark Jeronimus
    Nov 21 '18 at 9:42











  • Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

    – Jerico M Navarro
    Nov 21 '18 at 9:45








4




4





So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

– GhostCat
Nov 21 '18 at 9:41







So many basic things bad here. First: naming ... variable names go camelCase in java. DATA would be some constant. That is also a name that says nothing. Rather call it userData. Then: your loop counters should be named i, j ... x and y imply (by convention) that they are float/double values. i / j are loop counters. Go with that. Then: create a User class that has these properties as fields. Using a TABLE, and have each row represent a user is the opposite of what you do in OOP. And then instead of having a multi dim table of rows and columns, simply create an array User users. Done.

– GhostCat
Nov 21 '18 at 9:41






3




3





Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

– GhostCat
Nov 21 '18 at 9:41







Then read Minimal, Complete, and Verifiable example and explain to us what the error looks like. "I get an error" is nothing we can help with.

– GhostCat
Nov 21 '18 at 9:41















And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

– GhostCat
Nov 21 '18 at 9:42





And finally: only use relevant tags. It doesnt matter at all to your question that you used netbeans to edit your source code. You have a java problem, plain and simple.

– GhostCat
Nov 21 '18 at 9:42













Is DATA[y][y][0] a typo?

– Mark Jeronimus
Nov 21 '18 at 9:42





Is DATA[y][y][0] a typo?

– Mark Jeronimus
Nov 21 '18 at 9:42













Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

– Jerico M Navarro
Nov 21 '18 at 9:45





Sir, I only know the basics this code is our activity in our school. we are only allowed to use the basics like if statement, for loop, an array

– Jerico M Navarro
Nov 21 '18 at 9:45












2 Answers
2






active

oldest

votes


















1














This code has lot of issues. And this is not good way at all to do this. But since you are new to this and still playing around with java, for now you can do a change like this to your code and get it work.



import java.util.Scanner;

public class Main {

static boolean flag;

public static void main(String args) {
Scanner input = new Scanner(System.in);
System.out.print("How many names do you like to enter? ");
int user = input.nextInt();
String DATA = new String[user][4][3];

for (int x = 0; x < user; x++) {
flag = false;
System.out.print("n" + (x + 1) + ".Enter your first name: ");
DATA[x][0][0] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your midle name: ");
DATA[x][0][1] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your last name: ");
DATA[x][0][2] = new Scanner(System.in).nextLine();
for (int y = 0; y < user; y++) {
if (x == y) {
} else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
x -= 1;
flag = true;
break;
}
}

if (flag){
continue;
}

System.out.print("n" + (x + 1) + ".Enter your barangay: ");
DATA[x][1][0] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your city: ");
DATA[x][1][1] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your province: ");
DATA[x][1][2] = new Scanner(System.in).nextLine();
System.out.print("n" + (x + 1) + ".Enter your mailing address: ");
DATA[x][2][0] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your contact number: ");
DATA[x][2][1] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your email address: ");
DATA[x][2][2] = new Scanner(System.in).nextLine();
System.out.print("n" + (x + 1) + ".Enter your elementary school: ");
DATA[x][3][0] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your secondary school: ");
DATA[x][3][1] = new Scanner(System.in).nextLine();
System.out.print((x + 1) + ".Enter your tertiary: ");
DATA[x][3][2] = new Scanner(System.in).nextLine();


}
}
}


Again its just a little hack to work this code. Its not the proper way. You have to refactor the whole code to get it done properly. :)






share|improve this answer
























  • Thanks a lot sir

    – Jerico M Navarro
    Nov 21 '18 at 10:13











  • You're welcome @JericoMNavarro

    – Sand
    Nov 21 '18 at 10:17











  • @JericoMNavarro Then dont forget to accept the answer, too

    – GhostCat
    Nov 21 '18 at 10:21



















1














Agree with Sand. To me, it is better to give alternative approach, because reading your solution is really hard. This is my offer. I think it could be more clear to you. P.S. Within more that 15 years of developments, I faced with couple times with arrays more than 2D. Do use class instead of 3D... arrays.



private static final class User {

private final String firstName;
private final String middleName;
private final String lastName;

private String barangay;
private String city;
private String province;
private String mailingAddress;
private String contactNumber;
private String email;
private String elementarySchool;
private String secondarySchool;
private String tertiary;

public User(String firstName, String middleName, String lastName) {
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof User))
return false;
User user = (User)obj;
return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
&& Objects.equals(lastName, user.lastName);
}

@Override
public int hashCode() {
return Objects.hash(firstName, middleName, lastName);
}
}

private static User getUser(Scanner scan, int i) {
System.out.print("n" + i + ". Enter your first name: ");
String firstName = scan.next();

System.out.print(i + ". Enter your middle name: ");
String middleName = scan.next();

System.out.print(i + ". Enter your last name: ");
String lastName = scan.next();

return new User(firstName, middleName, lastName);
}

private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
System.out.print("n" + i + ". Enter your barangay: ");
user.barangay = scan.nextLine();

System.out.print(i + ". Enter your city: ");
user.city = scan.nextLine();

System.out.print(i + ". Enter your province: ");
user.province = scan.nextLine();

System.out.print("n" + i + ". Enter your mailing address: ");
user.mailingAddress = scan.nextLine();

System.out.print(i + ". Enter your contact number: ");
user.contactNumber = scan.nextLine();

System.out.print(i + ". Enter your email address: ");
user.email = scan.nextLine();

System.out.print("n" + i + ". Enter your elementary school: ");
user.elementarySchool = scan.nextLine();

System.out.print(i + ". Enter your secondary school: ");
user.secondarySchool = scan.nextLine();

System.out.print((i + 1) + ".Enter your tertiary: ");
user.tertiary = scan.nextLine();

return user;
}

private static Set<User> getUsers() {
try (Scanner scan = new Scanner(System.in)) {
System.out.print("How many names do you like to enter? ");
Set<User> users = new LinkedHashSet<>();

for (int i = 1, total = scan.nextInt(); i <= total; ) {
User user = getUser(scan, i);

if (users.contains(user)) {
System.out.println("Ops! Your name is already inputed.");
System.out.println("Please Try again.");
} else {
users.add(getUserAdditionalInfo(user, scan, i));
i++;
}
}

return Collections.unmodifiableSet(users);
}
}

public static void main(String args) {
Set<User> users = getUsers();
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408969%2fif-the-two-names-are-same-the-program-will-error%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This code has lot of issues. And this is not good way at all to do this. But since you are new to this and still playing around with java, for now you can do a change like this to your code and get it work.



    import java.util.Scanner;

    public class Main {

    static boolean flag;

    public static void main(String args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many names do you like to enter? ");
    int user = input.nextInt();
    String DATA = new String[user][4][3];

    for (int x = 0; x < user; x++) {
    flag = false;
    System.out.print("n" + (x + 1) + ".Enter your first name: ");
    DATA[x][0][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your midle name: ");
    DATA[x][0][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your last name: ");
    DATA[x][0][2] = new Scanner(System.in).nextLine();
    for (int y = 0; y < user; y++) {
    if (x == y) {
    } else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
    System.out.println("Ops! Your name is already inputed.");
    System.out.println("Please Try again.");
    x -= 1;
    flag = true;
    break;
    }
    }

    if (flag){
    continue;
    }

    System.out.print("n" + (x + 1) + ".Enter your barangay: ");
    DATA[x][1][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your city: ");
    DATA[x][1][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your province: ");
    DATA[x][1][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your mailing address: ");
    DATA[x][2][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your contact number: ");
    DATA[x][2][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your email address: ");
    DATA[x][2][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your elementary school: ");
    DATA[x][3][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your secondary school: ");
    DATA[x][3][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your tertiary: ");
    DATA[x][3][2] = new Scanner(System.in).nextLine();


    }
    }
    }


    Again its just a little hack to work this code. Its not the proper way. You have to refactor the whole code to get it done properly. :)






    share|improve this answer
























    • Thanks a lot sir

      – Jerico M Navarro
      Nov 21 '18 at 10:13











    • You're welcome @JericoMNavarro

      – Sand
      Nov 21 '18 at 10:17











    • @JericoMNavarro Then dont forget to accept the answer, too

      – GhostCat
      Nov 21 '18 at 10:21
















    1














    This code has lot of issues. And this is not good way at all to do this. But since you are new to this and still playing around with java, for now you can do a change like this to your code and get it work.



    import java.util.Scanner;

    public class Main {

    static boolean flag;

    public static void main(String args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many names do you like to enter? ");
    int user = input.nextInt();
    String DATA = new String[user][4][3];

    for (int x = 0; x < user; x++) {
    flag = false;
    System.out.print("n" + (x + 1) + ".Enter your first name: ");
    DATA[x][0][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your midle name: ");
    DATA[x][0][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your last name: ");
    DATA[x][0][2] = new Scanner(System.in).nextLine();
    for (int y = 0; y < user; y++) {
    if (x == y) {
    } else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
    System.out.println("Ops! Your name is already inputed.");
    System.out.println("Please Try again.");
    x -= 1;
    flag = true;
    break;
    }
    }

    if (flag){
    continue;
    }

    System.out.print("n" + (x + 1) + ".Enter your barangay: ");
    DATA[x][1][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your city: ");
    DATA[x][1][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your province: ");
    DATA[x][1][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your mailing address: ");
    DATA[x][2][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your contact number: ");
    DATA[x][2][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your email address: ");
    DATA[x][2][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your elementary school: ");
    DATA[x][3][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your secondary school: ");
    DATA[x][3][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your tertiary: ");
    DATA[x][3][2] = new Scanner(System.in).nextLine();


    }
    }
    }


    Again its just a little hack to work this code. Its not the proper way. You have to refactor the whole code to get it done properly. :)






    share|improve this answer
























    • Thanks a lot sir

      – Jerico M Navarro
      Nov 21 '18 at 10:13











    • You're welcome @JericoMNavarro

      – Sand
      Nov 21 '18 at 10:17











    • @JericoMNavarro Then dont forget to accept the answer, too

      – GhostCat
      Nov 21 '18 at 10:21














    1












    1








    1







    This code has lot of issues. And this is not good way at all to do this. But since you are new to this and still playing around with java, for now you can do a change like this to your code and get it work.



    import java.util.Scanner;

    public class Main {

    static boolean flag;

    public static void main(String args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many names do you like to enter? ");
    int user = input.nextInt();
    String DATA = new String[user][4][3];

    for (int x = 0; x < user; x++) {
    flag = false;
    System.out.print("n" + (x + 1) + ".Enter your first name: ");
    DATA[x][0][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your midle name: ");
    DATA[x][0][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your last name: ");
    DATA[x][0][2] = new Scanner(System.in).nextLine();
    for (int y = 0; y < user; y++) {
    if (x == y) {
    } else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
    System.out.println("Ops! Your name is already inputed.");
    System.out.println("Please Try again.");
    x -= 1;
    flag = true;
    break;
    }
    }

    if (flag){
    continue;
    }

    System.out.print("n" + (x + 1) + ".Enter your barangay: ");
    DATA[x][1][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your city: ");
    DATA[x][1][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your province: ");
    DATA[x][1][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your mailing address: ");
    DATA[x][2][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your contact number: ");
    DATA[x][2][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your email address: ");
    DATA[x][2][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your elementary school: ");
    DATA[x][3][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your secondary school: ");
    DATA[x][3][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your tertiary: ");
    DATA[x][3][2] = new Scanner(System.in).nextLine();


    }
    }
    }


    Again its just a little hack to work this code. Its not the proper way. You have to refactor the whole code to get it done properly. :)






    share|improve this answer













    This code has lot of issues. And this is not good way at all to do this. But since you are new to this and still playing around with java, for now you can do a change like this to your code and get it work.



    import java.util.Scanner;

    public class Main {

    static boolean flag;

    public static void main(String args) {
    Scanner input = new Scanner(System.in);
    System.out.print("How many names do you like to enter? ");
    int user = input.nextInt();
    String DATA = new String[user][4][3];

    for (int x = 0; x < user; x++) {
    flag = false;
    System.out.print("n" + (x + 1) + ".Enter your first name: ");
    DATA[x][0][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your midle name: ");
    DATA[x][0][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your last name: ");
    DATA[x][0][2] = new Scanner(System.in).nextLine();
    for (int y = 0; y < user; y++) {
    if (x == y) {
    } else if (DATA[x][0][0].equals(DATA[y][y][0]) && DATA[x][0][1].equals(DATA[y][y][1]) && DATA[x][0][2].equals(DATA[y][y][2])) {
    System.out.println("Ops! Your name is already inputed.");
    System.out.println("Please Try again.");
    x -= 1;
    flag = true;
    break;
    }
    }

    if (flag){
    continue;
    }

    System.out.print("n" + (x + 1) + ".Enter your barangay: ");
    DATA[x][1][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your city: ");
    DATA[x][1][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your province: ");
    DATA[x][1][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your mailing address: ");
    DATA[x][2][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your contact number: ");
    DATA[x][2][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your email address: ");
    DATA[x][2][2] = new Scanner(System.in).nextLine();
    System.out.print("n" + (x + 1) + ".Enter your elementary school: ");
    DATA[x][3][0] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your secondary school: ");
    DATA[x][3][1] = new Scanner(System.in).nextLine();
    System.out.print((x + 1) + ".Enter your tertiary: ");
    DATA[x][3][2] = new Scanner(System.in).nextLine();


    }
    }
    }


    Again its just a little hack to work this code. Its not the proper way. You have to refactor the whole code to get it done properly. :)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 21 '18 at 9:59









    SandSand

    1,6512521




    1,6512521













    • Thanks a lot sir

      – Jerico M Navarro
      Nov 21 '18 at 10:13











    • You're welcome @JericoMNavarro

      – Sand
      Nov 21 '18 at 10:17











    • @JericoMNavarro Then dont forget to accept the answer, too

      – GhostCat
      Nov 21 '18 at 10:21



















    • Thanks a lot sir

      – Jerico M Navarro
      Nov 21 '18 at 10:13











    • You're welcome @JericoMNavarro

      – Sand
      Nov 21 '18 at 10:17











    • @JericoMNavarro Then dont forget to accept the answer, too

      – GhostCat
      Nov 21 '18 at 10:21

















    Thanks a lot sir

    – Jerico M Navarro
    Nov 21 '18 at 10:13





    Thanks a lot sir

    – Jerico M Navarro
    Nov 21 '18 at 10:13













    You're welcome @JericoMNavarro

    – Sand
    Nov 21 '18 at 10:17





    You're welcome @JericoMNavarro

    – Sand
    Nov 21 '18 at 10:17













    @JericoMNavarro Then dont forget to accept the answer, too

    – GhostCat
    Nov 21 '18 at 10:21





    @JericoMNavarro Then dont forget to accept the answer, too

    – GhostCat
    Nov 21 '18 at 10:21













    1














    Agree with Sand. To me, it is better to give alternative approach, because reading your solution is really hard. This is my offer. I think it could be more clear to you. P.S. Within more that 15 years of developments, I faced with couple times with arrays more than 2D. Do use class instead of 3D... arrays.



    private static final class User {

    private final String firstName;
    private final String middleName;
    private final String lastName;

    private String barangay;
    private String city;
    private String province;
    private String mailingAddress;
    private String contactNumber;
    private String email;
    private String elementarySchool;
    private String secondarySchool;
    private String tertiary;

    public User(String firstName, String middleName, String lastName) {
    this.firstName = firstName;
    this.middleName = middleName;
    this.lastName = lastName;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if (!(obj instanceof User))
    return false;
    User user = (User)obj;
    return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
    && Objects.equals(lastName, user.lastName);
    }

    @Override
    public int hashCode() {
    return Objects.hash(firstName, middleName, lastName);
    }
    }

    private static User getUser(Scanner scan, int i) {
    System.out.print("n" + i + ". Enter your first name: ");
    String firstName = scan.next();

    System.out.print(i + ". Enter your middle name: ");
    String middleName = scan.next();

    System.out.print(i + ". Enter your last name: ");
    String lastName = scan.next();

    return new User(firstName, middleName, lastName);
    }

    private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
    System.out.print("n" + i + ". Enter your barangay: ");
    user.barangay = scan.nextLine();

    System.out.print(i + ". Enter your city: ");
    user.city = scan.nextLine();

    System.out.print(i + ". Enter your province: ");
    user.province = scan.nextLine();

    System.out.print("n" + i + ". Enter your mailing address: ");
    user.mailingAddress = scan.nextLine();

    System.out.print(i + ". Enter your contact number: ");
    user.contactNumber = scan.nextLine();

    System.out.print(i + ". Enter your email address: ");
    user.email = scan.nextLine();

    System.out.print("n" + i + ". Enter your elementary school: ");
    user.elementarySchool = scan.nextLine();

    System.out.print(i + ". Enter your secondary school: ");
    user.secondarySchool = scan.nextLine();

    System.out.print((i + 1) + ".Enter your tertiary: ");
    user.tertiary = scan.nextLine();

    return user;
    }

    private static Set<User> getUsers() {
    try (Scanner scan = new Scanner(System.in)) {
    System.out.print("How many names do you like to enter? ");
    Set<User> users = new LinkedHashSet<>();

    for (int i = 1, total = scan.nextInt(); i <= total; ) {
    User user = getUser(scan, i);

    if (users.contains(user)) {
    System.out.println("Ops! Your name is already inputed.");
    System.out.println("Please Try again.");
    } else {
    users.add(getUserAdditionalInfo(user, scan, i));
    i++;
    }
    }

    return Collections.unmodifiableSet(users);
    }
    }

    public static void main(String args) {
    Set<User> users = getUsers();
    }





    share|improve this answer




























      1














      Agree with Sand. To me, it is better to give alternative approach, because reading your solution is really hard. This is my offer. I think it could be more clear to you. P.S. Within more that 15 years of developments, I faced with couple times with arrays more than 2D. Do use class instead of 3D... arrays.



      private static final class User {

      private final String firstName;
      private final String middleName;
      private final String lastName;

      private String barangay;
      private String city;
      private String province;
      private String mailingAddress;
      private String contactNumber;
      private String email;
      private String elementarySchool;
      private String secondarySchool;
      private String tertiary;

      public User(String firstName, String middleName, String lastName) {
      this.firstName = firstName;
      this.middleName = middleName;
      this.lastName = lastName;
      }

      @Override
      public boolean equals(Object obj) {
      if (this == obj)
      return true;
      if (!(obj instanceof User))
      return false;
      User user = (User)obj;
      return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
      && Objects.equals(lastName, user.lastName);
      }

      @Override
      public int hashCode() {
      return Objects.hash(firstName, middleName, lastName);
      }
      }

      private static User getUser(Scanner scan, int i) {
      System.out.print("n" + i + ". Enter your first name: ");
      String firstName = scan.next();

      System.out.print(i + ". Enter your middle name: ");
      String middleName = scan.next();

      System.out.print(i + ". Enter your last name: ");
      String lastName = scan.next();

      return new User(firstName, middleName, lastName);
      }

      private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
      System.out.print("n" + i + ". Enter your barangay: ");
      user.barangay = scan.nextLine();

      System.out.print(i + ". Enter your city: ");
      user.city = scan.nextLine();

      System.out.print(i + ". Enter your province: ");
      user.province = scan.nextLine();

      System.out.print("n" + i + ". Enter your mailing address: ");
      user.mailingAddress = scan.nextLine();

      System.out.print(i + ". Enter your contact number: ");
      user.contactNumber = scan.nextLine();

      System.out.print(i + ". Enter your email address: ");
      user.email = scan.nextLine();

      System.out.print("n" + i + ". Enter your elementary school: ");
      user.elementarySchool = scan.nextLine();

      System.out.print(i + ". Enter your secondary school: ");
      user.secondarySchool = scan.nextLine();

      System.out.print((i + 1) + ".Enter your tertiary: ");
      user.tertiary = scan.nextLine();

      return user;
      }

      private static Set<User> getUsers() {
      try (Scanner scan = new Scanner(System.in)) {
      System.out.print("How many names do you like to enter? ");
      Set<User> users = new LinkedHashSet<>();

      for (int i = 1, total = scan.nextInt(); i <= total; ) {
      User user = getUser(scan, i);

      if (users.contains(user)) {
      System.out.println("Ops! Your name is already inputed.");
      System.out.println("Please Try again.");
      } else {
      users.add(getUserAdditionalInfo(user, scan, i));
      i++;
      }
      }

      return Collections.unmodifiableSet(users);
      }
      }

      public static void main(String args) {
      Set<User> users = getUsers();
      }





      share|improve this answer


























        1












        1








        1







        Agree with Sand. To me, it is better to give alternative approach, because reading your solution is really hard. This is my offer. I think it could be more clear to you. P.S. Within more that 15 years of developments, I faced with couple times with arrays more than 2D. Do use class instead of 3D... arrays.



        private static final class User {

        private final String firstName;
        private final String middleName;
        private final String lastName;

        private String barangay;
        private String city;
        private String province;
        private String mailingAddress;
        private String contactNumber;
        private String email;
        private String elementarySchool;
        private String secondarySchool;
        private String tertiary;

        public User(String firstName, String middleName, String lastName) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        }

        @Override
        public boolean equals(Object obj) {
        if (this == obj)
        return true;
        if (!(obj instanceof User))
        return false;
        User user = (User)obj;
        return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
        && Objects.equals(lastName, user.lastName);
        }

        @Override
        public int hashCode() {
        return Objects.hash(firstName, middleName, lastName);
        }
        }

        private static User getUser(Scanner scan, int i) {
        System.out.print("n" + i + ". Enter your first name: ");
        String firstName = scan.next();

        System.out.print(i + ". Enter your middle name: ");
        String middleName = scan.next();

        System.out.print(i + ". Enter your last name: ");
        String lastName = scan.next();

        return new User(firstName, middleName, lastName);
        }

        private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
        System.out.print("n" + i + ". Enter your barangay: ");
        user.barangay = scan.nextLine();

        System.out.print(i + ". Enter your city: ");
        user.city = scan.nextLine();

        System.out.print(i + ". Enter your province: ");
        user.province = scan.nextLine();

        System.out.print("n" + i + ". Enter your mailing address: ");
        user.mailingAddress = scan.nextLine();

        System.out.print(i + ". Enter your contact number: ");
        user.contactNumber = scan.nextLine();

        System.out.print(i + ". Enter your email address: ");
        user.email = scan.nextLine();

        System.out.print("n" + i + ". Enter your elementary school: ");
        user.elementarySchool = scan.nextLine();

        System.out.print(i + ". Enter your secondary school: ");
        user.secondarySchool = scan.nextLine();

        System.out.print((i + 1) + ".Enter your tertiary: ");
        user.tertiary = scan.nextLine();

        return user;
        }

        private static Set<User> getUsers() {
        try (Scanner scan = new Scanner(System.in)) {
        System.out.print("How many names do you like to enter? ");
        Set<User> users = new LinkedHashSet<>();

        for (int i = 1, total = scan.nextInt(); i <= total; ) {
        User user = getUser(scan, i);

        if (users.contains(user)) {
        System.out.println("Ops! Your name is already inputed.");
        System.out.println("Please Try again.");
        } else {
        users.add(getUserAdditionalInfo(user, scan, i));
        i++;
        }
        }

        return Collections.unmodifiableSet(users);
        }
        }

        public static void main(String args) {
        Set<User> users = getUsers();
        }





        share|improve this answer













        Agree with Sand. To me, it is better to give alternative approach, because reading your solution is really hard. This is my offer. I think it could be more clear to you. P.S. Within more that 15 years of developments, I faced with couple times with arrays more than 2D. Do use class instead of 3D... arrays.



        private static final class User {

        private final String firstName;
        private final String middleName;
        private final String lastName;

        private String barangay;
        private String city;
        private String province;
        private String mailingAddress;
        private String contactNumber;
        private String email;
        private String elementarySchool;
        private String secondarySchool;
        private String tertiary;

        public User(String firstName, String middleName, String lastName) {
        this.firstName = firstName;
        this.middleName = middleName;
        this.lastName = lastName;
        }

        @Override
        public boolean equals(Object obj) {
        if (this == obj)
        return true;
        if (!(obj instanceof User))
        return false;
        User user = (User)obj;
        return Objects.equals(firstName, user.firstName) && Objects.equals(middleName, user.middleName)
        && Objects.equals(lastName, user.lastName);
        }

        @Override
        public int hashCode() {
        return Objects.hash(firstName, middleName, lastName);
        }
        }

        private static User getUser(Scanner scan, int i) {
        System.out.print("n" + i + ". Enter your first name: ");
        String firstName = scan.next();

        System.out.print(i + ". Enter your middle name: ");
        String middleName = scan.next();

        System.out.print(i + ". Enter your last name: ");
        String lastName = scan.next();

        return new User(firstName, middleName, lastName);
        }

        private static User getUserAdditionalInfo(User user, Scanner scan, int i) {
        System.out.print("n" + i + ". Enter your barangay: ");
        user.barangay = scan.nextLine();

        System.out.print(i + ". Enter your city: ");
        user.city = scan.nextLine();

        System.out.print(i + ". Enter your province: ");
        user.province = scan.nextLine();

        System.out.print("n" + i + ". Enter your mailing address: ");
        user.mailingAddress = scan.nextLine();

        System.out.print(i + ". Enter your contact number: ");
        user.contactNumber = scan.nextLine();

        System.out.print(i + ". Enter your email address: ");
        user.email = scan.nextLine();

        System.out.print("n" + i + ". Enter your elementary school: ");
        user.elementarySchool = scan.nextLine();

        System.out.print(i + ". Enter your secondary school: ");
        user.secondarySchool = scan.nextLine();

        System.out.print((i + 1) + ".Enter your tertiary: ");
        user.tertiary = scan.nextLine();

        return user;
        }

        private static Set<User> getUsers() {
        try (Scanner scan = new Scanner(System.in)) {
        System.out.print("How many names do you like to enter? ");
        Set<User> users = new LinkedHashSet<>();

        for (int i = 1, total = scan.nextInt(); i <= total; ) {
        User user = getUser(scan, i);

        if (users.contains(user)) {
        System.out.println("Ops! Your name is already inputed.");
        System.out.println("Please Try again.");
        } else {
        users.add(getUserAdditionalInfo(user, scan, i));
        i++;
        }
        }

        return Collections.unmodifiableSet(users);
        }
        }

        public static void main(String args) {
        Set<User> users = getUsers();
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 10:26









        oleg.cherednikoleg.cherednik

        6,71721118




        6,71721118






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408969%2fif-the-two-names-are-same-the-program-will-error%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$