Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在具有两个对象的数组中,如何在(1)处获得越界异常?_Java - Fatal编程技术网

Java 在具有两个对象的数组中,如何在(1)处获得越界异常?

Java 在具有两个对象的数组中,如何在(1)处获得越界异常?,java,Java,我得到了一个“ArrayIndexOutOfBoundsException:1”错误,但只有在输入“q”退出程序时才会出现 import java.util.Scanner; public class ParseStrings { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); Scanner inSS = null; String userInfo = "";

我得到了一个“ArrayIndexOutOfBoundsException:1”错误,但只有在输入“q”退出程序时才会出现

import java.util.Scanner;

public class ParseStrings {
   public static void main(String[] args) {
  Scanner scnr = new Scanner(System.in);
  Scanner  inSS = null;
  String userInfo = "";
  String firstWord = "";
  String secondWord = "";
  String[] names = {"",""};


  System.out.println("Enter input string: ");
  userInfo = scnr.nextLine();

  boolean isFound = userInfo.contains(",");

  if (isFound == false) {
        System.out.println("Error: No comma in string");
        System.out.println("Enter input string: ");

        userInfo = scnr.nextLine();
        isFound = userInfo.contains(",");
     }

     while (isFound) {

        names = userInfo.split(",");

        firstWord = names[0];
        secondWord = names[1]; //<----- LINE 33

        System.out.println("First word: " + firstWord.trim());
        System.out.println("Second word: " + secondWord.trim());
        System.out.println("");
        System.out.println("");

        System.out.println("Enter input string: ");
        userInfo = scnr.nextLine();

     }

      if (userInfo == "q") {
           System.exit(0);
      }
   return;
   }
}
import java.util.Scanner;
公共类解析字符串{
公共静态void main(字符串[]args){
扫描仪scnr=新扫描仪(System.in);
扫描仪inSS=null;
字符串userInfo=“”;
字符串firstWord=“”;
字符串secondWord=“”;
字符串[]名称={“”“”};
System.out.println(“输入字符串:”);
userInfo=scnr.nextLine();
布尔值isFound=userInfo.contains(“,”);
if(isFound==false){
System.out.println(“错误:字符串中没有逗号”);
System.out.println(“输入字符串:”);
userInfo=scnr.nextLine();
isFound=userInfo.contains(“,”);
}
while(isFound){
name=userInfo.split(“,”);
firstWord=名称[0];
secondWord=名称[1];//此处:

names = userInfo.split(",");
userInfo
仅包含
q
时,
names
将是
{“q”}
,一个仅包含一个元素的数组

因此,
names[1]
,即
names
的第二个元素不存在

因此出现了
ArrayIndexOutOfBoundsException

您可以像这样重构它:

while (isFound) {
  if (userInfo.equals("q")) { // btw, notice the 'equals' instead of ==
    System.exit(0);
  }
  names = userInfo.split(",");
  firstWord = names[0];
  secondWord = names[1]; 
  [...]
在这里:

names = userInfo.split(",");
userInfo
仅包含
q
时,
names
将是
{“q”}
,一个仅包含一个元素的数组

因此,
names[1]
,即
names
的第二个元素不存在

因此出现了
ArrayIndexOutOfBoundsException

您可以像这样重构它:

while (isFound) {
  if (userInfo.equals("q")) { // btw, notice the 'equals' instead of ==
    System.exit(0);
  }
  names = userInfo.split(",");
  firstWord = names[0];
  secondWord = names[1]; 
  [...]

您应该在while代码块的末尾添加:
isFound=userInfo.contains(“,”);


问题似乎在于while循环永远不会停止。

您应该在while代码块的末尾添加以下内容:
isFound=userInfo.contains(“,”;


问题似乎在于while循环永远不会停止。

因为
名称[1]
在这种情况下不存在。这很容易解决。试想一下,如果
1
超出范围,数组显然没有两个元素,而是空的或只有一个元素。不确定为什么您认为它有两个元素。您可以拆分
并获取结果数组。如果您的输入不包含逗号,如
“hello world”
,生成的数组将只有一个元素。如果输入q,while循环应该停止执行是我的思维过程。然后检查循环条件。是否更新过它?因为
名称[1]
在这种情况下不存在。这很容易解决。试想一下,如果
1
超出范围,数组显然没有两个元素,而是空的或只有一个元素。不确定为什么您认为它有两个元素。您可以拆分
并获取结果数组。如果您的输入不包含逗号,如
“你好,世界”
,生成的数组将只有一个元素。如果输入q,while循环应该停止执行是我的思考过程。然后检查你的循环条件。你是否更新过它?谢谢。我认为一旦输入q,isfind将等于false并继续超出该循环。谢谢。我认为一旦输入q,isfind将d等于false,并继续超出该循环。