使用字符串标记器忽略括号?

发布于 2024-09-28 20:45:08 字数 368 浏览 5 评论 0原文

我的输入如下所示:(0 0 0)
我想忽略括号,只将数字(在本例中为 0)添加到数组列表中。
我正在使用扫描仪从文件中读取数据,这就是我到目前为止所拥有的

    transitionInput = data.nextLine();
    st = new StringTokenizer(transitionInput,"()", true);
    while (st.hasMoreTokens())
    {
        transition.add(st.nextToken(","));
    }

但是,输出看起来像这样 [(0 0 0)]
我想忽略括号

I have an input that looks like: (0 0 0)
I would like to ignore the parenthesis and only add the numbers, in this case 0, to an arraylist.
I am using scanner to read from a file and this is what I have so far

    transitionInput = data.nextLine();
    st = new StringTokenizer(transitionInput,"()", true);
    while (st.hasMoreTokens())
    {
        transition.add(st.nextToken(","));
    }

However, the output looks like this [(0 0 0)]
I would like to ignore the parentheses

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

得不到的就毁灭 2024-10-05 20:45:08

怎么样

 for(String number: transitionInput
       .replace('(', ' ').replace(')', ' ').split("\\s+")){
    transition.add(number);
 }

How about

 for(String number: transitionInput
       .replace('(', ' ').replace(')', ' ').split("\\s+")){
    transition.add(number);
 }
时光倒影 2024-10-05 20:45:08

您首先使用 () 作为分隔符,然后切换到 ,,但您是在提取第一个标记(括号之间的文本)之前切换的。

您可能想要做的是:

transitionInput = data.nextLine();
st = new StringTokenizer(transitionInput,"()", false);
if (st.hasMoreTokens())
{
    String chunk = st.nextToken();
    st = new StringTokenizer(chunk, ",");
    while (st.hasMoreTokens())
    {
        transition.add(st.nextToken());
    }
}

此代码假定表达式始终以括号开头和结尾。如果是这种情况,您也可以使用 String.substring() 手动删除它们。另外,您可能需要考虑使用 String.split() 进行实际的拆分:

String transitionInput = data.nextLine();
transitionInput = transitionInput.substring(1, transitionInput.length() - 1);
for (String s : transitionInput.split(","))
    transition.add(s);

请注意,这两个示例都假设使用逗号作为分隔符,如示例代码中所示(尽管您的文本问题另有说明)

You are first using () as delimiters, then switching to ,, but you are switching before extracting the first token (the text between parentheses).

What you probably intended to do is this:

transitionInput = data.nextLine();
st = new StringTokenizer(transitionInput,"()", false);
if (st.hasMoreTokens())
{
    String chunk = st.nextToken();
    st = new StringTokenizer(chunk, ",");
    while (st.hasMoreTokens())
    {
        transition.add(st.nextToken());
    }
}

This code assumes that the expression always starts and ends with parentheses. If this is the case, you may as well remove them manually using String.substring(). Also, you may want to consider using String.split() to do the actual splitting:

String transitionInput = data.nextLine();
transitionInput = transitionInput.substring(1, transitionInput.length() - 1);
for (String s : transitionInput.split(","))
    transition.add(s);

Note that both examples assume that commas are used as separators, as in your sample code (although the text of your question says otherwise)

嗫嚅 2024-10-05 20:45:08

另一种变体

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


public class simple {


 public static void main(String[] args)
 {

  List transition = new ArrayList();
     String transitionInput="(0 0 0)";
     transition = Arrays.asList((transitionInput.substring(1,transitionInput.length()-1)).split("\\s+"));
     System.out.println(transition);
 }
}

输出:[0, 0, 0]

Another variant

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


public class simple {


 public static void main(String[] args)
 {

  List transition = new ArrayList();
     String transitionInput="(0 0 0)";
     transition = Arrays.asList((transitionInput.substring(1,transitionInput.length()-1)).split("\\s+"));
     System.out.println(transition);
 }
}

Output : [0, 0, 0]

时光无声 2024-10-05 20:45:08
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


public class simple {


 public static void main(String[] args)
 {

  List transition = new ArrayList();
     String transitionInput="(0 0 0)";
     transition = Arrays.asList((transitionInput.substring(1,transitionInput.length()-1)).split("\\s+"));
     System.out.println(transition);
 }
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;


public class simple {


 public static void main(String[] args)
 {

  List transition = new ArrayList();
     String transitionInput="(0 0 0)";
     transition = Arrays.asList((transitionInput.substring(1,transitionInput.length()-1)).split("\\s+"));
     System.out.println(transition);
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文