【问题标题】:Java Split method in infinite loop confusion无限循环混乱中的Java拆分方法
【发布时间】:2018-12-15 16:31:29
【问题描述】:

我正在开始一个 Java 项目,其中用户进入一家宠物店,可以给宠物、查看所有宠物的列表、领养一只宠物,或者退出商店。由于一个命令输入需要多条信息,因此用户给出的每个命令都以空格分隔。我认为这会创建一个字符串数组,其中输入中的每个单词都是一个单独的元素。

import java.util.*;

public class Main {

static Scanner s = new Scanner(System.in);

public static void main(String[] args) {

    while(true) {

        System.out.println("Welcome to James' Pet Shop.");
        System.out.println("Commands:\n give [type (Str)] [name (Str)] [age (in months, int)]\n list\n adopt [type (Str)] [name (Str)] [age (in months, int)]");

        String input = s.next();
        String[] inputs = input.split(" ");

        if (inputs[0].equals("give")) {



        } else if (inputs[0].equals("list")) {



        } else if (inputs[0].equals("adopt")) {

        } else if (inputs[0].equals("bye")) {

            System.out.println("Thank you for stopping bye!");
            break;

        } else {

            System.out.println("Error: please enter an appropriate command to continue");

        }

    }

}

public static void list() {

}

public static void adopt() {

}

}

但是,当我运行代码并输入多个单词(由空格分隔)时,它能够测试元素 0 是否是某个命令并遵循该条件(即,如果第一个单词是“give ," "list," 等,它将沿着该路径返回,然后循环返回并重新开始,直到用户说“再见”),但是,比如说,如果你要打印出数组中的第二个单词索引 1,你作为输入输入,它给了我错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常:Main.main(Main.java:30)处的1。我发现的奇怪的事情是,如果我通过再次打印第一个单词索引 0 来绕过此错误,并且我输入了多个单词,它会将输入中的每个元素视为元素 0,并在循环中循环多次给定的元素/输入中输入的单词。我很困惑……

例如。 1

    String input = s.next();
    String[] inputs = input.split(" ");

    if (inputs[0].equals("give")) {

            //if you type give and another word
            //instead of printing out the latter, it gives an error
            System.out.println(input[1]);

    } else if (inputs[0].equals("list")) {

例如。 2

    String input = s.next();
    String[] inputs = input.split(" ");

    if (inputs[0].equals("give")) {

            //instead of printing the first element and then taking more input
            //if the first word is give, and 
            //overwriting the String array inputs, it instead prints out 
            //your first word, then cycles back through and treats the 
            //subsequent words as input 0.
            //if you type "give giver", it will first print the beginning instructions,
            //then print give because it satisfies that conditional,
            //and then cycle back through the instructions and print 
            //Error: please enter an appropriate command to continue
            System.out.println(input[0]);

    } else if (inputs[0].equals("list")) {

【问题讨论】:

  • 使用nextLine() 而不是next()。 next() 只扫描一个令牌。 (在这种情况下是一个词)
  • s.next() 在第一个空格处停止读取。

标签: java loops exception split


【解决方案1】:

这是因为您正在使用函数 s.next();在这里,它只返回单词直到遇到空格。因此,只输入您的第一个命令。

为了解决这个问题,使用下面的代码:-

System.out.println("Commands:\n give [type (Str)] [name (Str)] [age (in months, int)]\n list\n adopt [type (Str)] [name (Str)] [age (in months, int)]"); 
String[] inputs = sc.nextLine().split(" ");

这会奏效。请记住,next() 用于输入一个单词,而 nextLine() 用于输入多个单词。

【讨论】:

  • next() 不知道“单词”,它将空格识别为分隔符。
猜你喜欢
  • 2019-04-24
  • 2013-04-12
  • 1970-01-01
  • 1970-01-01
  • 2023-01-25
  • 2013-03-18
  • 1970-01-01
  • 2014-03-13
相关资源
最近更新 更多