2

I am doing a project for school, and I am trying to make it to where you can set up a name for yourself while going through a series of questions asked by the computer. I want the user to be able to change their name right after assigning it if they do not like what they put down or they typed something wrong. Right now the program assigns the name the user wants correctly the first time, but when it goes back through the loop to change it to something else the string is left blank. Console Output '''

    import java.util.*;

    public class JavaInputProdject 
     {
        public static void main(String args[])
          {
            int i=0;
            boolean boo = false;
            int likeab = 0;
            byte age;
            boolean Old=false;
            boolean aAge=true;
            String user="User";
            String un = user + "> ";
            Scanner bob = new Scanner(System.in);
    
    System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n"+un);
    
    do
        {
            user = bob.nextLine();
            System.out.println("Bob> This is the Username you want? \""+ user +"\"(true/false)");
            System.out.print(un);
             
            
            if(bob.nextBoolean()==true)
                {
                    boo = true;
                    un = user + "> ";
                }
            
            else
                {
                        if(i>=3)
                            {
                                System.out.println("Bob> I realize it is kind of hard to pick a name but could you hurry up?");
                            }
                    System.out.print("Bob> Please type in a new Username\n"+un);
                    bob.next();
                    i++;
                }
            
        } while(boo==false);
     }
    }

'''

  • No, I don't think that is the case, I had to add in next() because it was skipping nextLine(). Without it, it bulldozes right past where the user is supposed to be able to input their username. – Trinity Dionne Oct 17 '21 at 13:40

2 Answers2

0

when you want to get correct username based on false flag you doesnt init a value to user. you should write something like this with bob.nextLine :

 System.out.print("Bob> Please type in a new Username\n"+un);
 user = bob.nextLine();
 i++;
HamidSayyah
  • 423
  • 3
  • 19
  • Since the code is in a loop wouldn't the first part of the loop initialize the variable again? I understand what you are saying but after the user types out false, it asks them to type in a new username, and then it loops back to the beginning where it should be able to accept a new value for "user". – Trinity Dionne Oct 17 '21 at 13:31
  • @TrinityDionne ok you dont need to assign user to it but you should use bob.nextLine() instead of bob.next() – HamidSayyah Oct 17 '21 at 13:55
0

You need to replace the line bob.next() (near the end of the do-while loop) with bob.nextLine().

I believe that bob.next() does not consume the newline that is entered as a result of hitting the <ENTER> key after the bob.nextBoolean() call. Hence the user = bob.nextLine(); line (at the start of the do-while loop) is consuming that newline on the second and subsequent loop iterations. So replacing bob.next() with bob.nextLine() will resolve the problem.

For the sake of completeness, here is the corrected code:

import java.util.Scanner;

public class JavaInputProdject {

    public static void main(String[] args) {
        int i = 0;
        boolean boo = false;
        int likeab = 0;
        byte age;
        boolean Old = false;
        boolean aAge = true;
        String user = "User";
        String un = user + "> ";
        Scanner bob = new Scanner(System.in);
        System.out.print("Bob> Hey User, My name is BOB.... what is your name?\n" + un);
        do {
            user = bob.nextLine();
            System.out.println("Bob> This is the Username you want? \"" + user + "\"(true/false)");
            System.out.print(un);
            if (bob.nextBoolean()) {
                boo = true;
                un = user + "> ";
            }
            else {
                if (i >= 3) {
                    System.out.println(
                            "Bob> I realize it is kind of hard to pick a name but could you hurry up?");
                }
                System.out.print("Bob> Please type in a new Username\n" + un);
                bob.nextLine();
                i++;
            }

        } while (boo == false);
    }
}

Refer to Scanner is skipping nextLine() after using next() or nextFoo()?

Abra
  • 19,142
  • 7
  • 29
  • 41