Question

JAVA Help Write an application that inputs a line of text and a search character and...

JAVA Help Write an application that inputs a line of text and a search character and uses String method indexOf to determine the number of occurrences of the character in the text.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.io.File;

import java.util.Scanner;

public class abc {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a String: ");

        String input = sc.nextLine();  // taking the input string from the user

        System.out.println("Enter the char: ");

        String ch = sc.next(); // taking the character to be counted from the user

        int count = 0; // declaring the variable to store the count with 0

        while (input.indexOf(ch) != -1) { // indexOf return the first index at which the char is found

            input = input.substring(input.indexOf(ch) + 1); // then updating the string with the substring

            // which is from one index after the index at which the char was to found to last

            count++; // increasing the value of count

        }

        System.out.println("The number of occurrences of the character in the text is " + count); //printing it

    }

}

FOR ANY OTHER QUERY PLEASE COMMENT.

Add a comment
Know the answer?
Add Answer to:
JAVA Help Write an application that inputs a line of text and a search character and...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following question Write a script based on the program in Exercise...

    I need help with the following question Write a script based on the program in Exercise 11.13 that inputs several lines of text and uses String method indexOf to determine the total number of occurrences of each letter of the alphabet in the text. Uppercase and lowercase letters should be counted together. Store the totals for each letter in an array, and print the values in tabular format in an HTML5 textarea after the totals have been determined. I need...

  • java 2. Write a recursive method to return the number of occurrences a character c in...

    java 2. Write a recursive method to return the number of occurrences a character c in a string s. c and s should be parameters.

  • JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix...

    JAVA DATA STRUCTURES Write a line-based text editor. The command syntax is similar to the Unix line editor ed. The internal copy of the file is maintained as a linked list of lines. To be able to go up and down in the file, you have to maintain a doubly linked list. Most commands are represented by a one-character string. Some are two characters and require an argument (or two). Support the commands shown below: Command Function Go to the...

  • JAVA Write an application to test the HuffmanTree class. Your application will need to read a...

    JAVA Write an application to test the HuffmanTree class. Your application will need to read a text file and build a frequency table for the characters occurring in that file. Once that table is built create a Huffman code tree and then a string consisting of 0 and 1 characters that represents the code string for that file. Read that string back in and re-create the contents of the original file. Prompt the user for file name. Read the file,...

  • USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements ...

    USING JAVA and NOTTT using BufferedReader or BufferedWriter Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line from...

  • in java plz and not using bufferedreader or bufferedwriter!! Write an application that implements a simple...

    in java plz and not using bufferedreader or bufferedwriter!! Write an application that implements a simple text editor. Use a text field and a button to get the file. Read the entire file as characters and display it in a TextArea. The user will then be able to make changes in the text area. Use a Save button to get the contents of the text area and write that over the text in the original file. Hint: Read each line...

  • WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search,...

    WRITTEN IN JAVA Complete the method printIndices that accepts a String source and a character search, and prints the position of each occurrence of the search value in source. For example: Input: source = "String Programming in Java is fun", search = 'i' Console Output: 3 15 19 27 Template provided from question:    public static int printIndices(String source, char search)    {    }    public static void main(String[] args)    {        System.out.println("Expected printIndices() prints indices 3...

  • Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application...

    Write a Java application that implements the recursive Binary Search alogrithm below: Write a Java application that implements the recursive Binary Search alogrithm below:/** * Performs the standard binary search using two comparisons * per level. This is a driver that calls the recursive method * @return index where item is found or NOT FOUND if not found */public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a, AnyType x) {return binarySearch(a, x, 0, a.length -1);}/** * Hidden recursive...

  • Write a program that takes in a line of text as input, and outputs that line...

    Write a program that takes in a line of text as input, and outputs that line of text in reverse. You may assume that each line of text will not exceed 50 characters. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text. Ex: If the input is: Hello there Hey quit then the output is: ereht olleH уен Hint: Use the fgets function to read a string with spaces from the user...

  • I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according...

    I've been assigned to create a new Java application called "CheckString" (without the quotation marks) according to the following guidelines. ** Each method below, including main, should handle (catch) any Exceptions that are thrown. ** ** If an Exception is thrown and caught, print the Exception's message to the command line. ** Write a complete Java method called checkWord that takes a String parameter called word, returns nothing, and is declared to throw an Exception of type Exception. In the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT