1

Relatively new to programming here so I apologize if this is rather basic.

I am trying to convert string lines into actual variables of different types. My input is a file in the following format:

 double d1, d2 = 3.14, d3;
 int a, b = 17, c, g;
 global int gInt = 1;
 final int fInt = 2;
 String s1, s2 = "Still with me?", s3;

These lines are all strings at this point. I wish to extract the variables from the strings and receive the actual variables so I can use and manipulate them.

So far I've tried using regex but I'm stumbling here. Would love some direction as to how this is possible. I thought of making a general type format for example:

 public class IntType{
      boolean finalFlag;
      boolean globalFlag;
      String variableName;

      IntType(String variableName, boolean finalFlag, boolean globalFlag){
      this.finalflag = finalFlag;
      this.globalFlag = globalFlag;
      this.variableName = variableName;
      }
 }

Creating a new wrapper for each of the variable types.

By using and manipulating I would like to then compare between the wrappers I've created and check for duplicate declarations etc'.

But I don't know if I'm on the right path.

Note: Disregard bad format (i.e. no ";" at the end and so on)

Arghavan
  • 1,125
  • 1
  • 11
  • 17
  • I wouldn't use regular expressions for this sort of thing but rather would use tools built specifically for this type of work, and I think what you're looking for is a lexical analyzer, although I'm not sure though since I don't do this work. Although again I **am** sure that regex is not the way to go. – Hovercraft Full Of Eels Jun 17 '17 at 12:07
  • Is each String line follow a repeatable format ? If yes, which one ? Give a sample of the input. Otherwise, how to know if the value has to be assigned to a specific type ? For example both double and int may fit `2`. – davidxxx Jun 17 '17 at 12:07
  • Perhaps [ANTLR](http://www.antlr.org/) is what you're looking for. Again, I'm not an expert in this field, which is why I'm not posting an answer but instead a comment. – Hovercraft Full Of Eels Jun 17 '17 at 12:16
  • @davidxxx yes. It follows the format you would expect when declaring variables. However there could be a different amount in each line. – Reckless Engineer Jun 17 '17 at 12:23
  • @HovercraftFullOfEels I will look into ANTLR and the lexical analyzer, thank you. – Reckless Engineer Jun 17 '17 at 12:25
  • Also, I have to wonder if your question is really an [XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) in disguise where you ask how to solve a specific code problem when the best solution is to use a completely different approach. You might be better off if you tell us the overall problem that you're trying to solve rather than how you're currently trying to solve it. – Hovercraft Full Of Eels Jun 17 '17 at 12:48
  • 1
    @HovercraftFullOfEels The bigger problem is I'm trying to make a program to determine if a certain sjava file will run properly. Meaning I need check there are no duplicate creations of variables and methods. That variable and method names are good, scopes are opened and closed properly etc') Methods is easy, but extracting the variable and their types is whole other matter and that is what is hard for me to figure out. (sjava files declare variables as well as "executes" if's and while's and is able to call methods). – Reckless Engineer Jun 17 '17 at 13:13
  • Please elaborate -- what are "sjava files", and how are they supposed to "work"? – Hovercraft Full Of Eels Jun 17 '17 at 13:13
  • @YonMan If an user answered your question please also **accept** his answer ([Accepting Answers: How does it work?](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work)). If not than please specify what remains unanswered, this is a really crucial part of StackOverflow, thank you very much. – Zabuzard Jul 27 '17 at 12:02
  • @Zabuza I didn't accept any answer =/ eventually I found the text and parsed it manually using regex, then creating variables using factories and switch cases of those specific strings. Saving the results in an ArrayList of a wrapping "Variable" class Later on, I'll upload a proper answer. – Reckless Engineer Jul 29 '17 at 14:27

2 Answers2

1

While others said that this is not possible, it actually is. However it goes somewhat deep into Java. Just search for java dynamic classloading. For example here: Method to dynamically load java class files

It allows you do dynamically load a java file at runtime. However your current input does not look like a java file but it can easily be converted to one by wrapping it with a small wrapper class like:

public class CodeWrapper() {
    // Insert code from file here
}

You can do this with easy file or text manipulations before loading the ressource as class.

After you have loaded the class you can access its variables via reflection, for example by

Field[] fields = myClassObject.getClass().getFields();

This allows you to access the visibility modifier, the type of the variable, the name, the content and more.

Of course this approach presumes that your code actually is valid java code. If it is not and you are trying to confirm if it is, you can try to load it. If it fails, it was non-valid.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
0

I have no experience with Java, but as far as my knowledge serves me, it is not possible to actually create variables using a file in any language. You'll want to create some sort of list object which can hold a variable amount of items of a certain type. Then you can read the values from a file, parse them to the type you want it to be, and then save it to the list of the corresponding type.

EDIT:

If I were you, I would change my file layout if possible. It would then look something like this:

1 2 3 4 //1 int, 2 floats, 3 booleans and 4 strings
53
3.14
2.8272
true
false
false
#etc.

In pseudo code, you would then read it as follows:

string[] input = file.Readline().split(' ');    // Read the first line and split on the space character
int[] integers = new int[int.Parse(input[0])]   // initialise an array with specefied elements
 // Make an array for floats and booleans and strings the same way
while(not file.eof) // While you have not reached the end of the file
{
     integers.insert(int.Parse(file.ReadLine())) // parse your values according to the size which was given on the first line of the file
}

If you can not change the file layout, then you'll have to do some smart string splitting to extract the values from the file and then create some sort of dynamic array which resizes as you add more values to it.

MORE EDITS:

Based on your comment:

You'll want to split on the '=' character first. From the first half of the split, you'll want to search for a type and from the second half, you can split again on the ',' to find all the values.

D-Inventor
  • 450
  • 5
  • 23
  • That is where I was going with when I wrote the second code block. However, I do not know how to effectively extract the names and values. I thought I could use regex for the format of a = #(somenumber) Assuming this is an int type, my pattern would be: [A-Za-z]+(\\s)?=(\\s)?[0-9]+ But I'm not sure how to effectively use it and extract the value from that. – Reckless Engineer Jun 17 '17 at 12:25
  • Unfortunately I cannot change the format. I wish to read code files and check that they will run properly. I cannot change the type of input of a code, as it won't run. – Reckless Engineer Jun 17 '17 at 13:19