• 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

Why is this code ending before printing out the sum?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason it allows me to input both integers but it does not print out the Sum or 'Test' at the end. Please help!
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, a couple of things please use the [code][/code] html built-in tags when posting code. They are located within the code posting interface. Also, I can attribute the reason why the code ends is due to the Scanner class and the way it handles input.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Anthony. Being a moderator of these forums, I've added the code tags that Naziru was talking about. See how much better your code looks?

The problem with your code is that you are calling nextInt() on your Scanner objects multiple times. Each time you call that method, the Scanner is going to read a new integer.

Instead of calling the method multiple times, call it once when you want the user to enter a number, and then save the result in a variable. Then use that variable later when you want to display what the user entered.

Also, you don't need two Scanner objects A and B. You can use just one Scanner object. Finally, when you have a Scanner that reads from System.in, you should not close it (lines 15 and 16).
 
I love a good mentalist. And so does this tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!