Question

is there anything wrong with the solution. the question are from java course Write a main...

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:

  1. Takes any number, but at least two, command line arguments which are words (represented as strings) and will print to the console the number of words of length three. (Hint: loop through the args array)
  2. If there are not at least two command line arguments, throw an IllegalArgumentException with an appropriate message.

For example,

C:>java Question1 Mary had a little cat

There are 2 words of length three

Selected Answer:

// 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

for ( int i = 0;i < args.length; i++) {

num = Integer.parseInt(args[i]);

if ( num % 10 == 0){ // its checks the divisibility of (x/10)

divisibles = divisibles + 1;

}

}

}// prints out the results and ends the program.

Homework Answers

Answer #1
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.
    }
}

=====================================================================

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Consider the following Java program : public static void main (string args [ ]) { int...
Consider the following Java program : public static void main (string args [ ]) { int result, x ; x = 1 ; result = 0; while (x < = 10) { if (x%2 == 0) result + = x ; + + x ; } System.out.println(result) ; } } Which of the following will be the output of the above program? A. 35 B. 30 C. 45 D. 35 2. public static void main(String args[]) { int i =...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length();...
import java.util.Scanner; public class FindMinLength { public static int minLength(String[] array) { int minLength = array[0].length(); for (int i = 0; i < array.length; i++) { if (array[i].length() < minLength) minLength = array[i].length(); } return minLength; } public static void main(String[] args) { Scanner in = new Scanner(System.in); String[] strings = new String[50]; for (int i = 0; i < strings.length; i++) { System.out.print("Enter string " + (i + 1) + ": "); strings[i] = in.nextLine(); } System.out.println("Length of smallest...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {       ...
import java.util.Stack; import java.util.Scanner; class Main { public static void main(String[] args)    {        Stack<Integer> new_stack = new Stack<>();/* Start with the empty stack */        Scanner scan = new Scanner(System.in);        int num;        for (int i=0; i<10; i++){//Read values            num = scan.nextInt();            new_stack.push(num);        }        int new_k = scan.nextInt(); System.out.println(""+smallerK(new_stack, new_k));    }     public static int smallerK(Stack s, int k) {       ...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
In Java Let’s say we’re designing a Student class that has two data fields. One data...
In Java Let’s say we’re designing a Student class that has two data fields. One data field, called id, is for storing each student’s ID number. Another data field, called numStudents, keeps track of how many Student objects have been created. For each of the blanks, indicate the appropriate choice. id should be   public/private   and   static/not static . numStudents should be   public/private   and   static/not static . The next three questions use the following class: class Counter {     private int...
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students...
using java LO: (Remember) Students will recall how to read from standard input. LO: (Apply) Students will write loops that iterate as long as a condition is true. Write a program that reads words from standard input and displays them to standard output. When the word "end" is read, print it out and then stop reading input. starter code: import java.util.Scanner; /* * Reads and displays words from standard input until hitting a stop word. */ public class WhileLoop {...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have...
I am writing code in java to make the design below. :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) :-):-):-):-):-):-)  :-):-):-):-):-):-)  :-):-):-):-):-):-) I already have this much public class Main { int WIDTH = 0; double HEIGHT = 0; public static void drawSmileyFaces() { System.out.println(" :-):-):-):-):-):-)"); } public static void main (String[] args) { for (int WIDTH = 0; WIDTH < 3; WIDTH++ ) { for(double HEIGHT = 0; HEIGHT < 2; HEIGHT++) { drawSmileyFaces(); } } } } I am pretty sure that alot of this is wrong...