我用过
int num = Integer.parseInt(str)
将整数值写入字符串。我需要一个读取这些值的函数 从给定的字符串中计算它们的总和。
示例:输入 - “43 68 9 23 318” 输出 - 461
答案 0 :(得分:8)
String str = "43 68 9 23 318";
int num = Integer.parseInt(str)
你正在做的是,尝试一次解析完整的输入字符串,这将抛出NumberFormatException
。您需要先拆分它,然后对每个返回的String
执行求和。
将输入字符串拆分为whitespace
,然后解析每个数字并执行求和。
public static void main(String[] args) {
String input = "43 68 9 23 318";
String numbers[] = input.split("\\s+"); // Split the input string.
int sum = 0;
for (String number : numbers) { // loop through all the number in the string array
Integer n = Integer.parseInt(number); // parse each number
sum += n; // sum the numbers
}
System.out.println(sum); // print the result.
}
答案 1 :(得分:6)
在Java 8中,使用流
String input = "43 68 9 23 318";
String numbers[] = input.split("\\s+");
int[] nums = Arrays.stream(numbers.substring(1, numbers.length()-1).split(","))
.map(String::trim).mapToInt(Integer::parseInt).toArray();
int sum = IntStream.of(nums).sum();
System.out.println("The sum is " + sum);
答案 2 :(得分:3)
另一种Java 8方式:
public void test() {
String str = "43 68 9 23 318";
int sum = Pattern.compile(" ")
.splitAsStream(str)
.mapToInt(Integer::parseInt)
.sum();
System.out.println("sum:" + sum);
}
答案 3 :(得分:1)
如果您不想轻易解决问题,可以在现场进行:
public void test() {
String str = "43 68 9 23 318";
int sum = 0;
int value = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
switch (ch) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = value * 10 + (ch - '0');
break;
default:
sum += value;
value = 0;
break;
}
}
// Remember the last number.
sum += value;
value = 0;
System.out.println("sum:" + sum);
}
答案 4 :(得分:1)
您可以尝试跟随java-8。在这里,我们首先将 for (int j =0; j<[self.papikerCalender count]; j++) {
NSString * papikDate = [self.papikerCalender[j] valueForKey:@"strat_date"];
if ([currentDate isEqualToString:papikDate]) {
[self.rowData addObject:self.papikerCalender[j]];
}
}
//Once all data is added to self.rowData then add it to dictionary
[self.dataDictionay setObject:self.rowData forKey:currentDate];
}
拆分为String
,其中包含数字String[]
(即 String
),然后在["43", "68", "9", "23", "318"]
的帮助下我们可以将所有Arrays.stream
映射到Strings
,我们将Integer
,我们可以获得所有流IntStream
sum
}值。
Integer
<强>输出强>
public class Test {
public static void main(String[] args) {
String input = "43 68 9 23 318";
int sum = Arrays.stream(input.trim().split("\\s+"))
.mapToInt(Integer::parseInt)
.sum();
System.out.println(sum);
}
}
答案 5 :(得分:1)
这是功能
所需的工作副本 public class Main {
/*This function is static because we called it from static main*/
public static int returnSum(String str)
{
String []numbStr = str.split(" ");
int sum = 0;
for(String temp:numbStr)
{
sum += Integer.parseInt(temp);
}
return sum;
}
public static void main(String... args)
{
String str = "43 68 9 23 318";
/*Note that we can call only static functions from main when directly calls*/
int result = returnSum(str);
System.out.println(result);
}
}
答案 6 :(得分:1)
Java 8流在一行中
Arrays.stream(text.split(" ")).mapToInt(Integer::parseInt).sum();
答案 7 :(得分:0)
这不仅适用于包含用空格分隔的数字的字符串,而且适用于包含数字以外的字符的字符串,例如“ abc10jh20”-> 30
import java.util.Arrays.*;
public class HelloWorld{
//this code works also if the string contains alphabets not just spaces
static int count,sum;
public static void main(String []args){
String str="123 10 7";//getting input in form of string
int length=str.length();
for(int i=0;i<=length-1;i=i+count+1)
{
count=0;
if(Character.isDigit(str.charAt(i)))
{
for(int j=i;j<=length-1;j++)
{
if(Character.isDigit(str.charAt(j)))
{
count++;//to find the length of the number
}
else{
break;
}
}
String str2=str.substring(i,i+count);//selecting the substring
System.out.println("the parsing string "+str2);//displays strings getting parsed
int inc=count+1;
sum=sum+Integer.parseInt(str2);//finding sum
}
}
System.out.println("the sum is "+sum);
}
}