?
Solved

line by line

Posted on 2004-03-23
8
Medium Priority
?
172 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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
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.

 
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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 14 hours left to enroll

762 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