Question

In some place of a class I have declared a temporal String variable:

String name;

Which I will use to store data from a text. The text have many fields with these two types of format:

Type: text/html

name=foo

For this case, I am particularly interested in the fields of the type name=foo

So, I breaked previously the lines of the text using split

String lines[] = text.split("\n"); 

And, again, I will use split to identify the fields of the type mentioned. In the code below, the while cycle stops where it detects a name=foo field, and prints the value of that field in the console.

int i = 0;  // Counter for the while cycle

while (!(lines[i].split("=")[0].equals("name"))) {
    i++;                      

    if (lines[i].split("=")[0].equals("name")) // If the field is name...
    System.out.println(lines[i].split("=")[1]); // Prints the value of the field

    name = lines[i].split("=")[1]; // <-- My problem is here
}

My problem starts when I want to copy the value of the field to the String variable mentioned early, giving me an java.lang.ArrayIndexOutOfBoundsException.

I need that String to do something with it later. Any idea to safely copy the value of that field to a String variable?

Was it helpful?

Solution

Adding paranthesis to your if saves you from two problems:

  • if a line contains no = the whole String is in [0] and accessing [1] will result in said Exception
  • you are changing (overwriting) the variable name regardless of the condition

To please the compiler you may also want to intialize name to something like null.

int i = 0;  // Counter for the while cycle

while (!(lines[i].split("=")[0].equals("name"))) {
    i++;                      

    if (lines[i].split("=")[0].equals("name")){ // If the field is name...
        System.out.println(lines[i].split("=")[1]); // Prints the value of the field

        name = lines[i].split("=")[1]; // <-- My problem is here
    }
}

OTHER TIPS

In your code:

String name;
name = lines[i].split("=")[1];

Here name will overwrite every time.

I think you are looking for something like this:

String names[];

String lines[] = text.split("\n");
names[] = new String[lines.length];

And inside you while loop do it like:

names[i] = lines[i].split("=")[1];

There are quite a few things to note about your code:

  • you probably miss {} after the if-statement and therefore update name every run of the while-loop
  • you access [1] without checking how many elements the split("=") yielded
  • you literally call split("=") 4 times on almost every line. Save CPU-time by introducing a temporary variable!
  • you can replace your while-loop by a for-loop which also finds name=value in the first line and does not "throw up" if name=value is not inside any of the lines (you don't check whether i is less than lines.length)

I left your comments inside my answer; feel free to remove them.

Variant a (using an index):

for (int i = 0; i < lines.length; i++) {
    // Only split once and keep X=Y together in name=X=Y by specifying , 2
    final String[] split = lines[i].split("=", 2);

    if (split.length == 2 && split[0].equals("name")){ // If the field is name...
        System.out.println(split[1]); // Prints the value of the field

        name = split[1]; // <-- My problem is here
        break; // no need to look any further
    }
}

Variant b (using "for-each"):

for (String line : lines) {
    // Only split once and keep X=Y together in name=X=Y by specifying , 2
    final String[] split = line.split("=", 2);

    if (split.length == 2 && split[0].equals("name")) { // If the field is name...
        System.out.println(split[1]); // Prints the value of the field

        name = split[1]; // <-- My problem is here
        break; // no need to look any further
    }
}

I suppose your problem is when you reach the last line or a line which doesn't contains a "=" sign. You are checking

!(lines[i].split("=")[0].equals("name"))

but then you add 1 to i, so maybe this condition now is false

if (lines[i].split("=")[0].equals("name"))

and you will get java.lang.ArrayIndexOutOfBoundsException here

name = lines[i].split("=")[1];

if the line doesn't contains a "=".

Try

if (lines[i].split("=")[0].equals("name")) { // If the field is name...
    System.out.println(lines[i].split("=")[1]); // Prints the value of the field

    name = lines[i].split("=")[1];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top