Java 如何解析这个输入?

Java 如何解析这个输入?,java,Java,我正在尝试创建一个计算器,该计算器采用4种不同格式的字符串: input: add 1 2 13 5 together => 1 + 2 + 13 + 5 = 21 input: add 9 to 10 => 9 + 10 = 19 input: subtract 5 from 13 => 13 - 5 = 8 input: bye => end program 我的代码设置如下: import java.util

我正在尝试创建一个计算器,该计算器采用4种不同格式的字符串:

input: add 1 2 13 5 together
       => 1 + 2 + 13 + 5 = 21
input: add 9 to 10
       => 9 + 10 = 19
input: subtract 5 from 13
       => 13 - 5 = 8
input: bye
       => end program
我的代码设置如下:

import java.util.*;

    class Calculator {
      public static void main(String[] args) {
        System.out.println("yes>" )
        Scanner zip = new Scanner(System.in);
        String input = zip.nextLine(); 

        if (input.equals("bye")) 
           System.exit(0); 
        else if (input.substring(0, 1) == "a") //assume first cmd is add
           (integer.ParseInt(input.subtring(3, BLANK) //extract first number
我的问题是我不知道字符串中数字的索引。我想这样做:

import java.util.*;

    class Calculator {
      public static void main(String[] args) {
        System.out.println("yes>" )
        Scanner zip = new Scanner(System.in);
        String input = zip.nextLine(); 

        if (input.equals("bye")) 
           System.exit(0); 
        else if (input.substring(0, 1) == "a") //assume first cmd is add
           (integer.ParseInt(input.subtring(3, BLANK) //extract first number
如果(input.substring(0,3)=“add”)=>下一个数字将是一个数字。解析整数。子字符串(3,空白)。空格应该是空格字符的索引。然后,需要将该数字放入变量中,程序需要检查所有其他整数

我对如何处理这个问题有点困惑。如果有500个数字需要加(或减)怎么办?有没有一种递归的方法?这个程序应该使用我读过的正则表达式,但我不知道如何实现

任何帮助都将不胜感激。提前感谢

您可以使用将行拆分为单独的标记,然后检查第一个是否为命令,并验证其他是否为数字

简短示例:

String[] tokens = input.split(" ");
if (tokens.length > 0) { // there are tokens
    if (tokens[0].equals("add")) { // valid command - add
        // parse tokens and make sure they are numbers
使用创建的并使用以下方法:
hasNext
next
(用于读取“TO”、“SUBTRACT”等)、
hasNextInt
nextInt
(用于读取NUM)。将
输入提供给扫描仪后,不得对其进行任何操作。确保在
hasNext
之前尝试
hasNext
,因为如果下一个单词与整数匹配,
hasNext
将返回true:)

以下是一些有助于构建解析器的注释:

  • “加上”NUM1 NUMN+“一起”;如果第一个NUM1后跟另一个NUMN,则此格式为。继续读取NUMN,直到没有更多——可以放入ArrayList供以后使用,也可以立即计算并发出输出。然后应该有一个“在一起”的结尾

  • 在“NUM2”之后添加“NUM1”;仅当NUM1后跟“TO”时

  • “NUM2”减去“NUM1”

  • “再见”

  • 可能是这样开始的:

    String action = scanner.next();
    if (action.equals("bye")) {
       ...
    } else if (action.equals("add")) {
       if (scanner.hasNextInt()) {
         int num1 = scanner.nextInt();
         if (scanner.hasNextInt()) {
            /* ADD NUM1 NUMN+ TOGETHER */
            int total = num1;
            System.out.print("" + num1);
            while (/* has more int */) {
               int numN = /* read int */
               System.out.print(" + " + numN);
               /* increase total */
            }
            /* display results */
            /* now expecting "together" */
         } else {
           /* ADD NUM TO NUM */
           if (!scanner.hasNext()
               || !scanner.next().equals("to")) {
             /* expecting "to" */
           } else {
             /* get next number, add, show results */
           }
         }
       } else {
          /* expecting NUM */
       }
    } else ...
    

    快乐的家庭作业。

    我会使用正则表达式——你可以用非常确定的模式捕捉关键词和数字。我会写代码,但我不知道Java的方式……这就是我想知道的。我的语法太离谱了