Problem: Inner If Statements

78 views
Skip to first unread message

Pb600

unread,
Sep 24, 2012, 4:40:25 PM9/24/12
to javap...@googlegroups.com
Hi, 
I'm trying to use JavaParser to convert a code like this:

if (i != 10) {
if (i != 2) {
if (i == 7) {
i = 7;
} else if (i != 5) {
if (i != 3) {
if (i == 4) {
i = 4;
}
} else {
i = 3;
}
} else {
i = 5;
}
} else {
i = 2;
}
} else {
i = 10;
}

Into this:
                if (i == 10) {
i = 10;
} else if (i == 7) {
i = 7;
} else if(i == 5) {
i = 5;
} else if(i == 4) {
i = 4;
} else if(i == 3) {
i = 3;
} else if(i == 2) {
i = 2;
}

Seems easy, but I cannot get Inner statements.
Example:

if (i != 1) {
    if (i == 3) {
i = 1;
    }
}

getThenStmt() of the first Statement is supposed to return an IfStmt, but it's returning a Statement...

Look:

IfStmt firstStatement = new IfStmt("i != 1"); << Obviously It's not a correct constructor, just done that to show what is the first if statement.

firstStatement.getThenStmt() instanceof IfStmt will return false; but the statement is the "if(i == 3)", there's anyway to do what I want with this JavaParser? Would be cool get a List of Statements inside the Then Statement, also Inside the Else Statement ^^

Thanks =D

Ahti Legonkov

unread,
Sep 25, 2012, 1:29:34 PM9/25/12
to javap...@googlegroups.com
On 24 September 2012 23:40, Pb600 <ipb...@gmail.com> wrote:
> Hi,
> I'm trying to use JavaParser to convert a code like this:
>
> if (i != 10) {
> if (i != 2) {
> if (i == 7) {
> i = 7;
> } else if (i != 5) {
> if (i != 3) {
> if (i == 4) {
> i = 4;
> }
> } else {
> i = 3;
> }
> } else {
> i = 5;
> }
> } else {
> i = 2;
> }
> } else {
> i = 10;
> }

[snip]

> firstStatement.getThenStmt() instanceof IfStmt will return false; but the
> statement is the "if(i == 3)", there's anyway to do what I want with this
> JavaParser? Would be cool get a List of Statements inside the Then
> Statement, also Inside the Else Statement ^^

If you look more closely, you'll see that the "then statement" is not
a "if statement" but rather a "block statement" that contains an "if
statement".

--
lego

Michael Koch

unread,
Sep 25, 2012, 3:25:48 AM9/25/12
to javap...@googlegroups.com
Hi,


From you code getThenStmt() should returna BlockStmt which contains an IfStmt as first child.


Cheers,
Michael
> *if (i != 1) {*
> * if (i == 3) {*
> * i = 1;*
> * }*
> *}*
>
> getThenStmt() of the first Statement is supposed to return an IfStmt, but
> it's returning a Statement...
>
> Look:
>
> *IfStmt firstStatement = new IfStmt("i != 1");* << Obviously It's not a
> correct constructor, just done that to show what is the first if statement.
>
> *firstStatement.getThenStmt() instanceof IfStmt* will *return false*; but
> the statement is the "*if(i == 3)*", there's anyway to do what I want with
> this JavaParser? Would be cool get a List of Statements inside the *Then
> Statement*, also Inside the *Else Statement* ^^
>
> Thanks =D
>
> --
> You received this message because you are subscribed to the Google Groups "JavaParser" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/javaparser/-/cR_9d2rRKmsJ.
> To post to this group, send email to javap...@googlegroups.com.
> To unsubscribe from this group, send email to javaparser+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/javaparser?hl=en.
>

--
CTO
Enough Software GmbH & Co. KG
S�gestra�e 70, 28195 Bremen, Germany
tel: +49 421 9889 131
fax: +49 421 9889 132
mail: michae...@enough.de
http: www.enough.de, www.j2mepolish.org
Registered at the local court of Bremen under HRA 25283 HB
Management Board: Robert Virkus, Michael Koch, Marco Tabor
CEO: Robert Virkus

twitter: @enoughsoftware

Liability partner:
Enough Beteiligungs GmbH, Bremen, Germany
CEO: Robert Virkus
Registered at the local court of Bremen under HRB 25348 HB
Reply all
Reply to author
Forward
0 new messages