Solved

line by line

Posted on 2004-03-23
8
163 Views
Last Modified: 2010-03-31
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
Comment
Question by:krizmotlhala
  • 3
  • 2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 10659581
The easiest way is to take the whole text and parse it:

String[] instructions = ta.getText().split("\n");
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 10659601
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
 
LVL 13

Expert Comment

by:Webstorm
ID: 10659714
Don't forget to catch exception in order to output error message.
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 86

Expert Comment

by:CEHJ
ID: 10659755
>>String operand = instructionAtoms[1].trim();

Of course you shouldn't assume that the instruction *has* an operand (check array length)
0
 
LVL 92

Expert Comment

by:objects
ID: 10662188
StringReader in = new StringReader(ta.getText());
String line = null;
while (null!=(line=in.readLine())
{
   // then parse the line
}
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 10662424
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
github account with ecipse 1 47
tomcat not starting 6 45
runtime exception 2 27
String array comparison 4 32
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question