• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Question about Scanner

 
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am working through an online textbook for Java. I am working on the following programming exercise:

If you have N eggs, then you have N/12 dozen eggs, with N%12 eggs left over. (This is essentially the definition of the / and % operators for integers.) Write a program that asks the user how many eggs she has and then tells the user how many dozen eggs she has and how many extra eggs are left over. (http://math.hws.edu/javanotes/c2/exercises.html)

The proposed solution by the text uses a file written by the author. I was trying to complete it using java.util.scanner. Here is my code, but it keeps throwing an error. Which is why the author probably proposed a different solution. But, in short, I'd like to know why this code doesn't work.



Here is the error I get: java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=\.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\QNaN\E][infinity string=\Q∞\E]

Thanks in advance!

 
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of this line in your code?:
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The purpose of that line is to get input from the user. I guess I could have put it on the same line as the previous print statement. Is that why it isn't working?
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khayla Matthews wrote:The purpose of that line is to get input from the user.


The println method displays information, it doesn't read information.


The line that follows is the one actually reading the input from the user/console:
 
Khayla Matthews
Greenhorn
Posts: 23
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The println method displays information, it doesn't read information.


The line that follows is the one actually reading the input from the user/console:


And it works now! Thanks. I thought I had to essentially print the blank space to the screen, so the user would know ti input information. But, I'm seeing now that when 'dozens' and 'remainingAmt' are referenced/called the program goes through the necessary steps.
 
Ron McLeod
Marshal
Posts: 4017
549
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you pass an object to println, it will invoke the object's toString() method.  It looks like in the case of a Scanner instance, that toString() returns some information about it's internal configuration/state.
 
Your mind is under my control .... your will is now mine .... read this tiny ad
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic