简体   繁体   中英

Unexpected line printing when using Scanner

I am using practice it for java, and have a question about the following exercise:

Write a complete program called "TheNameGame," where the user inputs a first and last name and a song in the following format is printed about their first, then last, name. Use a method to avoid redundancy.

What is your name? Fifty Cent
Fifty Fifty, bo-Bifty
Banana-fana fo-Fifty
Fee-fi-mo-Mifty
FIFTY!
Cent Cent, bo-Bent
Banana-fana fo-Fent
Fee-fi-mo-Ment
CENT!

This is the code I wrote for the answer:

public class TheNameGame {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        String first = scan.next();
        String last = scan.next();
        String newFirst = first.substring(1);
        String newLast = last.substring(1);

        System.out.println("What is your name? " + first + " " + last);
        playGame(first, newFirst);
        playGame(last, newLast);
    }
}

public static void playGame(String name, String nn) {
    System.out.println(name + " " + name + ", " + "bo-B" + nn);
    System.out.println("Banana-fana fo-F" + nn);
    System.out.println("Fee-fi-mo-M" + nn);
    System.out.println(name.toUpperCase() + "!");
}

And this is the output:

Fifty Cent
What is your name? Fifty Cent
Fifty Fifty, bo-Bifty
Banana-fana fo-Fifty
Fee-fi-mo-Mifty
FIFTY!
Cent Cent, bo-Bent
Banana-fana fo-Fent
Fee-fi-mo-Ment
CENT!

My question is; where is the first line (Fifty Cent) coming from? Thank you in advance.

I see that you are trying to ask the question, and then get the input. You only have to make a couple of minor changes in your main method. Your main method should look like this:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    System.out.print("What is your name? "); //asks question first,
    String first = scan.next();              //THEN accepts first
    String last = scan.next();               //and last name
    String newFirst = first.substring(1);
    String newLast = last.substring(1);

    playGame(first, newFirst);
    playGame(last, newLast);
}

This will give the output:

What is your name? Fifty Cent
Fifty Fifty, bo-Bifty
Banana-fana fo-Fifty
Fee-fi-mo-Mifty
FIFTY!
Cent Cent, bo-Bent
Banana-fana fo-Fent
Fee-fi-mo-Ment
CENT!

It first asks the question, then accepts the first and last name of the user. Your issue was simply that your code was out of order.

My question is; where is the first line (Fifty Cent) coming from?

When you enter :

Fifty Cent

String first = scan.next(); will read Fifty and String last = scan.next(); read Cent

for that when you print :

System.out.println("What is your name? " + first + " " + last);

It will print :

What is your name? Fifty Cent

My question is; where is the first line (Fifty Cent) coming from?

It is not coming from anywhere. You input it, when you do

String first = scan.next();
String last = scan.next();

and Fifty Cents stays there on the screen. Remember , values you input continue to stay on the screen .

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