-2

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
Zoe
  • 27,060
  • 21
  • 118
  • 148
brn
  • 275
  • 1
  • 6
  • 17
  • 1
    `indexOf()` inside of a loop is not needed. Is this homework? Are you allowed to use string methods like `indexOf()`? If you're required to write the loop yourself I don't think `indexOf()` would be allowed. – markspace Sep 08 '18 at 18:55
  • The code you are showing and the output dont fit together at all. Where is that `La chaîne contient : 9 caractères` printed for example? Please: programming is about caring for **all** details. We can't help you unless you provide a clear, correct [mcve]! – GhostCat Sep 08 '18 at 18:56
  • 1
    String comparisons should use `.equals()` not `==`. – Sam Sep 08 '18 at 18:57
  • 1
    @SamLittlefair is the OP comparing strings or characters though? – markspace Sep 08 '18 at 18:58
  • if you want to write a loop then you would use charAt(...) to test each character for a space. Otherwise indexOf(...) will just return the index of the first space so no loop is required. – camickr Sep 08 '18 at 18:58
  • @markspace It's a training exercise more than a homework, I tried indexOf() because I had already tried charAt() and subrstring() without success. – brn Sep 09 '18 at 09:42
  • @GhostCat Yes, I just noticed the end output was not conformed to the code I am sorry I tried a lot of different ways so my class was full of comment and the output "la Chaine contient 9 caractères" was at the very bottom of the class so I missed it. Thanks for the feedback! – brn Sep 09 '18 at 09:44
  • @camickr I already had tried charAt() without success sadly so I tried few other ways, the one written above was the last one so I published my code with it. Thanks for the edback ! – brn Sep 09 '18 at 09:46

2 Answers2

0

If you don't mind I have re-written your code, don't know whether you will like it or not but here it goes:

public static void main(String[] args) throws IOException {
    Scanner sc = new Scanner(System.in);
    String firstName="";
    String lastName="";
    String fullName="";
    String names[] = new String[2];

    System.out.println("Entrez votre nom et prénom :");
    fullName = sc.nextLine();
    names = fullName.split(" ");
    firstName = names[0];
    lastName = names[1];
    System.out.println("Full name: "+fullName+" First name :"+firstName+" Last Name :"+lastName);

}

I have split the input String on the blank space and collected the first and last names. You don't need any loops to do this.

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
  • Your code works perfectly! If I understand it correctly, you created a Sring array of two named 'names', split the string output by the user on the blank " " thanks to the method split which also places the result before and after inside the array names and then placed it into firstName and lastName? Thanks for your support ! – brn Sep 09 '18 at 09:52
  • The use of StringTokenizer is discouraged in new code as per the Javadoc. Please refer this answer: https://stackoverflow.com/a/6983908/3888766 – Fullstack Guy Sep 09 '18 at 11:31
-2

If I understood right you are trying to take the Full Name as an input and separate it into two variable so try this Example code I wrote for You using class called String Tokenizer which main purpose is to take a string and a separator and outputs tokens after cutting this string.

String Fullname , Firstname , Lastname;

   Scanner input = new Scanner(System.in);
   System.out.println("Enter Full Name");
   Fullname = input.nextLine();

        StringTokenizer st = new StringTokenizer(Fullname, " ");
        Firstname = st.nextToken();
        Lastname = st.nextToken();
        System.out.println("First Name: "+Firstname);
        System.out.println("Last Name: "+Lastname);
Mohamed Nashaat
  • 90
  • 2
  • 11
  • 1
    You understood right, your code works perfectly. The class Tokenizer seems really efficient I'm gonna read more about it, thanks for your help ! – brn Sep 09 '18 at 09:49
  • (1-) Variable names should NOT start with an upper case character. Follow Java naming conventions when posting code. – camickr Sep 09 '18 at 18:06
  • @camickr So you left the Logic of the Function and Everything and the only thing that annoys you is the Naming Convention!!!!!! – Mohamed Nashaat Oct 06 '20 at 23:54