Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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.lang.ArrayIndexOutOfBoundsException和If逗号后的条目不是整数_Java_String_If Statement_Indexoutofboundsexception - Fatal编程技术网

java.lang.ArrayIndexOutOfBoundsException和If逗号后的条目不是整数

java.lang.ArrayIndexOutOfBoundsException和If逗号后的条目不是整数,java,string,if-statement,indexoutofboundsexception,Java,String,If Statement,Indexoutofboundsexception,我的代码中不断出现错误:java.lang.ArrayIndexOutOfBoundsException:1。这是我学习java的第一个学期,非常感谢您的帮助。还有,一石二鸟。我的一个if语句有问题 如果逗号后的条目不是整数: 输出:“错误:逗号后面没有整数。” 提前谢谢 import java.util.Scanner; import java.util.ArrayList; import java.io.PrintWriter; import java.io.StringWriter; p

我的代码中不断出现错误:java.lang.ArrayIndexOutOfBoundsException:1。这是我学习java的第一个学期,非常感谢您的帮助。还有,一石二鸟。我的一个if语句有问题

如果逗号后的条目不是整数:

输出:“错误:逗号后面没有整数。”

提前谢谢

import java.util.Scanner;
import java.util.ArrayList;
import java.io.PrintWriter;
import java.io.StringWriter;

public class DataVisualizer {
   public static void main(String[] args) {
      Scanner input = new Scanner(System.in);      
      Scanner inSS = null;
      String title = "";
      String column1header = "";
      String column2header = "";
      String dataString = "";
      String dataPoint = "";
      String dataInt = "";
      boolean inputDone = false;
      char comma = ',';

      System.out.println("Enter a title for the data: ");
      title = input.nextLine();

      System.out.println("You entered: " + title);
      System.out.println("");

      System.out.println("Enter the column 1 header: ");
      column1header = input.nextLine();

      System.out.println("You entered: " + column1header);
      System.out.println("");

      System.out.println("Enter the column 2 header: ");
      column2header = input.nextLine();

      System.out.println("You entered: " + column2header);
      System.out.println("");

      ArrayList<String> string = new ArrayList<String>();
      ArrayList<Integer> integer = new ArrayList<Integer>();

      System.out.println("Enter a data point (-1 to stop input): ");     

      while (!inputDone) {

          dataPoint = input.nextLine();
          inSS = new Scanner(dataPoint);

          if (dataPoint.equals("-1")) {
              inputDone = true;
              break;
          }

          if (!dataPoint.contains(",")) {
              System.out.println("Error: No comma in string.");

          }

 // Here is the problem I am running with my if statement.            

          if (dataPoint.indexOf(",") != dataPoint.lastIndexOf(",")) {
              System.out.println("Error: Too many commas in input.");

          }

          if (Character.isDigit(dataPoint.charAt(-1))) {
              System.out.println("Error: Comma not followed by an integer.");
              System.out.println("");
              System.out.println("Enter a data point (-1 to stop input): ");
          }


          else {

// This is where Im getting my error              

              String[] items = dataPoint.split(",");

              for (int i = 0; i < items.length; ++i){
                  dataString = items[0];
                  dataInt = items[1];
                }

              Integer dataInteger = Integer.valueOf(dataInt);
              System.out.println("Data string: " + dataString);

              System.out.println("Data integer: " + dataInteger);

              string.add(dataString);
              integer.add(dataInteger);
            }   
            System.out.println("");
            System.out.println("Enter a data point (-1 to stop input): ");

      }
      return;
   }     
}
import java.util.Scanner;
导入java.util.ArrayList;
导入java.io.PrintWriter;
导入java.io.StringWriter;
公共类数据可视化工具{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(System.in);
扫描仪inSS=null;
字符串标题=”;
字符串column1header=“”;
字符串column2header=“”;
字符串dataString=“”;
字符串dataPoint=“”;
字符串dataInt=“”;
布尔inputDone=false;
字符逗号=',';
System.out.println(“为数据输入标题:”);
title=input.nextLine();
System.out.println(“您输入:”+标题);
System.out.println(“”);
System.out.println(“输入第1列标题:”);
column1header=input.nextLine();
System.out.println(“您输入:“+column1header”);
System.out.println(“”);
System.out.println(“输入第2列标题:”);
column2header=input.nextLine();
System.out.println(“您输入:“+column2header”);
System.out.println(“”);
ArrayList字符串=新的ArrayList();
ArrayList integer=新的ArrayList();
System.out.println(“输入数据点(-1)停止输入):”;
而(!inputDone){
dataPoint=input.nextLine();
inSS=新扫描仪(数据点);
如果(数据点等于(“-1”)){
输入完成=真;
打破
}
如果(!dataPoint.contains(“,”){
System.out.println(“错误:字符串中没有逗号。”);
}
//下面是我使用if语句运行的问题。
if(dataPoint.indexOf(“,”)=dataPoint.lastIndexOf(“,”)){
System.out.println(“错误:输入中的逗号太多。”);
}
if(Character.isDigit(dataPoint.charAt(-1))){
System.out.println(“错误:逗号后面没有整数。”);
System.out.println(“”);
System.out.println(“输入数据点(-1)停止输入):”;
}
否则{
//这就是我的错误所在
String[]items=dataPoint.split(“,”);
对于(int i=0;i
您试图获取索引-1处的字符,这里您将获得
StringIndexOutOfBoundsException

Character.isDigit(dataPoint.charAt(-1))

索引应该介于0到string.length()-1之间

如果您试图获取索引-1处的字符,这里将获得
StringIndexOutOfBoundsException

Character.isDigit(dataPoint.charAt(-1))

索引应该介于0到string.length()-1之间,您的错误是
dataInt=items[1]失败,因为没有
项[1]
。另外,如果要通过硬编码索引访问元素,为什么要循环
items
?错误是
dataInt=items[1]失败,因为没有
项[1]
。另外,如果要通过硬编码索引访问元素,为什么要循环使用
?谢谢!这是有道理的。虽然现在我得到一个numberformatorat:Integer-dataInteger=Integer.valueOf(dataInt);我应该将逗号后的信息存储到整数变量中。所以我想这会管用的。我觉得我在这里做错了。
items[1].trim()
,然后将其解析为
Integer
谢谢!这是有道理的。虽然现在我得到一个numberformatorat:Integer-dataInteger=Integer.valueOf(dataInt);我应该将逗号后的信息存储到整数变量中。所以我想这会管用的。我觉得我在这里做错了什么。
items[1].trim()
,然后将其解析为
Integer