0

in the title is an error for my array. What does it mean????

ALSO How do i parse the file to remove all the junk at the end like the ENTER ENTER ENTER spaces... (it's like carriage returned a few times, empty lines.. I have NO idea what those are called... they definitely aren't white space..)

EDIT: I'm going to sleep now. I'll wake up early and try to fix it with the help I get. Thank you everyone!

while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
    String line1 = scanner1.nextLine();
    String line2 = scanner2.nextLine();

    // parse line1
    String[] line1Tokens = line1.split(",");

    // parse line2
    String[] line2Tokens = line2.split(",");

    // Print
    String ticket = line1Tokens[0];
    String arrived1 = line1Tokens[2];
    String arrived2 = line1Tokens[3];

    String pickup1 = line2Tokens[1];
    String pickup2 = line2Tokens[2];

This is the whole method:

 public static void merge(File file1, File file2) throws IOException
 {
 Scanner scanner1 = new Scanner(file1);
 Scanner scanner2 = new Scanner(file2);
 String trash = scanner1.nextLine();
 String trash2 = scanner2.nextLine();


while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
    String line1 = scanner1.nextLine();
    String line2 = scanner2.nextLine();

    // parse line1
    String[] line1Tokens = line1.split(",");

    // parse line2
    String[] line2Tokens = line2.split(",");

    // Print
    String ticket = line1Tokens[0];
    String arrived1 = line1Tokens[2];
    String arrived2 = line1Tokens[3];

    String pickup1 = line2Tokens[1];
    String pickup2 = line2Tokens[2];

    System.out.println("Ticket: " + ticket + ", " + 
                       "Arrived: " + arrived1 + ":" + arrived2 + " --- " + 
                       "Pickup: " + pickup1 + ":" + pickup2 + " --- " + 
                       "Cost: ");
}
}

Error I'm recieving:

Ticket: 1421, Arrived: 12:8 --- Pickup: 12:9 --- Cost: 
Ticket: 1422, Arrived: 12:8 --- Pickup: 12:12 --- Cost: 
Ticket: 1423, Arrived: 12:9 --- Pickup: 12:13 --- Cost: 
Ticket: 1424, Arrived: 12:9 --- Pickup: 12:14 --- Cost: 
Ticket: 1425, Arrived: 12:9 --- Pickup: 12:16 --- Cost: 
Ticket: 1426, Arrived: 12:9 --- Pickup: 12:17 --- Cost: 
Ticket: 1427, Arrived: 12:10 --- Pickup: 12:18 --- Cost: 
Ticket: 1428, Arrived: 12:10 --- Pickup: 12:19 --- Cost: 
Ticket: 1429, Arrived: 12:11 --- Pickup: 13:21 --- Cost: 
Ticket: 1430, Arrived: 12:12 --- Pickup: 13:7 --- Cost: 
Ticket: 1431, Arrived: 12:14 --- Pickup: 13:9 --- Cost: 
Ticket: 1432, Arrived: 12:17 --- Pickup: 13:16 --- Cost: 
Ticket: 1433, Arrived: 12:19 --- Pickup: 13:19 --- Cost: 
Ticket: 1434, Arrived: 12:21 --- Pickup: 13:20 --- Cost: 
java.lang.ArrayIndexOutOfBoundsException: 1
at ParkingLot.merge(ParkingLot.java:45)
at LotDriver.main(LotDriver.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

EDIT: So I started correcting it with all the information i've earned.
Here is my method code so far.

public static void merge(File file1, File file2) throws IOException
{
Scanner scanner1 = new Scanner(file1);
Scanner scanner2 = new Scanner(file2);
String trash = scanner1.nextLine();
String trash2 = scanner2.nextLine();


while (scanner1.hasNextLine() && scanner2.hasNextLine())
{
  String line1 = scanner1.nextLine();
  String line2 = scanner2.nextLine();
  String ticket = "";
  String arrived1 = "";
  String arrived2 = "";
  String pickup1 = "";
  String pickup2 = "";


  // parse line1

  //    String[] line1Tokens = line1.split(",");

  // parse line2
  //   String[] line2Tokens = line2.split(",");
  if (line1.contains(","))
  {
    String[] line1Tokens = line1.split(",");
    // Print
  ticket = line1Tokens[0];
  arrived1 = line1Tokens[2];
  arrived2 = line1Tokens[3];
  }
  if (line2.contains(","))
  {
    String[] line2Tokens = line2.split(",");
  pickup1 = line2Tokens[1];
  pickup2 = line2Tokens[2];
  }

  System.out.println("Ticket: " + ticket + ", " + 
                     "Arrived: " + arrived1 + ":" + arrived2 + " --- " + 
                     "Pickup: " + pickup1 + ":" + pickup2 + " --- " + 
                     "Cost: ");
 }
}

However, it's giving me an error still...

 java.lang.ArrayIndexOutOfBoundsException: 1
at ParkingLot.merge(ParkingLot.java:45)
at LotDriver.main(LotDriver.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

Any help here? How do I set the array to solve for -1 everything?

BIood Fox
  • 23
  • 6
  • I don't understand the question. I can't remember the last time I saw an array in a parking lot. – Radiodef Nov 05 '13 at 06:32
  • @Radiodef let me rephrase that then... sorry. – BIood Fox Nov 05 '13 at 06:34
  • Where are you catch error? – alex2410 Nov 05 '13 at 06:35
  • @alex2410 My teacher never taught us to catch, so I have no idea how. I am however, using throws IOException – BIood Fox Nov 05 '13 at 06:35
  • I mean add stacktrace with error to your question – alex2410 Nov 05 '13 at 06:36
  • An ArrayIndexOutOfBounds error is when you try to access an array element that doesn't exist. Say your array has 5 elements as in `int[] ray = new int[5];`. If you try to access index #6 (which is the _7th_ element) you will get an ArrayIndexOutOfBoundsException because there are only 5 elements (_0_ through _4_). – Radiodef Nov 05 '13 at 06:37
  • @Radiodef How would I go about fixing my parsing so it can fit in my array. I believe I just need to fix the array position to -1 ... but how? – BIood Fox Nov 05 '13 at 06:39
  • @alex2410 , I am really sorry but I have no idea what a stacktrace is. – BIood Fox Nov 05 '13 at 06:39
  • Stack Trace: Its what you get on the console. Red colored text is the error. The sequence of calls made to reach that error (method in which error has occured) is called stack trace. BTW show us ur input. – TheLostMind Nov 05 '13 at 06:42
  • Maybe you should review array basics: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html You will need to create a new array at the beginning of the program with your expected number of inputs, then keep track of the next index whenever you add an element. Logically something like `int[] ray = new int[3]; int index = 0; ray[index] = 43; index++; ray[index] = 24; index++; ray[index] = 11;` – Radiodef Nov 05 '13 at 06:42
  • I added the "Stack Trace" ... – BIood Fox Nov 05 '13 at 07:06

3 Answers3

0

First question: It means that you try to access element of array that is greater that array length.

Second. You can use String.contains(CharSequence s) to check that text you read have commas inside, if not, skip it in loop, of course if all your valid lines have to have commas to separate values.

Prettygeek
  • 2,461
  • 3
  • 22
  • 44
  • Okay. So you seem to be the only one with helping the "if line contains comma" thing. I just tried that, and all it did was error. How would you go about doing it? – BIood Fox Nov 05 '13 at 06:52
  • you have to call it like this: `String line2 = scanner2.nextLine();` and then" `if (line2.contains(","))....` – Prettygeek Nov 05 '13 at 07:01
  • Thanks! That helped a bunch! I updated my edit with my new method! It's still giving an array too large error... :/ – BIood Fox Nov 05 '13 at 13:45
  • You have to check your files, maybe in some lines you dont have enough values and that's, a problem. Run debugger and toggle watch on your line Strings, look up at values. – Prettygeek Nov 06 '13 at 07:20
0

If you want to remove whitespaces, simply use trim() like this.

String newVar = oldVar.trim();

If this won't remove the new lines and carriage returns, use this:

newVar = newVar.replaceAll("\\r|\\n", "");

this removes every line separators.

If you want to skip those lines when you're reading the file, here are many answers and methods for checking this: How do I check that a Java String is not all whitespaces?

Community
  • 1
  • 1
Leo Pflug
  • 544
  • 3
  • 15
  • those variables are inside another method. They aren't seeming to work. How do i make those work inside another method? (I'll update the question with my whole method) – BIood Fox Nov 05 '13 at 06:59
  • Actually normally you should deal with those whitespaces directly when getting the data, so I don't see the problem. If you mean that "newVar" and "oldVar" don't work, well that might be since this is just an example and you should adjust the variables' names to your need ... If I'm misunderstanding you, try to explain yourself a bit clearer again. – Leo Pflug Nov 05 '13 at 07:17
0

One of your variable 'line1' or 'line2' doesn't contain the split character ',' thar causes the splitted String [] size is just 1 and it contains the whole original String. You get exception when you access fields with index 1 or 2 in this case. This very often happens when you read a file and you reach the last line that is empty. I recommend you to chech sizes of arrays and skip your code if it is not applicable.

Martin Strejc
  • 4,307
  • 2
  • 23
  • 38