Read numbers from keyboard and calculate their total until the user enters the word «total». Display to the screen the total. import java.io.BufferedReader; import java.io.InputStreamReader;

public class Solution { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); for(int i=0;;i++ ) { int n = Integer.parseInt(br.readLine()); if() }

}

} how do I add a condition where user enters a string(total) while its taking int input?

asked 04 Jul '15, 12:22

mrymmrhn's gravatar image

mrymmrhn
11
accept rate: 0%


for example, you're able to use coditional operator if with strings like:

if ( stringName.equals("stringToCompareWith") )
{
  //do something here
}

good luck :)

link

answered 08 Jul '15, 00:15

Greg%20Neginskiy's gravatar image

Greg Neginskiy
11
accept rate: 0%

edited 08 Jul '15, 00:16

Thank you so much! I've done this task, it works just fine at my end but the server is not passing the test. :/ int total = 0; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String in = br.readLine(); while(!in.equalsIgnoreCase("total")){ try{ total = total + Integer.parseInt(in); }catch(NumberFormatException nfe){ total = total; } in = br.readLine(); } System.out.println(total);

(28 Jul '15, 00:55) mrymmrhn

In order to get your solution right you need to use in the program word "total" translated into Russian ("total"-> "сумма"). Only after this amend I have passed the test.

link

answered 30 Mar '16, 07:07

ILIA%20KISIL's gravatar image

ILIA KISIL
124
accept rate: 0%

my program works perfectly fine but the server cannot compile it.... why???

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            int total=0;
            String x;

        do{
             x = reader.readLine();

            try{
                if(x.equals("total")){
                    System.out.println(total);
                }
                else{
                    int num = Integer.parseInt(x);
                    total += num;
                }

            }catch (Exception e){
                System.out.print("Enter a number!");
            }  
        } while(!x.equals("total"));
link

answered 26 Feb '17, 03:51

Dmitry%20Sky's gravatar image

Dmitry Sky
113
accept rate: 0%

Passed test:

  package com.javarush.test.level05.lesson12.home05;

/* Read numbers from keyboard and calculate their total
Read numbers from keyboard and calculate their total until the user enters the word «total». Display to the screen the total.
*/

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Solution
{
    public static void main(String[] args) throws Exception
    {
        BufferedReader scan = new BufferedReader(new InputStreamReader(System.in));
        int total = 0;
        while(true){
            String s = scan.readLine();
            if(s.equals("сумма")){
                System.out.println(total);
                break;
            }
            total += Integer.parseInt(s);
        }
    }
}
link

answered 18 May '17, 17:22

Igor's gravatar image

Igor
112
accept rate: 0%

edited 18 May '17, 17:26

I couldn't pass this test until I replaced "if(s.equals("total")" to "if(s.equals("сумма")".

сумма means total in Russian.

link

answered 12 Jan, 14:15

Vivi's gravatar image

Vivi
1
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×30
×21
×6

Asked: 04 Jul '15, 12:22

Seen: 1,534 times

Last updated: 12 Jan, 14:15