[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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.
0
krizmotlhala
Asked:
krizmotlhala
  • 3
  • 2
2 Solutions
 
CEHJCommented:
The easiest way is to take the whole text and parse it:

String[] instructions = ta.getText().split("\n");
0
 
CEHJCommented:
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
 
WebstormCommented:
Don't forget to catch exception in order to output error message.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
objectsCommented:
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now