I have been trying to make a program in which the user is asked to enter a sentence with only one blank, and once they do that, my loop is supposed to go across the string and find a blank space, then take the first name of the user and the last name. The only problem is that my loop does not see the blank, it never enters the if. I've tried all afternoon even though it seemed simple and now it is literally eating me.
Here is my code:
Scanner sc = new Scanner(System.in);
String firstName="";
String lastName="";
String fullName="";
int cpt=0;
int cptBlank=0;
System.out.println("Entrez votre nom et prénom :");
fullName = sc.nextLine();
cpt = fullName.length();
indicateur = cpt+1;
for(int i=0; i<cpt;i++) {
if(fullName.indexOf(i) == ' ') {
cptBlank = i;
System.out.println(i);
}
System.out.println("test");
}
firstName = fullName.substring(0, cptBlank);
lastName = fullName.substring(cptBlank+1, fullName.length() - firstName.length() );*
Here is the return of my program:
Entrez votre nom et prénom :
jean jean
bla
bla
bla
bla
bla
bla
bla
bla
bla
La chaîne contient : 9 caractères
9
Your full name is : jean jean
Votre prénom est :
Votre nom est : ean jean