-1

I have a project which I need to ask the user to introduce at least 5 valid full names (until 10 full names or if the user introduce "fim"). Each full name needs to have at least 2 names with 4 characters each and the full name is only valid if it doesn't exceed 120 characters. I need to create a array for each full name which the elements are the names that are part of the full name. Here I have my code yet. I have a lot of options that doesn't work in comment. "Nome Inválido" id invalid name and "Nome Válido" iS Valid Name.

public static void main(String[] args) {
    Scanner keyboard = new Scanner (System.in);
    System.out.println("Introduza até 10 nomes completos com até 120 caracteres e pelo menos dois nomes com pelo menos 4 caracteres: ");
    String nome;
    int i = 0;
    do {
        //nomes[i] = keyboard.next();
        nome = keyboard.nextLine();
        i++;

        String[] nomeSeparado =  nome.split(" ");
        System.out.print(Arrays.toString(nomeSeparado));

        int j = nomeSeparado[i].length();

        /**
        1) for(int k = 0; k < 2; k++) {
            if(!(j == 4)) {
                System.out.println(" Nome Inválido ");
            }
            else {
                System.out.println(" Nome Válido ");
            }
        }

       2) while( k < 2 ) {
            if(!(j == 4)) {
                System.out.println(" Nome Inválido ");
            }
            else {
                System.out.println(" Nome Válido ");
            }
        }

        3) if(while(!(nomeSeparado[i].length() == 4)<2)) {
            System.out.println(" Nome Inválido ");
        }

        4) for(i = 0; i < 10 ; i++) {
            if( j > 2 && nomeSeparado[i].length() == 4 ) {
                System.out.println(" Nome Válido ");
            }
            else {System.out.println(" Nome Inválido ");}
        }
        **/

    }
    while(!nome.equalsIgnoreCase("fim") && i<10);
}
Jeff Butler
  • 991
  • 6
  • 11
Ruca
  • 17
  • 4
  • 4
    Can you describe how/why it isn't working? – Preston Martin Mar 21 '18 at 16:34
  • 4
    Welcome to stackoverflow, where we gladly help if presented with concrete, reproducible problems. stackoverflow is not for having others do your homework or debug your code. Please debug your code and present the results: Why are these options not working? What are the errors you get? What behavior do you see? What are the values in each step? Post the (unexpected) results, post error messages (if any), identify and post the lines where things go wrong. Tell us what you tried to fix those issues, tell us why it didn't work. – Max Vollmer Mar 21 '18 at 16:38
  • Because when the name is invalid it says that the name is valid (vice-versa). The full names need to have at least too names with 4 characters to be valid. How would I do that? – Ruca Mar 21 '18 at 16:38
  • If you don't know how, see this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Max Vollmer Mar 21 '18 at 16:39
  • 1
    It is not working bc it doesn't have the desired output. – Ruca Mar 21 '18 at 16:39
  • first of all your code has a problem. you say `The full names need to have at least too names with 4 characters to be valid`. At least means there could be more. what if you have 3 names `n1`, `n2`, `n3` and `n1`, and `n3` is valid, but not `n2` ? you have no way of knowing this when you only check `for(int k = 0; k < 2; k++)`. You should check all the tokens. – arthur Mar 21 '18 at 16:47

2 Answers2

1

Every time the user inputs a name you save the first name as the first element of the nomeSeparado and the second name as a second element. What you have to do is inspect the length of both the first and second element and check if they comply with the rules. A logic like this should work:

 int lengthOfFirstName = nomeSeparado[0].length();
 int lengthOfSecondName = nomeSeparado[1].length();
 if (lengthOfFirstName >= 4 && lengthOfSecondName >= 4 && lengthOfFirstName + lengthOfSecondName < 120) {
     System.out.println("Valid name");
 } else {
     System.out.println("Invalid name");
 }
mishless
  • 159
  • 10
  • 1
    Although OP's english is doubtful, this should be `lengthOfFirstName >= 4 && lengthOfSecondName >= 4`. At least it seems so, by the second rule `it can't exceed 120 characters` – Jorge Campos Mar 21 '18 at 16:50
0

Why are you using arrays? I think .length() saves you from that

import java.util.Scanner;

public static void main(String[] args) {

String firstName, surname, fullName;
int fullNameCount;

 Scanner input = new Scanner(System.in);

 System.out.println("Enter your first name");
 firstName = input.nextLine();

 System.out.println("Enter your surname");
 surname = input.nextLine();



 if (firstName.length() <= 4 && surname.length() <=4 ){
     System.out.println("Invalid name! Exit");
 } else{
      fullName = firstName + surname;
      fullNameCount = fullName.length();
      System.out.println(fullNameCheck(fullNameCount));
      char[] fullNameArray = fullName.toCharArray();
 } 
}

public static String fullNameCheck(int fullNameCount){
    if (fullNameCount <= 120){
        return "Valid Name";
    }
    else{
        return "Invalid Name";
    }
}   

}

Desiree
  • 3
  • 5