0% found this document useful (0 votes)
40 views

Exercise: Advanced Programming (Chapter 1 and 2)

The document provides code snippets to: 1) Parse a full name into first, middle and last name components using substring and split methods. 2) Accept user input words and store them in an ArrayList for display. 3) Develop a basic bilingual dictionary program that allows adding word pairs to a text file and searching for translations.

Uploaded by

Gemechis Tadele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Exercise: Advanced Programming (Chapter 1 and 2)

The document provides code snippets to: 1) Parse a full name into first, middle and last name components using substring and split methods. 2) Accept user input words and store them in an ArrayList for display. 3) Develop a basic bilingual dictionary program that allows adding word pairs to a text file and searching for translations.

Uploaded by

Gemechis Tadele
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercise: Advanced Programming (Chapter 1 and 2)

Design a GUI using Swing that accepts a full name and parse it to FName, Mname and LName on a
button click (Button Parse).

Hint: declare a string variable and read the full name to it. Then use susbstring function of string class to
split the provided full name in to three components.

Answer:

private void btnParseActionPerformed(java.awt.event.ActionEvent evt) {

String x=txtFullName.getText();

String [] y=x.split(" ");

txtFname.setText(y[0]);

txtMname.setText(y[1]);

txtLName.setText(y[2]);

}
2) Write a program that accept a word from a text field and add it to the text area as shown in the figure
below on button Add click. Note that you should have to append it to the existing word while adding the
new one. Use Array List to store all the data.

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {

String x=txtInput.getText();

alldata.add(x);

txtArea.setText("");

for(String i:alldata)

txtArea.append(i+'\n');

Remember to declare a ArrayList as a class variable (i.e., outside of any function)

ArrayList<String> alldata=new ArrayList<>();


3) Develop a bilingual dictionary using the concept that you learnt in Lesson 1 and Lesson 2.

Instruction:

On button Add click your program has to both the Afaan Oromo text the translated one to a textfile
(dictionary.text).

For instance if you enter Saree, and Dog, on button Add click the new input will be added as follows;

Saree_Dog

Underscore is added to make it suitable for split

private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {

String x=jTextField1.getText();

String y=jTextField2.getText();

String xy=x+"_"+y+'\n';

try {
Writer wr = new FileWriter("C:\\Users\\gaddisa\\Desktop\\dictionary.txt",true);

wr.write(xy);

wr.close();

// TODO add your handling code here:

} catch (IOException ex) {

Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);

private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {

// TODO add your handling code here:

String x=jTextField1.getText();

BufferedReader reader;

Boolean isfound=false;

try {

reader = new BufferedReader(new FileReader("C:\\Users\\gaddisa\\Desktop\\dictionary.txt"));

String line=reader.readLine();

while(line!=null)

String[] stringParts = line.split("_");

if(stringParts[0].equals(x))

jTextField2.setText(stringParts[1]);

isfound=true;

break;

line=reader.readLine();

if(!isfound)
{ JFrame f=new JFrame();

JOptionPane.showMessageDialog(f,"The word "+x+" not found.");

reader.close();

// TODO add your handling code here:

} catch (IOException ex) {

Logger.getLogger(Home.class.getName()).log(Level.SEVERE, null, ex);

You might also like