lundi 18 juillet 2016

Switch Cases vs. If, else when comparing multiple strings

I have a question in regards to parsing a file. Here is what a few of the statements look like that I parse the file with:

while(scan.hasNextLine()){
    line = scan.nextLine();//this gets the whole line
    if(line.contains("SomeDataIKnow"){
        data = new Scanner(line).useDelimiter("=");//scans just the one line
        value = data.next();//sets value to the string right after the =
    }
    else if(line.contains("DifferentDataIKnow"){
        data = new Scanner(line).useDelimiter("=");
        value = data.next();
 }

Mind you there are about 30 of the lines that all have different Strings in the line contains statement.

I know what the words before each "=" will say but not after the equals. So I need to parse through looking for specific data and then get those values after the equals sign. I know typically you shouldn't use if statements when there are more than like 2 or 3 statements. So I'm curious if anyone has any ideas of another way to do it? Possibly Switch statements I was thinking, but not sure if that would even help at 30 some lines. Also if it helps visualize the file, there is like 400 total lines but I only need about 30 of them. I can't post the file but an example of a full line in the file would be something like:

Device.A Bunch of other info that changes.THE DATA I KNOW = THE VALUE I NEED

So I parse multiple files and the data changes with the exception of the DATA I KNOW part, that never changes and its what I use to find the line that has the value I need. Sorry if this at all sounds confusing.

Aucun commentaire:

Enregistrer un commentaire