Input line from Scanner object gets printed before official print command











up vote
0
down vote

favorite












I have a the following method below that receives a Scanner object with N number of lines, each with a a one word string.



    public static void longestName(Scanner console, int n) {

for (int i = 1; i <= n; i++) {

System.out.println("name #" + i + "?" + " " + console.nextLine());

}

}


Instead of this (expected output)...



name #1? roy
name #2? DANE
name #3? Erik
name #4? sTeFaNiE
name #5? LaurA


...I'm getting this



roy
name #1? roy
DANE
name #2? DANE
Erik
name #3? Erik
sTeFaNiE
name #4? sTeFaNiE
LaurA
name #5? LaurA


Why is the "nextLine()" from the Scanner object being printed once prior to the actual print command output?



****** This is a practice problem I'm using and they only ask me to define a method "longestName" that takes a Scanner object and an integer "n" denoting the number of names in the Scanner object.



The output above is a result of the method being used with a Scanner object with "n" number of names.










share|improve this question
























  • Please post full code.
    – Sasikumar Murugesan
    Nov 8 at 19:42






  • 2




    Er isn't that just the text you typed yourself, followed by the output of the code?
    – OpenSauce
    Nov 8 at 19:43










  • @OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
    – Edson
    Nov 8 at 21:05












  • @Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
    – OpenSauce
    Nov 8 at 21:14










  • @OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
    – Edson
    Nov 8 at 21:17















up vote
0
down vote

favorite












I have a the following method below that receives a Scanner object with N number of lines, each with a a one word string.



    public static void longestName(Scanner console, int n) {

for (int i = 1; i <= n; i++) {

System.out.println("name #" + i + "?" + " " + console.nextLine());

}

}


Instead of this (expected output)...



name #1? roy
name #2? DANE
name #3? Erik
name #4? sTeFaNiE
name #5? LaurA


...I'm getting this



roy
name #1? roy
DANE
name #2? DANE
Erik
name #3? Erik
sTeFaNiE
name #4? sTeFaNiE
LaurA
name #5? LaurA


Why is the "nextLine()" from the Scanner object being printed once prior to the actual print command output?



****** This is a practice problem I'm using and they only ask me to define a method "longestName" that takes a Scanner object and an integer "n" denoting the number of names in the Scanner object.



The output above is a result of the method being used with a Scanner object with "n" number of names.










share|improve this question
























  • Please post full code.
    – Sasikumar Murugesan
    Nov 8 at 19:42






  • 2




    Er isn't that just the text you typed yourself, followed by the output of the code?
    – OpenSauce
    Nov 8 at 19:43










  • @OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
    – Edson
    Nov 8 at 21:05












  • @Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
    – OpenSauce
    Nov 8 at 21:14










  • @OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
    – Edson
    Nov 8 at 21:17













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a the following method below that receives a Scanner object with N number of lines, each with a a one word string.



    public static void longestName(Scanner console, int n) {

for (int i = 1; i <= n; i++) {

System.out.println("name #" + i + "?" + " " + console.nextLine());

}

}


Instead of this (expected output)...



name #1? roy
name #2? DANE
name #3? Erik
name #4? sTeFaNiE
name #5? LaurA


...I'm getting this



roy
name #1? roy
DANE
name #2? DANE
Erik
name #3? Erik
sTeFaNiE
name #4? sTeFaNiE
LaurA
name #5? LaurA


Why is the "nextLine()" from the Scanner object being printed once prior to the actual print command output?



****** This is a practice problem I'm using and they only ask me to define a method "longestName" that takes a Scanner object and an integer "n" denoting the number of names in the Scanner object.



The output above is a result of the method being used with a Scanner object with "n" number of names.










share|improve this question















I have a the following method below that receives a Scanner object with N number of lines, each with a a one word string.



    public static void longestName(Scanner console, int n) {

for (int i = 1; i <= n; i++) {

System.out.println("name #" + i + "?" + " " + console.nextLine());

}

}


Instead of this (expected output)...



name #1? roy
name #2? DANE
name #3? Erik
name #4? sTeFaNiE
name #5? LaurA


...I'm getting this



roy
name #1? roy
DANE
name #2? DANE
Erik
name #3? Erik
sTeFaNiE
name #4? sTeFaNiE
LaurA
name #5? LaurA


Why is the "nextLine()" from the Scanner object being printed once prior to the actual print command output?



****** This is a practice problem I'm using and they only ask me to define a method "longestName" that takes a Scanner object and an integer "n" denoting the number of names in the Scanner object.



The output above is a result of the method being used with a Scanner object with "n" number of names.







java java.util.scanner user-input system.out console-input






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 21:16

























asked Nov 8 at 19:37









Edson

123111




123111












  • Please post full code.
    – Sasikumar Murugesan
    Nov 8 at 19:42






  • 2




    Er isn't that just the text you typed yourself, followed by the output of the code?
    – OpenSauce
    Nov 8 at 19:43










  • @OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
    – Edson
    Nov 8 at 21:05












  • @Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
    – OpenSauce
    Nov 8 at 21:14










  • @OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
    – Edson
    Nov 8 at 21:17


















  • Please post full code.
    – Sasikumar Murugesan
    Nov 8 at 19:42






  • 2




    Er isn't that just the text you typed yourself, followed by the output of the code?
    – OpenSauce
    Nov 8 at 19:43










  • @OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
    – Edson
    Nov 8 at 21:05












  • @Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
    – OpenSauce
    Nov 8 at 21:14










  • @OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
    – Edson
    Nov 8 at 21:17
















Please post full code.
– Sasikumar Murugesan
Nov 8 at 19:42




Please post full code.
– Sasikumar Murugesan
Nov 8 at 19:42




2




2




Er isn't that just the text you typed yourself, followed by the output of the code?
– OpenSauce
Nov 8 at 19:43




Er isn't that just the text you typed yourself, followed by the output of the code?
– OpenSauce
Nov 8 at 19:43












@OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
– Edson
Nov 8 at 21:05






@OpenSauce - huh? I manually posted the output. The code i'm using for a practice problem on a website that runs the code I input. It shows me the output I posted above.
– Edson
Nov 8 at 21:05














@Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
– OpenSauce
Nov 8 at 21:14




@Edson the Scanner gets its input from somewhere, presumably from you typing it into the console. Since you type into the console, what you type appears in the console. The confusion probably comes from you putting the code into a website which makes it look like your input is separate from the console content. If you just ran the code as a standalone Java application it would be more clear.
– OpenSauce
Nov 8 at 21:14












@OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
– Edson
Nov 8 at 21:17




@OpenSauce - Yup, I understand what you asked now, I edited my post with an explanation.
– Edson
Nov 8 at 21:17












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










This will give you the expected output:



System.out.println("name #" + i + "?" + " ");
console.nextLine();





share|improve this answer





















  • Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
    – Edson
    Nov 8 at 21:11












  • When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
    – Justin
    Nov 8 at 22:16


















up vote
1
down vote













It is not printing it multiple times, it is the text you have typed into the console yourself. You type Erik as you can see, only after that it will process your print statement and print name #3? Erik where Erik is the text you've typed in.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214956%2finput-line-from-scanner-object-gets-printed-before-official-print-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    This will give you the expected output:



    System.out.println("name #" + i + "?" + " ");
    console.nextLine();





    share|improve this answer





















    • Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
      – Edson
      Nov 8 at 21:11












    • When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
      – Justin
      Nov 8 at 22:16















    up vote
    1
    down vote



    accepted










    This will give you the expected output:



    System.out.println("name #" + i + "?" + " ");
    console.nextLine();





    share|improve this answer





















    • Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
      – Edson
      Nov 8 at 21:11












    • When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
      – Justin
      Nov 8 at 22:16













    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    This will give you the expected output:



    System.out.println("name #" + i + "?" + " ");
    console.nextLine();





    share|improve this answer












    This will give you the expected output:



    System.out.println("name #" + i + "?" + " ");
    console.nextLine();






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 8 at 19:50









    Justin

    37618




    37618












    • Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
      – Edson
      Nov 8 at 21:11












    • When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
      – Justin
      Nov 8 at 22:16


















    • Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
      – Edson
      Nov 8 at 21:11












    • When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
      – Justin
      Nov 8 at 22:16
















    Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
    – Edson
    Nov 8 at 21:11






    Thanks, it worked after I used "print" instead of "println". Any ideas as to why the original code wasn't working correctly?
    – Edson
    Nov 8 at 21:11














    When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
    – Justin
    Nov 8 at 22:16




    When constructing the string to print out, java attempts to get the input before printing out to console. The function must finish before the line prints out
    – Justin
    Nov 8 at 22:16












    up vote
    1
    down vote













    It is not printing it multiple times, it is the text you have typed into the console yourself. You type Erik as you can see, only after that it will process your print statement and print name #3? Erik where Erik is the text you've typed in.






    share|improve this answer

























      up vote
      1
      down vote













      It is not printing it multiple times, it is the text you have typed into the console yourself. You type Erik as you can see, only after that it will process your print statement and print name #3? Erik where Erik is the text you've typed in.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        It is not printing it multiple times, it is the text you have typed into the console yourself. You type Erik as you can see, only after that it will process your print statement and print name #3? Erik where Erik is the text you've typed in.






        share|improve this answer












        It is not printing it multiple times, it is the text you have typed into the console yourself. You type Erik as you can see, only after that it will process your print statement and print name #3? Erik where Erik is the text you've typed in.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 19:41









        Mark

        2,4551620




        2,4551620






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214956%2finput-line-from-scanner-object-gets-printed-before-official-print-command%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Landwehr

            Reims

            Javascript gets undefined on array