简体   繁体   中英

Why is my String Variable empty after assigning something to it for a second time?

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

'''

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++;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM