• Post Reply Bookmark Topic Watch Topic
  • New Topic

Split variables+its values from txt files the store into arrayList  RSS feed

 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends,
I want split all variables and its values and then store them into an array-list  while reading txt files, could anybody help me in this regard.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us what have you tried so far.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have only the idea that i have to split word by word and find the variables then load to arraylist but i dont know how to split it. like from content of bellow  txt file

public class Sum {
   
   
   
   
  public static void main(String[] args){
    

     
      int sum=0;
      System.out.println("please enter the number");
      int n = 10;
      int count = 0;
      for(int i=0; i<n;i++)
      {
        
          sum= sum+ i;
      }
      System.out.println("the sum of the numbers are ="+ sum);
    
     
  }
   
}
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you saying you need to read from a text file which contains Java source code.
As a result from reading, you need to find variable names and their values and add them in some data structure.

Try to be a bit more collaborative, otherwise I feel that trying to take words from your mouth and do work for you.

First, you need clearly define what is the task. I can tell further, that task will have sub-tasks, which one of those will be: [1] read file... [2]...

Do you know how to open file? Do you know how to read file? Can you print out file's content to console by reading it? Show us.



 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you,
yes exactly i need the things that you mentioned, I can read file and open file but i dont know how find variables and its values then store into some data structure. the below is the content of file that i am going to read.

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Do you know how to read file?

Mohammad Ali Raisi wrote:I can read file and open file

Liutauras Vilda wrote:Show us.

?
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this one i read file and extract the comments from the content of previous program, but for more clarification I removed those comments that i am extracting through this program.

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so you have some  code already.

You need to find the way to identify variables. Easiest place probably is at their declaration moment. There are built in data types and user's defined. How you find later?

Also, you said you need to find their values. Initial values, all values or ending values?
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Mr. Vilda,
i am checking for built in data types and initial values of them
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello Mr. Vilda do you have any progress to help me resolving that problem, please?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Raisi wrote:hello Mr. Vilda do you have any progress to help me resolving that problem, please?

I thought you wrote you are checking something - misunderstood apparently.

So, you know that Java has finite amount of built in types. One thing you might could do, is to look for variables declarations, i.e.:

int a = 4;

So, look for exact match of 'int' or 'char' or 'long' or ... Long, Integer... next one is variable name, further is its value.

But then there is at least one extra step to think:

int a;

Here you don't have initial value, only variable name. Hence you'd need to look for the very first assignment, i.e. a = 4.

I'm sure there are many other things to think, but those are one of those.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for reply Mr. Vilda as I am new in programming, I searched a lot but finally i found that we can check word by word through split but i could not implement that's why i posted that someone implement and i review their code and learn from that.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be honest, that task as a whole is very complicated. If you just started, that would be an overwhelming.
There are many things to consider, as:

So, here you don't have variable declaration, it is just a comment, what you'd do with that?

Who gave you that task? Is it school assignment? Which year?
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Vilda, no it is not comment , please ignore the comment, and it is not school homework actually i am so interested to know the work of parser.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, just be clear. You need to show some effort and come up with something, so we could give some hints or thoughts.
This site doesn't let us provide solutions, as that way doesn't let people to learn.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mr. Vilda I have already showed you a program that i could find and extract the comments, and for this one i have only understand the way but i could not implement, so i think the purpose of this site is learning from each other as i am the beginner not advance programmer that i raise only the logical error, actually for beginner is implementation is problem when i got the idea sure for the next time i can do by myself i want consider the below example.

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, less talking more work.

So, what you do as a first step, you write a method to print each line to console. Once you can do that, you can move on.

So, write such method and show us. That is learning.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you do that, remove leading and trailing spaces. Google how to do that.

Next, look which lines start with a built in data types - these are of your interest.

Then look for split method. Simple approach split by space, then by '=', that way looking to your given example you could extract variable name and value.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldnt be that simple if variable declared within 'for' loop, hence line wouldnt start with build in data type.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the below is code+ its output and please guide me for the remained, please write a little code that how do i start for the remaining.



output

public class Sum {
    
    
    
    
  public static void main(String[] args){
     

      
      int sum=0;
      int a=4;
      float b;
      b=0.4;
      String name="David";
      
      
  }
    
}
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, read further what I wrote and follow next step. Take a lead of this exercise, it isn't that difficult if you need to do that in its simplest form as you described, without taking into account all possible cases.
 
Mohammad Ali Raisi
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mr. Vilda you are describing generally as i told i know the theory and i dont know how to implement, only write a little code that at least i understand how to start the other parts, and i will search for remaining guide.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohammad Ali Raisi wrote:you are describing generally as i told i know the theory


Liutauras Vilda wrote:remove leading and trailing spaces. Google how to do that.


Google search you put in: Java remove leading and trailing spaces takes up to 5 seconds to find the way how to do. The thing is, that try to look information yourself, try to do all you can from your side, and only then say: "i don't know how to do it, I know only theory". You will always know only the theory, unless you try it out yourself. I have been in that situation too quite recently, so I know what you are talking, and I know what I'm talking.

Mohammad Ali Raisi wrote:implementation is problem when i got the idea sure for the next time i can do by myself
You need to do by yourself from very first time. One can show you how to do A, sure, if the the next task going to be exact A, you'll just copy/paste. Once you get task B, again you won't know what to do, hence will ask somebody else to show. If next time is lucky one and you get exact task B, you'll know how to do. Once you get task C - same again. D - again. E - yet again.

Get an idea? You need to gain understanding yourself by researching a lot - that is a first thing you need to master. Pickup existing tools, like Google and search. We have search functionality here too. You can look and you might even find similar exercise done by somebody else.

Don't expect nobody else to write for you code so you could copy/paste and "learn" from it. Learn I have putted in quotes on purpose.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!