2014-12-04 11 views
0

我有一个程序,但我不知道具体是什么我的错误或如何解决它:问题是: 编写一个程序,要求用户输入一系列用逗号分隔的数字。 程序应计算并显示所有数字的总和。 例如,如果我输入4,5,6,7,总和显示应该是22使用逗号和字符串分词器不能做数字的总和

这是我到目前为止有:

import java.util.Scanner; 

public class SumAll { 

    public static void main(String[] args) { 

     Scanner keyboard = new Scanner(System.in); 

     String userNumber; 
     String sum = null; 

     //get numbers from user and store 
     System.out.println("Enter numbers seperated by coma's: "); 

     userNumber = keyboard.nextLine(); 
     String[] tokens = userNumber.split("[, ]"); 

     for (int i = 0; i < tokens.length; i++) { 
      sum = tokens.length[i++]; //showing me error here. Its written array required but int //found. 
     } 

     System.out.println("Sum is: " + sum); 
    } 
} 

非常感谢您的帮助。

+1

你不能用绳子做算术题,你必须先将他们转换为数字。另外,正如编译器告诉你的,要访问'tokens'的* i * -th元素,使用'tokens [i]'。最后,你在每次迭代中重新分配'sum'而不是增加它。 – 5gon12eder 2014-12-04 23:41:38

回答

3

总和应该是int

int sum = 0; 

您的循环应该是

for (int i = 0; i < tokens.length; i++) { 
     sum += Integer.parseInt(tokens[i]); 
} 
+1

你已经在每个迭代的'for'声明中增加'i'。 'sum = tokens [i ++];'不需要额外的增量。另外,在每次迭代中,您都重写'sum'的值。它应该是'sum + =令牌[i];' – 2014-12-04 23:45:26

+1

干得好。我收回我的downvote。 – 2014-12-04 23:47:39

+0

是的,那是我的错误。我完全忘记了解析。以为我应该使用String而不是int。非常感谢您的帮助。真的很感激它。 – Sevs24 2014-12-04 23:51:07

2

因为它应该是:

sum += Integer.parseInt(tokens[i]); 
+0

顺便说一句:我的例子没有错误处理 – 2014-12-04 23:42:38

2

有几件事情错的这一行码。

sum = tokens.length[i++]; 
  1. 你不能索引一样,数组的length。只需索引数组(参见下文)。
  2. for循环已经递增i。你不需要再做一次。
  3. 您需要将令牌转换为整数,然后才能将其添加到总和中。
  4. 您需要将新值添加到总和中,而不是替换旧的总和。

试试这个:

sum += Integer.parseInt(tokens[i]); 

您还需要做出sum整数。取而代之的

String sum = null; 

你需要

int sum = 0; 
1

我知道我2年多晚,但我开始学习Java不是很久以前,想分享我的解决方案。 :)我使用了StringTokenizer类。我希望这能帮助2017年以后的人。

import java.util.Scanner; 
import java.util.StringTokenizer; 

public class SumOfNumbersInString { 

    public static void main(String[] args) { 

     // Create a Scanner object 
     Scanner keyboard = new Scanner(System.in); 

     // Get user input 
     System.out.print("Enter a series of numbers seperated by commas\n> "); 
     String input = keyboard.nextLine(); 

     // Display sum by calling the getSum method 
     System.out.println("SUM: " + getSum(input)); 
    } 

    /** 
    * 
    * @param input with the format --> (#,#,#,#) 
    * @return sum of numbers in the input 
    */ 

    public static int getSum(String input) { 

     // Declare and initialize the sum accumulator variable 
     int sum = 0; 

     // Create a StringTokenizer object 
     // The string to be tokenized is passed as 1st parameter 
     // The "," that separates tokens/numbers is the 2nd parameter 
     StringTokenizer stringTokenizer = new StringTokenizer(input, ","); 

     // The hasMoreTokens method of the StringTokenizer class returns true if there are more tokens left in the string 
     // Otherwise, it returns false 
     while (stringTokenizer.hasMoreTokens()) { 
      // While the string has another token (number), parse the number to an integer and add its value to sum 
      sum += Integer.parseInt(stringTokenizer.nextToken()); 
     } 

     // Return sum's value to the method call 
     return sum; 
    } 

} 

输出

Enter a series of numbers seperated by commas 
> 4,5,6,7 
SUM: 22