In: Advanced Math
Using Java (Swing) language(please hard code)... Create a program that has a textfield for the user to type in a set of input. Below that textfield have the following controls to show string manipulations:
(1) A button that will change the entire textfield’s current text to uppercase.
(2) A button with its own textfield for a search value, that will tell the position the search value appears in the textfield above.
(3) A button that reports the current number of characters in the textfield above.
Program:
import javax.swing.*;
import java.awt.event.*;
class StringManipulations
{
public static void main(String args[])
{
JFrame frm=new JFrame("String Manipulations!!");
frm.setLayout(null);
JTextField txtInput=new JTextField();
txtInput.setBounds(180,40,120,40);
frm.add(txtInput);
JButton btn1=new JButton("Uppercase");
btn1.setBounds(40,110,130,30);
frm.add(btn1);
JButton btn2=new JButton("Search");
btn2.setBounds(180,110,110,30);
frm.add(btn2);
JButton btn3=new JButton("CharactersCount");
btn3.setBounds(300,110,140,30);
frm.add(btn3);
JLabel lblOutput=new JLabel();
lblOutput.setBounds(40,180,260,40);
frm.add(lblOutput);
btn1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String output="Uppercase : ";
String str=txtInput.getText();
str=str.toUpperCase();
output=output+str;
lblOutput.setText(output);
}
});
btn2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String output="Position of entered search value: ";
String str=txtInput.getText();
JFrame f1=new JFrame();
String searchValue=JOptionPane.showInputDialog(f1,"Enter search
character's position: ");
int position=str.indexOf(searchValue);
output=output+position;
lblOutput.setText(output);
}
});
btn3.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String output="Number of characters : ";
String str=txtInput.getText();
int len=str.length();
int count=0;
for(int i=0;i<len;i++)
{
count++;
}
output=output+count;
lblOutput.setText(output);
}
});
frm.setVisible(true);
frm.setSize(500,350);
frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
Output: