1

I keep getting the error: java.lang.ArrayIndexOutOfBoundsException: 1 within my code. This is my first semester learning java so any help would be greatly appreciated. Also, to kill two birds with one stone. I'm having problems with one of my if statements.

If entry after the comma is not an integer:

Output: "Error: Comma not followed by an integer."

Thanks in advance.

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;
   }     
}
  • And your error is telling you that `dataInt = items[1];` failed, because there is no `items[1]`. Also, why loop over `items` if you're going to access the elements by hard-coded index? – Elliott Frisch Jul 14 '16 at 02:45
  • http://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it – Mehraj Malik Jul 14 '16 at 04:57

1 Answers1

0

You're trying to get the char at index -1, here you will be getting StringIndexOutOfBoundsException

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

The index should be between 0 to string.length()-1

Saravana
  • 11,085
  • 2
  • 29
  • 43
  • Thank you! That makes sense. Although now I am getting a NumberFormatError at: Integer dataInteger = Integer.valueOf(dataInt); I'm supposed to store information after the comma into an integer variable. So I thought this would work. I feel like I'm doing something wrong here. – Hunter Juhan Jul 15 '16 at 01:21
  • `items[1].trim()` before parsing it as `Integer` – Saravana Jul 15 '16 at 04:09