• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Printing system.out.statment twice

 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi



"Please enter your name" is being printed out twice, any ideas why? I tried putting in sc.nextLine(); but it didn't work.
 
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have in a while(true) block and there's only one break inside. If you don't exercise the break you'll go back again.
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code is inside of a "while(true)" loop, which will repeat endlessly until it encounters a "break," until an exception is thrown, or until the program exits. If you type "Barry", it ought to print the prompt only once and exit, but under any other circumstances, it'll print it endlessly.
 
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry to nitpick, Just wanted to point out :
 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Sorry to nitpick, Just wanted to point out :



I saw that and fixed it,I was trying something else before I posted here and left that extra bracket in by mistake.

 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Just to show ye what its doing, I'm actually stumped by it
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Screen shot is useless, too small. Can you copy-and-paste text into a post?
 
Ranch Hand
Posts: 218
5
MS IE Notepad Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well - pretty obvious what's missing : context!
you just posting some random line of your code about some loop prints something twice

common beginner mistake I spotted: using scanner to fetch users input
you don't have to post the rest of the code but i can tell you from experience with such code (and i bet my next beer on it): you almost 100% certian use scanner somewhere earlier but don't use readLine but some other of the nextXXX methods - what happens: the line break is still stuck in the internal buffer of the scanner cause it's correctly flushed in the last usage - so the first loop will get an empty string

easy to proof: add as the first line inside the loop a String.length wich will result 0 and so it doesn't get matched by any of your if - cause : second mistake : using if-elsif without final -else to clear all other cases where neither if matches - don't this


advice:
1) get rid of scanner and use propper input like BufferedReader.nextLine
2) ALWAYS use nextLine and perform casts and parses yourself - so tailing line break always gets cleared
3) use switch(String) with propper default handling - it cleans up old-style if-else chains and if the default is filled with useful logic like invalidargumentexception you know you made a logical code mistake by letting lines happen executed wich you not supposed to get run

4) please dont post screenshots - use code-tags and copy'paste
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Wong wrote:advice:
1) get rid of scanner and use proper input like BufferedReader.nextLine
2) ALWAYS use nextLine and perform casts and parses yourself - so tailing line break always gets cleared


Yeah, I fought the Scanner vs BufferedReader as well as the "parse it yourself" issues for a long time. I eventually gave up. The tide of common usage was overwhelming. So now I'm reduced to educating others on how to avoid the pitfalls of Scanner. In some specific instances I return to BufferedReader but I see where sometimes Scanner has its place as well.
 
Kevin O'Sullivan
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I'll read up on Buffer.
 
salvin francis
Bartender
Posts: 2880
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:I saw that and fixed it,I was trying something else before I posted here and left that extra bracket in by mistake.


I am glad that you realized that.

Now let's look at your program as a standalone code ...


As you rightly stated, your code works fine for 'Barry' and does not quit for 'Kevin'. Isn't that pointing to the direction that you are doing something special for 'Kevin' that's missing for 'Barry' ?

Let me point out the lines of code:

Both if and else conditions are calling a method: println() and getName() ... Can you spot the difference ?
 
Carey Brown
Saloon Keeper
Posts: 8588
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:Thanks guys, I'll read up on Buffer.


I guess what I was trying to say was use Scanner instead of BufferedReader but expect a couple of nasty bumps to the shins as you try to wrap your head around the pitfalls.

To start out:
  • Only ever create a new Scanner() with System.in once for the life time of your running project. It's best to create it as a class constant and share that constant among all of your other code.
  • Never ever close a Scanner created with System.in, no matter what the compiler/IDE tells you.
  • Scanner#nextLine() is the only method guaranteed to absorb the ending new-line character. That's not to say don't use other methods, e.g. nextInt(), just remember they'll leave a new-line in the input stream that you WILL have to deal with.

  • Often a project contains a class whose only purpose is to be a wrapper around Scanner(System.in). Here's a blatantly simplistic class.
    Now in your project you can call
    To elaborate on this you could incorporate wrappers for each of the Scanner methods, like nextInt().
    Now in your project you can call

     
    salvin francis
    Bartender
    Posts: 2880
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:....It's best to create it as a class constant and share that constant among all of your other code.


    I haven't used Scanner for a while now, but as far as I remember, it is not thread safe. Is it good to use it as a class constant ?
     
    Carey Brown
    Saloon Keeper
    Posts: 8588
    71
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    salvin francis wrote:

    Carey Brown wrote:....It's best to create it as a class constant and share that constant among all of your other code.


    I haven't used Scanner for a while now, but as far as I remember, it is not thread safe. Is it good to use it as a class constant ?


    No, it isn't. But what about any CUI (console user interface) would make any sense in a threaded environment? Each Scanner has its own buffer and state, so if you open two Scanners to System.in, even in a single threaded app, the keystrokes could end up in the wrong Scanner object. Even GUIs only allow a single point of keyboard focus at a time.
     
    Marshal
    Posts: 74037
    332
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Matt Wong wrote:. . . advice:
    1) get rid of scanner and use propper input like BufferedReader.nextLine
    2) ALWAYS use nextLine and perform casts and parses yourself - so tailing line break always gets cleared . . .

    I think that is bad advice. The default delimiter for a Scanner is multiple whitespace, which includes line ends, so all the nextXXX() methods except nextLine() simply run past the line ends. If you use a buffered reader you will end up with lots of repeated code, so you want a utility class to read from the keyboard. That is what we used to write before Scanner was introduced in 2004. After that, I was told that utility classes were no longer required, but I think that is mistaken. You should have a utility class which will give you nextInt(), nextBigDecimal(), etc methods. If you search my posts you will find the loop which Rob Spoor taught me about a long time ago, which will ensure that nextInt() returns an int and avoids any input mismatch exceptions. If you search far enough, you will find out how to write a nextLine() method that never returns an empty String. You will also find a mistake of mine. There is an explanation for the empty String problem here.
    Using a buffered reader would mean you have to write lots of code to handle checked exceptions, an dyuo woiul have difficulty with input not matching the required formats. So you end up with much more complicated code than using a Scanner in the utility class.

    please dont post screenshots - use code-tags and copy'paste

    I think that is good advice. Look here and here. Using copy'n'paste might have prevented that duplication of brackets.
     
    salvin francis
    Bartender
    Posts: 2880
    150
    Google Web Toolkit Eclipse IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:But what about any CUI (console user interface) would make any sense in a threaded environment?


    Agreed, so just stating this for OP's reference, ideally, a Scanner would not be a constant at a class level. But, in the case of System.in, it can be used since we aren't worried about multiple threads.
    Or .. the best of both worlds.. Create a Utility class that internally uses a Scanner as Campbell suggested.

    Back to the Op's question..
    were you able to figure out the difference between Barry and Kevin in your code ?
     
    You showed up just in time for the waffles! And this tiny ad:
    Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    reply
      Bookmark Topic Watch Topic
    • New Topic