line by line

I am developing a CPU simulator. So have a textArea where I input a an assembly language code, should be taken as instructions and put into the memory.
So what I want to do is check whether the entered code is a valid assembly language code or what.

Example of a valid one is: LDA 5
                                      ADD 2
                                      SUB 3

I so I am planning to take the code line by line and check its validity. How can I take that code line after line from the textArea? I trust you guys. Help me.
krizmotlhalaAsked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
You can then take the lines and do the same

String[] instructionAtoms = instructions[0].split("\\s");
String instruction = instructionAtoms[0].trim();
String operand = instructionAtoms[1].trim();
0
 
CEHJCommented:
The easiest way is to take the whole text and parse it:

String[] instructions = ta.getText().split("\n");
0
 
WebstormCommented:
Don't forget to catch exception in order to output error message.
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
CEHJCommented:
>>String operand = instructionAtoms[1].trim();

Of course you shouldn't assume that the instruction *has* an operand (check array length)
0
 
objectsCommented:
StringReader in = new StringReader(ta.getText());
String line = null;
while (null!=(line=in.readLine())
{
   // then parse the line
}
0
 
objectsConnect With a Mentor Commented:
To parse your expression use a regular expression which you can modify to match the exact details of the required operands:

import java.io.*;
import java.util.regex.*;


Pattern p = Pattern.compile("([A-Z]+) +(\\d)");  // change the RE to match your required syntax

StringReader in = new StringReader(ta.getText());
String line = null;
while (null!=(line=in.readLine())
{
   // then parse the line
   Matcher m = p.matcher(line.trim());
   if (m.matches())
   {
       String op = m.group(1);
       int reg = Integer.parseInt(m.group(2));
       // do whatever is required with op
   }
}


      System.out.println(op+":"+reg);
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.