public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader qwe = new BufferedReader(new InputStreamReader(System.in));
int a = 0;
while (true) {
String s = qwe.readLine();
if (s.equals("сумма")) break;
int q = Integer.parseInt(s);
a+=q;
}
System.out.println(a);
}
}
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader qwe = new BufferedReader(new InputStreamReader(System.in));
int a = 0;
while (true) {
String s = qwe.readLine();
int q = Integer.parseInt(s);
a+=q;
if (s.equals("сумма")) break;
}
System.out.println(a);
}
}
в первом случае все ок, во втором выдает ошибку:
Exception in thread "main" java.lang.NumberFormatException: For input string: "сумма"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at com.javarush.task.task05.task0529.Solution.main(Solution.java:16)
почему сравнение строки с шаблоном после преобразования строки в число не проходит? String s после преобразования перестает существовать?package com.javarush.task.task05.task0529;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/*
Консоль-копилка
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader qwe = new BufferedReader(new InputStreamReader(System.in));
int a = 0;
while (true) {
String s = qwe.readLine();
if (s.equals("сумма")) break;
int q = Integer.parseInt(s);
a+=q;
}
System.out.println(a);
}
}