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.
[0]for data to exist?line1Partshave only 1 cell, you can force the split method to return 2 cell by specifying this value after the String (second param, integer)[0]exist. Also I can label the line. The error occurs when user id is empty. @Arc676