CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Start learning now
  • All questions
Bartek J.
Level 13
Zielona Góra
  • 20.08.2019
  • 837views
  • 2comments

Problem with adding

Question about the task Console-based piggy bank
Java Syntax,  Level 5,  Lesson 12
Resolved

Use the keyboard to enter numbers and calculate their sum until the user enters the word "sum".
Display the resulting sum on the screen.

Hint: to read from the keyboard until the string 'exit' is input, do this:

BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
while (true)
{
	String s = buffer.readLine();
	if (s.equals("exit"))
		break;
}
Requirements:
  • The program must read data from the keyboard.
  • The program should stop reading data from the keyboard after the user enters the word "sum" in lowercase letters and presses Enter.
  • The program should work correctly if the user enters one number and the word "sum".
  • The program should work correctly if the user enters two numbers and the word "sum".
  • The program should work correctly if the user enters more than two numbers and the word "sum".
  • The program should display text on the screen.
package com.codegym.task.task05.task0529; import java.io.BufferedReader; import java.io.InputStreamReader; /* Console-based piggy bank */ public class Solution { public static void main(String[] args) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int calcNumbers=0; while(true){ // Integer.parseInt(reader.readLine()); calcNumbers = calcNumbers += Integer.parseInt(reader.readLine()); if(reader.readLine().equals("sum")){ System.out.println(calcNumbers); break; } } } }
0
Comments (2)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Gabriella LaPlace
Level 16 , Basseterre, Saint Kitts and Nevis
23 August 2019, 15:33
package com.codegym.task.task05.task0529; import java.io.BufferedReader; import java.io.InputStreamReader; /* Console-based piggy bank Use the keyboard to enter numbers and calculate their sum until the user enters the word "sum". Display the resulting sum on the screen. Hint: to read from the keyboard until the string 'exit' is input, do this: BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); while (true) { String s = buffer.readLine(); if (s.equals("sum")) break; } Requirements: 1. The program must read data from the keyboard. 2. The program should stop reading data from the keyboard after the user enters the word "sum" in lowercase letters and presses Enter. 3. The program should work correctly if the user enters one number and the word "sum". 4. The program should work correctly if the user enters two numbers and the word "sum". 5. The program should work correctly if the user enters more than two numbers and the word "sum". 6. The program should display text on the screen. */ public class Solution { public static void main(String[] args) throws Exception { //write your code here int sum = 0; int number; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { String s = reader.readLine(); if (s.equals("sum")) break; number = Integer.parseInt(s); sum += number; } System.out.println(sum); } }
+1
GCPix Backend Developer
20 August 2019, 09:42
Could it be this line? calcNumbers = calcNumbers += Integer.parseInt(reader.readLine()); I can't see anything else but not sure how this would only pick up every second input.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.