【问题标题】:Looping through a command line string to print out words循环通过命令行字符串打印出单词
【发布时间】:2014-03-03 02:11:34
【问题描述】:

我必须在命令行中循环一个字符串,例如 (java Lab2 "HELLO WORLD") 并使用循环打印出子字符串 [HELLO] 和 [WORLD]。这是我目前所拥有的。

public static void main(String argv[])
{
    if (argv.length() == 0)
    {
        System.out.println("Type in string");
    }

    String Input = argv[0];
    String sub;

    for (int end = 0; end < Input.length(); end++)
    {
        end = Input.indexOf(' ');
        sub = Input.substring(0, end);
        System.out.println("sub = [" + sub + "]");


        if(end > 0)
        {   
            int start = end +1;
            end = Input.indexOf(' ');  
            sub = Input.substring(start,end);
            System.out.println("sub = [" + sub + "]");
        }
    }
}
}

Input 中的第一个单词会很好地打印出来。然后在那之后我要么得到一个无限循环,要么我将被抛出一个索引数组越界异常。例外是指 for 循环中的 if 语句。

【问题讨论】:

    标签: java string for-loop command-line-arguments


    【解决方案1】:

    这是一种方法:

    if (argv.length == 0) // Length it not a method
    {
        System.out.println("Type in string");
        return; // the code should be stopped when it happens
    }
    
    String input = argv[0];// Avoid use this kind of name
    
    int idx;
    int lastIndex = 0;
    
    // indexOf returns the index of the space
    while ((idx = input.indexOf(' ', lastIndex)) != -1)
    {
        System.out.println("[" + input.substring(lastIndex, idx) + "]");
        lastIndex = idx + 1;
    }
    
    System.out.println("[" + input.substring(lastIndex, input.length()) + "]");
    

    我使用indexOf 来知道字符串中每个空格的索引。最后一行是必需的,因为它找不到最后一个单词。 一种解决方法是:

    if (argv.length == 0) // Length it not a method
    
    {
        System.out.println("Type in string");
        return; // the code should be stopped when it happens
    }
    
    String input = argv[0] + " ";// Avoid use this kind of name
    
    int idx;
    int lastIndex = 0;
    
    // indexOf returns the index of the space
    while ((idx = input.indexOf(' ', lastIndex)) != -1)
    {
        System.out.println("[" + input.substring(lastIndex, idx) + "]");
        lastIndex = idx + 1;
    }
    

    我想你注意到input 行中的+ " ";

    【讨论】:

    • 有没有办法在循环中抛出 if 语句来打印出最后一个单词?
    • 什么?我不明白你
    • 好吧,当它到达最后一个词时,while 完成,所以最后一个词在lastIndexinput.length()。只需在一段时间后添加if
    【解决方案2】:

    看来你试图自己拆分字符串过于复杂了。 大多数编程语言(如 Java)都有一个 split() 函数,它将一个字符串拆分为一个数组,它可以看到某个子字符串(在您的情况下是 " ")。然后,您可以使用 foreach 语句来遍历此数组。在 Java 中,foreach 是这样完成的:

    for (String current : String[] array) { }
    

    对于拆分,你需要这样做:

    String[] elements = Input.split(" ");
    

    总的来说,您可以执行以下操作:

    String[] elements = Input.split(" ");
    
    for (String sub : elements) {
        System.out.println("sub = [" + sub + "]");
    }
    

    【讨论】:

    • 我必须这样做。我必须完成更多的逻辑问题......如果可以的话,我肯定会节省自己的时间和空间。
    【解决方案3】:

    在行中,

    int start = end + 1;

    如果(结束> 0){

    如果 start 是 6,那么 end 将是 5.. 那么 Input.sub string(6,5) 肯定是错误的,因为开始索引应该总是小于结束索引,反之亦然

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2012-07-07
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多