• 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

Skipping user input statment

 
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 , the following program basically takes in user input and then display the information using toString method but it skips over the last question of what town there from and just prints out the info






Thanks as always.
 
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
This is a classic Scanner problem. When you call sc.nextInt() it returns the integer BUT it leaves the new-line character still in the input stream. This causes the sc.nextLine() call in your getTowns() method to see that new-line and immediately return an empty String. The solution is to add, after your call to sc.nextInt(), make a call to nextLine(). This will remove the dangling new-line.
 
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

Carey Brown wrote:This is a classic Scanner problem. When you call sc.nextInt() it returns the integer BUT it leaves the new-line character still in the input stream. This causes the sc.nextLine() call in your getTowns() method to see that new-line and immediately return an empty String. The solution is to add, after your call to sc.nextInt(), make a call to nextLine(). This will remove the dangling new-line.



So it should be



instead of



 
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
Think I was confused by your answer,  I took out Line and just used next, thanks for the help.
 
Carey Brown
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 a number of issues with your code.
  • Java convention suggests that the use of "get" or "set" to start a method name with indicates that it is a "getter" or a "setter" method with predictable behavior of getting or setting a member variable (aka "field"). You have those, but you also have getLastNames(), which neither sets nor gets the member variable "name". These methods should be named differently, perhaps inputName(). Additionally, my feeling is that these input methods should be setting the member variables and not setting and returning a local variable. They should return void.
  • Using getter/setter convention, the method name should start with get or set followed by the member variable name in camel case. Your methods getlastName() and setlastName() should be getLastName() and setLastName() (with an upper case 'L').
  • Your toString() method should do nothing more than build and return a string from the member variables. You have it inputting data.
  • If you are going to have a "lastName" field, then your other name field should really be firstName.

  •  
    Carey Brown
    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
    This is the ONLY change you needed to make
     
    Carey Brown
    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

    Kevin O'Sullivan wrote:Think I was confused by your answer,  I took out Line and just used next, thanks for the help.


    You don't want to call next(), it will fail if the town name has two words, e.g. "Wheat Ridge". Put it back to calling nextLine().
     
    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 Carey.
     
    Carey Brown
    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
    Ideally you'd change the method to look more like this.

    Changed:
    return type of void
    method name
    println() to print()
    assign to member variable
    don't return anything
     
    reply
      Bookmark Topic Watch Topic
    • New Topic