Question

Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...

Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows:

public static int countNumAlphabetic ( String str ) {

}


Sample Output:

countNumAlphabetic("mA13Th9zB86") returns 8

Homework Answers

Answer #1

public static int countNumAlphabetic(String str) {
    int count = 0;
    for (int i = 0; i < str.length(); i++) {
        if (Character.isAlphabetic(str.charAt(i)) || str.charAt(i) == '1' || str.charAt(i) == '2' || str.charAt(i) == '3')
            ++count;
    }
    return count;
}

public class CountNumAlphabetic {

    public static int countNumAlphabetic(String str) {
        int count = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isAlphabetic(str.charAt(i)) || str.charAt(i) == '1' || str.charAt(i) == '2' || str.charAt(i) == '3')
                ++count;
        }
        return count;
    }

    public static void main(String[] args) {
        System.out.println(countNumAlphabetic("mA13Th9zB86"));
    }
}

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
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of...
THIS IS JAVA PROGRAMMING Write a method that finds the shortest word in an array of String values and returns the length, in characters, of that word. The method should use the following header. public static int minLength(String[] array) Write a test program that prompts the user to enter ten strings, store them in an array and invokes this method to return the shortest length, and displays that value. (A near complete program can be found attached to the dropbox)
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...
6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. Ex: If the input is: n Monday the output is: 1 Ex: If the input is: z Today is Monday the output is: 0 Ex: If the input is: n It's a sunny day the output is: 2 Case matters. n is different than N. Ex:...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output is the miles walked. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); Ex: If the input is: 5345 the output is: 2.67 Your program must define and call a method: public static double stepsToMiles(int userSteps)
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
Java Write the method rightN. The method has two inputs: a String str and an int...
Java Write the method rightN. The method has two inputs: a String str and an int n. The output of the method is a new String that contains a "rotated right n" version of str. You can assume that the length of str will be at least n. Here is some sample input and output: rightN("Hello", 2) → "loHel" rightN("Chocolate", 3) → "ateChocol " rightN("Chocolate", 1) → "eChocolat"
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....
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
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);...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Plato states in the Phaedo that: “either knowledge is nowhere to be gained, or else it...
    asked 4 minutes ago
  • 1. The nucleus is a membrane-bound organelle that contains the cells genetic information. What other intracellular...
    asked 19 minutes ago
  • Do genetics play a significant role in human attraction? Defend a position (yes or no). Use...
    asked 41 minutes ago
  • 9.14 The quality-control manager at a compact flu-orescent light bulb (CFL) factory needs to determine whether...
    asked 46 minutes ago
  • Describe a business you are familiar with. Discuss ONE way this firm adjusts its short-term capacity...
    asked 1 hour ago
  • Write a persuasive speech on whether online or face to face learning/education is better for students
    asked 1 hour ago
  • (a) As an analyst briefly explain what you will consider in applying nested designs (b) State...
    asked 1 hour ago
  • Metals are good thermal conductors — that is, when there is a temperature difference across their...
    asked 1 hour ago
  • Theoretical Perspectives that attempts to explain and answer fundamental questions why societies form and why they...
    asked 1 hour ago
  • I am working with data analysis and with a survey. I am cleaning up the following...
    asked 1 hour ago
  • For this problem, carry at least four digits after the decimal in your calculations. Answers may...
    asked 1 hour ago
  • The International Air Transport Association surveys business travelers to develop quality ratings for transatlantic gateway airports....
    asked 1 hour ago