I have a string which is getting the data from a jtextfield, and then I substring this data into 3 letters in three strings (String first, second,third)
try {
String text,first,second,third,result;
Swing get = new Swing();
text = get.getMyText();
first = text.substring(0,1);
second = text.substring(1,2);
third = text.substring(2,3);
result = first + third + second;
if(text.isEmpty) {
throw new Exception();
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null,"Empty","....",JOptionPane.ERROR_MESSAGE);
}
I get this strange message from the system instead of the JOptionPane message:
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
any clue of what I miss here ?
for your information, I've tried as well the below, as well still the same error
if(first.isEmpty() || second.isEmpty() || third.isEmpty()) {
// my message
}
my Swing class is as follow:
public class Swing {
// second line of the frame
private static JFrame window; // creating the frame
private static JTextField text;
// setting the frame
/**
* @wbp.parser.entryPoint
*/
public void Run() {
window = new JFrame("Tool");
window.setResizable(false);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().setBackground(new Color(230, 230, 250));
window.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
window.getContentPane().isOpaque();
window.getContentPane().setLayout(null);
// the textfield
text = new JTextField();
text.setHorizontalAlignment(SwingConstants.CENTER);
text.setForeground(Color.BLUE);
text.setFont(new Font("David", Font.PLAIN, 20));
text.setColumns(10);
text.setBounds(504, 11, 149, 20);
window.getContentPane().add(text);
// adding the button from the other class (MyBtn)
MyBtn addBTN = new MyBtn();
window.getContentPane().add(addBTN.run());
// setting the frame
window.setVisible(true);
window.setSize(750, 500);
window.setLocationRelativeTo(null);
}
// preparing the getters for the input
public String getText() {
return text.getText();
}