Solved

line by line

Posted on 2004-03-23
8
156 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

708 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now