• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Need help with java.util.InputMismatchException error

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This what my code is running after I enter the third throw for the first person?


 
Master Rancher
Posts: 195
7
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Zachery. Welcome to the Ranch! May I please ask you to reformat your code using the code tags? This will make it easier for people to read and understand your code.
 
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't looked for the specific problem, but right off the bat I can see you are creating Scanners incorrectly. For a given project, you should only ever create a single Scanner built from System.in. If you create multiple Scanner(System.in) instances you can't control which of the Scanners is getting the buffered input from System.in. The solution is to pick (or create) a single class in your project that has a single constant for this particular Scanner and then use that constant everywhere in your project, rather than creating new instances.
 
Carey Brown
Bartender
Posts: 6140
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Marshal
Posts: 65467
249
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Brecht Geeraerts wrote:. . . Welcome to the Ranch!

Yes, welcome

. . . reformat your code using the code tags? . . ..

Since he is new, I have done that for him, and doesn&aspo;t it look better

As Carey says, you are getting yourself confused about how many tokens you are getting from the keyboard. Your calls to input.next.charAt(0); consume one token from the input, calculate its first character, and discard that character because you aren't doing anything with it. I agree that you would use nextLine() to read a name, but there is a pitfall about nextLine() which I bet you won't find in your book. See this post. If you get the input you want from the Scanner out of phase with what you are asking the user to write, you will get incorrect input and the Scanner can't cope with parsing a name as a number.
Only call nextLine() twice after nextAnythingElse().
Don't use multiple Scanners for System.in. You only need one such Scanner. Using multiple Scanners makes it that much more difficult to keep them in phase with themselves. Never close Scanners (or anything else) reading System.in.

You aren't using your computer to do the work. You are asking for a name, not using it, and then writing Linda Stahl. Get the input and then use it in the display.
Have you been told about repeating actions? You have lots of repeated code, which should be in a different method, making your main() method much too long.
Your arithmetic is incorrect. The only bit you got right is 3 feet to a yard.
 
Happily living in the valley of the dried frogs with a few tiny ads.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!