2016-01-22 58 views
1

的StringTokenizer的定界符我寫一個程序,它用逗號隔開,總人數的合計數值列表的輸入。例如,我有字符串「10,20,30,40,50」使用字符在Java中

我想從字符串中分別提取每個數字「10」「20」「30」「40」「50」數字列表的總和。

我找到了一個解決方案,但是,我發現我的代碼有點亂,當我回去看看吧,我會在一分鐘內不少「WTF」的。

所以,

我想知道是否有寫下面一行更好的辦法:

StringTokenizer inputTokenizer = new StringTokenizer(input, "- ,\\n\\r\\t\\b\\fabcdefghijklmnopqrstuvwxyz"); 

我的目標是我想要的程序使用的每個字符是一個非數字作爲StringTokenizer的分隔符。 因此,例如串"11abc33"應該分成"11""33"

這裏是什麼,我想出了

public static void main(String[] args) { 
    do { 
     //Prompts user to enter a series of numbers and stores it in the String "input" 
     String input = JOptionPane.showInputDialog("Enter a series of numbers separated by commas."); 

     //total stores the sum of each number entered 
     int total = 0; 

     if ((input != null)) //checks if the user didn't cancel or quit the program 
     { 

      //sets every alphabetical character in the input String to lowercase 
      input = input.toLowerCase(); 

      //creates a StringTokenizer that uses commas and white spaces as delimiters 
      StringTokenizer inputTokenizer = new StringTokenizer(input, "- ,\\n\\r\\t\\b\\fabcdefghijklmnopqrstuvwxyz"); 

      //sums the total of each number entry 
      while (inputTokenizer.hasMoreTokens()) { 
       total = total + Integer.parseInt(inputTokenizer.nextToken()); 
      } 
     } else { 
      //exit the program because the user hit cancel or exit 
      System.exit(0); 
     } 

     //display the sum of the total number entries 
     JOptionPane.showMessageDialog(null, "Total: " + total); 

    } while (true); 
} 
+0

所以你想分割一個輸入字符串使用空格或數字之間的逗號? –

+0

是或任何非數字字符 –

+0

如果您只想分割逗號,則分隔符看起來很複雜。如果你確實想要使用所有的分隔符,你可能想要檢查一下String.split()方法,並使用regex來表達所有那些更清晰的語法。 –

回答

2

沒有,你想要做什麼更多的只是StringTokenizer構造函數或工廠方法。如果你必須有一個StringTokenizer話,我不認爲有一個更好的方式來得到一個,但因爲你可以在分隔符字符串調整字符。

你寫

我的目標是我想要的程序使用的每個字符是一個非數字作爲StringTokenizer的分隔符。

但這似乎有點狹隘。這似乎是最重要的事情將是令牌,不標記生成器,如果這確實是你的話,那麼正則表達式爲基礎的String.split()可能提供一個滿意的選擇:

for (String token : input.split("[^0-9]+")) { 
    int i = Integer.parseInt(token); 

    // ... 
} 

這需要你從字面上你字,那你要考慮的一切這是非數字作爲分隔符。

還有其他基於正則表達式的解決方案,例如使用匹配一個數字的模式來遍歷字符串Matcher.find()

+0

這不會僅僅分裂數字0-9?如果我想將0號分爲無窮大? –

+0

@TommySaechao,不,它會分割任何長度的數字。 0-9只是表示你的數字只包含從0到9的字符。並且+表示從一個字符到無限長度的長度。這個答案是最好的。 –

+0

@TommySaechao,我建議拆分的正則表達式字符串匹配任何不是('[^]')十進制數字('0-9')的一個或多個字符('+')的任何序列。與該正則表達式匹配的每個最大運行時間都將作爲分隔符,因此所產生的拆分字符串將保留在之間:您想要的數字運行。 –

4

注意,StringTokenizer的分離數字和使用空格在正常模式下子我的源代碼。我的意思是使用此構造StringTokenizer(string)

但可以使用strTokenizer

StringTokenizer(String str, String delim) 

,您可以使用","作爲DELIM參數使用另一個構造函數不同的號碼和所有子將根據","在此分離,鎖定例如:

String numbers = "10,20,30,40,50,60,70"; 
    StringTokenizer t = new StringTokenizer(numbers, ","); 
    int sum=0; 
    while (t.hasMoreTokens()) { 
     sum+=Integer.parseInt(t.nextToken()); 
    } 
    System.out.println("sum: " + sum); 

你也可以這樣做只是使用split(String regex)方法String
這裏是你的一個例子和解決方案。

String numbers = "10,20,30,40,50,60,70";// all numbers 

    String[] separated_numbers = numbers.split(",");// separate them by comma 

    // calculating sum 
    int sum = 0; 
    for (String number : separated_numbers) { 
     sum += Integer.parseInt(number); 
    } 
    // print sum 
    System.out.println("sum: " + sum); 
+0

我不知道爲什麼這個答案落選。這正是我如何解決這個問題。 –

+1

我低估了,因爲如果你深入研究OP問題,有一個矛盾,他想得到所有數字的總和,有什麼之間的差額,輸入可以是'qsdqskdqskdqsd254131qdsdgkljf' –

+0

@AnthonyRaymond鎖在問題,他不想計算所有數字的總和,請參閱:「我想從字符串中分別提取每個數字,」10「」20「」30「」40「」50「,並找出數字列表的總和。 「 –

1

你可以用做正則表達式

do { 
    String input = JOptionPane.showInputDialog("Enter a series of numbers separated by commas."); 

    int total = 0; 

    if ((input != null)) 
    { 
     Matcher m = Pattern.compile("(-?[0-9]+.[0-9]*)+").matcher(input); 
     // when using regex, the first group is always the full text, so we skip it. 
     for (int i = 1; i<=matcher.groupCount(); i++) { 
      total = total + Integer.parseInt(matcher.group(i)); 
     } 
    } else { 
     System.exit(0); 
    } 

    JOptionPane.showMessageDialog(null, "Total: " + total); 

} while (true); 
+0

這個解決方案應該正確地處理所有數字'-12','12','12.34'這兩個工作 –

0

您可以將所有非數字字符替換爲一個單個字符,然後使用split方法獲取所有數字數組。

public static void main(String[] args) { 
    do { 
     //Prompts user to enter a series of numbers and stores it in the String "input" 
     String input = JOptionPane.showInputDialog("Enter a series of numbers separated by commas."); 

     //total stores the sum of each number entered 
     int total = 0; 

     if ((input != null)) //checks if the user didn't cancel or quit the program 
     { 
      String[] characterTokens = input.split("[^0-9]+"); 
      for (String characterToken : characterTokens) { 
       input.replace(characterToken, ","); 
      } 
      String[] numberTokens = input.split(","); 
      for (String numberToken: numberTokens) { 
       total += Integer.parseInt(numberToken); 
      } 
     } else { 
      //exit the program because the user hit cancel or exit 
      System.exit(0); 
     } 

     //display the sum of the total number entries 
     JOptionPane.showMessageDialog(null, "Total: " + total); 

    } while (true); 
}