Console-based piggy bank

  • 8
Imagine that we have a piggy bank. You can put coins in it until, at some point, you break it. Let's write a console-based emulator for this piggy bank: We will enter numbers from the keyboard and add them until we "break" the process using the special word "sum". Then the program should calculate how much you have saved.
You can't complete this task, because you're not signed in.
Comments (11)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Adam
Level 8, Florida, United States
17 September 2020, 18:36
I just used try and catch with a scanner
Rebekah H
Level 8, Gothenburg, Sweden
8 July 2020, 22:47
If you go stuck on this, here is a hint: We know the only inputs are going to be either "sum" or a number, hence after we have checked weather the input is "sum" or not we can convert the String to an int :)
MCantu
Level 8, Texas, United States
24 November 2020, 02:15
Thank you for the advice it really helped. :) it finally worked.
Goff
Level 8, Berlin, Germany
9 October 2019, 22:51
Just a hint - Scanner class lets you check what data type is there before you read it. I did the task for doubles, it looks like while(input.hasNextDouble()) sum+=input.nextDouble();
Divakar Singh
Level 6, Muscat, Oman
23 April 2019, 11:50
Pls guide: what is wrong here
Roman
Level 41
24 April 2019, 06:09
If you need help, describe your question/issue in the HELP section at codegym.cc/help.
Hashirama
Level 26, Port-Harcourt, Nigeria
11 January 2019, 13:18
Guys please how do we extract 23 from the String "sum23"? Is it even possible?
Jeremiah Samuel Krakowski
Level 17, Austin, TX, United States
31 December 2018, 22:10
I overcomplicated this one, created an elaborate "isThisAnInteger" testing mechanism that would parse the text to an an integer only if it passed the test and add it lol. After reviewing other classmates work I see that this is just too much work for this, instead all you need to do is check if the string is "sum" and if not, then parse and add ;) HAHA Was fun to learn how to do a "type" check script tho! now i can make anything into anything ;)
Hashirama
Level 26, Port-Harcourt, Nigeria
11 January 2019, 13:17
Could you show us your code for the "isThisAnInteger" testing mechanism?
Jeremiah Samuel Krakowski
Level 17, Austin, TX, United States
12 January 2019, 21:31
I used this: https://learn-java-by-example.com/java/check-java-string-integer/
Hashirama
Level 26, Port-Harcourt, Nigeria
12 January 2019, 21:58
Thanks bro this was really helpful.