FOR JAVA
Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”. Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit.
import javax.swing.*;
public class CountDigitStrings {
public static void main(String[] args) {
String str = "", res;
int endIndex, count = 0;
char firstCh, lastCh;
while(!str.equalsIgnoreCase("stop")) {
str = JOptionPane.showInputDialog("Enter String: ");
endIndex = str.length() - 1;
firstCh = str.charAt(0);
lastCh = str.charAt(endIndex);
if(Character.isDigit(firstCh) && Character.isDigit(lastCh)) {
count++;
}
}
res = "The total number of string(s) which starts and ends with a digit are " + count;
JOptionPane.showMessageDialog(null, res);
}
}
Get Answers For Free
Most questions answered within 1 hours.