Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

print all variables

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please urgent help needed.
through this program i want extract the variable name + value and store to array-list and also find if-else and execute its body but it does not work

Output of program...


expected out put
System.out.println("please enter the number"45);
System.out.println("sum is grater than 100"45);
System.out.println("i is grater than 50"45);
System.out.println("sum is less than 50"45);

the file that i am reading is

lowercase baba
Posts: 12893
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's really hard to follow the flow of your code, since you crammed it all into the main method. This is not good programming practice.

Can you explain how you are trying to find all the variables, and their values? You should be able to do this in English (or whatever natural language you prefer) before you write one line of Java.

I would suggest you comment out everything after your // variables loop.  then, in that loop, put a ton of System.out.println statements to see what it is doing. Get THAT piece to work first. Make sure you are simply finding the right strings from your sourcefile before you try and save them + their values values...
Posts: 22002
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mohammad Ali Raisi wrote:please urgent help needed.

Please ease up.

You will never get that output, for a few simple reasons:
1) You loop over the types first. As a result, you will never get b=0.4 before sum=45[/url], because [tt]sum is an int and b is a float, and you always process ints before floats. You probably want to go over the tokens first.
2) You only process assignments that occur with declarations. The variables your code finds are int sum=null (because it doesn't come with a direct assignment), int n = 10, int count = 0, int i = 0, float b = 0.4 and String name = "David". All other assignments get lost.
3) I think you want your code to run this program, but it doesn't evaluate boolean expressions, perform any of the assignments other than those of reason 2, and doesn't perform any loops.

If I am correct in my assumption on what you want to do (run programs), drop everything you've done. Parsing a complex language is even more complex, and then running it all is even more complex. Instead, use what Java already provides (note: you need to run your program using a JDK, a JRE is not enough). First, use JavaCompiler to compile your source file, then use reflection (in combination with URLClassLoader) to load your new class and execute it.

Alternatively, you can use the Rhino (Java 6/7) or Nashorn (Java 8) JavaScript engine. You then need to write JavaScript instead of Java, but you can access Java classes as well.
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please do not think about logic of that sample code i am going to read only i want to store the name of variable+ its value and also i find if-else and print their body please give me a code i am new in java and it is urgent
Posts: 15894
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nobody is going to write code for you. This is a learning site and giving you code is counterproductive to that goal and it is cheating. The Ranch is NotACodeMill.
Hang a left on main. Then read this tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic