0

I am trying to read users id and their link type from a txt file. The data is given in following format.

SRC:aa
TGT:bb
VOT:1

SRC:cc
TGT:bb
VOT:-1

where 'SRC' and 'TGT' indicators of the users id. In data, some users ids are blank, i.e. users disagree to reveal their identities as following:

SRC:
TGT:cc
VOT:-1

SRC:ff
TGT:bb
VOT:1

In this case, I want to give them a special id, "anonymous". So, I wrote the following code:

//Reading source
 dataline = s.nextLine();
String[] line1parts = new String[2];
.
.
 //split the line at ":"
 line1parts = dataline.split(":");
 //if the line has source
 if (line1parts[0].trim().equals("SRC")){
    //if the soruce name is empty
    if (line1parts[1].isEmpty()) {
        src = "anonymous";
        System.out.print("src: " + src);
    }
    //if the source already integer id
    else {
        src = line1parts[1].trim();
        System.out.print("src: " + src);
    }
}

The program shows java.lang.ArrayIndexOutOfBoundsException error. I have also tried if (line1parts[1].equals("") and if (line1parts[1].equals(null). Probably for the case SRC: (when empty) the string array is not creating any object (sorry if I am wrong. I am very new in java). How can I assign an user ID when it is empty? Thanks in advance.

6
  • Can you label the line on which this error occurs? You can edit your post. Commented May 30, 2016 at 11:30
  • Have you checked index [0] for data to exist? Commented May 30, 2016 at 11:31
  • The only reason would be line1Parts have only 1 cell, you can force the split method to return 2 cell by specifying this value after the String (second param, integer) Commented May 30, 2016 at 11:32
  • From your text file sample, there is blank line that might be causing the exception .. Commented May 30, 2016 at 11:41
  • @DrewKennedy, yes, [0] exist. Also I can label the line. The error occurs when user id is empty. @Arc676 Commented May 30, 2016 at 16:37

3 Answers 3

2

If a line only contains SRC: the line1parts array will have only one item, thus line1parts[1] raises an ArrayIndexOutOfBoundsException .

Replace if (line1parts[1].isEmpty()) by if (line1parts.length < 2 )

Sign up to request clarification or add additional context in comments.

1 Comment

I forgot this stupid logic with the split... thanks.
0

Has StephaneM made me remember, the split method trim the empty cell during the splitting. This removed every empty cells at the end. This mean the empty cell if there is no SRC value in your case

To prevent this, you can call

java.lang.String.split(String, int)

This integer specify the length of the array you want, at minimum.

line1parts = dataline.split(":", 2);

You are sure to receive an array with length of 2 or more. So this could still remove some cells, but with a length constraint.

A good think to know is that if you send -1, the split will return EVERY cells. No trimming is done.

Comments

0

You are trying to get the index which is not present.

split() helps to divide value from regex you provided.

you are split the string 1."TGT:cc" in this case split method split String value from ":" and it is returning an array of size 2 i.e. [TGT,cc] (it has 0 and 1 index).

2.When you split the String "SRC:" in this case split method create an array of size 1 i.e [SRC] (it has only 0 index) because in this String after ":" nothing and so that it does not create extra index for null value.

When you call "line1parts[1].isEmpty()" it throw ArrayIndexOutOfBoundsException because it does not have the index 1.

Here you have to check "line1parts.length" before call "line1parts[1].isEmpty()".

    line1parts = dataline.split(":");
    // if the line has source
    if (line1parts[0].trim().equals("SRC")) {

        if (line1parts.length > 1) {

            // if the soruce name is empty
            if (line1parts[1].isEmpty()) {
                src = "anonymous";
                System.out.print("src: " + src);
            }
            // if the source already integer id
            else {
                src = line1parts[1].trim();
                System.out.print("src: " + src);
            }
        }
    }

Or-----------------

You have to do ::

line1parts = dataline.split(":",2);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.