is there anything wrong with the solution. the question are from java course
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. |
Answer: |
import javax.swing.*; public class IsAllLetters { public static void main(String[] args) { String input; int count = 0; boolean nonAlphabet = false; while (true) { nonAlphabet = false; input = JOptionPane.showInputDialog("Enter a string (STOP to exit): "); if (input.equalsIgnoreCase("STOP")) break; for (char letter : input.toCharArray()) { if (!Character.isLetter(letter)) { nonAlphabet = true; break; } } if (!nonAlphabet) count += 1; } JOptionPane.showMessageDialog(null, "Valid numbers of the strings that was entered is " + count); } } |
Write a complete main method that does the following:
For example, C:>java Question1 Mary had a little cat There are 2 words of length three |
|||||
|
Below are the corrected code. =============================================================================== import javax.swing.*; public class IsAllLetters { public static void main(String[] args) { String input; int count = 0; while (true) { input = JOptionPane.showInputDialog("Enter a string (STOP to exit): "); if (input.equalsIgnoreCase("STOP")) break; if (input.length() >= 2 && Character.isDigit(input.charAt(0)) && Character.isDigit(input.charAt(input.length() - 1))) { count++; } } JOptionPane.showMessageDialog(null, "Valid numbers of the strings that was entered is " + count); } }
=====================================================================
// given main method public class Question1 { public static void main(String args[]) { int num, divisible = 0; // this test the two arguments. if (args.length < 2) { throw new IllegalArgumentException("Two minimum argument is required."); } else { //parsing the argument to integer int length3Count = 0; for (int i = 0; i < args.length; i++) { if(args[i].length()==3){ length3Count+=1; } } System.out.println("There are "+length3Count+" words of length three"); }// prints out the results and ends the program. } }
=====================================================================
Get Answers For Free
Most questions answered within 1 hours.