Jerico M Navarro
Jerico M Navarro

Reputation: 19

If the two names are same the program will error

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();


    }
}

}

Upvotes: 1

Views: 169

Answers (2)

Oleg Cherednik
Oleg Cherednik

Reputation: 18245

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();
}

Upvotes: 1

Sandeepa
Sandeepa

Reputation: 3755

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. :)

Upvotes: 1

Related Questions