Question

Need help with java and returning #times string appearing. I was thinking using like a text.contains method but it want characters ch and it's really confusing.3 points static int Q2 (String input char ch) return the number of times the character ch appears in the input String return 0

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

Answer:

*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package javaapplication13;
import java.util.Scanner;


/**
*
* @author ELIXIR BLACK
*/
public class CheckString
{
public String str1;
public String str2;
public Scanner inp;
public CheckString()
{
str1 = "NULL";
str2 = "NULL";
inp = new Scanner(System.in);
}
public CheckString(String s, String x)
{
this.str1 = s;
this.str2 = x;
inp = new Scanner(System.in);
}
public void setStringOne(String s)
{
this.str1 = s;
}
public void setStringTwo(String a)
{
this.str2 = a;
}
public String getStringOne()
{
return this.str1;
}
public String getStringTwo()
{
return this.str2;
}
public void inputStringOne()
{
System.out.println("Please input string one: ");
str1 = this.inp.next();
}
public void inputStringTwo()
{
System.out.println("Please input string two: ");
str2 = this.inp.next();
}
public int getCount()
{
int count = 0;
for(int j = 0; j<str2.length(); j++)
{
for(int i = 0; i<str1.length(); i++)
{
if(str1.charAt(i) == str2.charAt(j))
{
count++;
}
}
}
return count;
}
}

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication13;
import javaapplication13.CheckString;
/**
*
* @author ELIXIR BLACK
*/
public class JavaApplication13 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
CheckString str = new CheckString("iiiiii","i love");
System.out.println(str.getCount());
}
}

Output :

public class JavaApplication13 i 11 12 13 14 15 國lavaAppication 13.java Lbraries @param args the command line arguments ,卻lev

Add a comment
Know the answer?
Add Answer to:
Need help with java and returning #times string appearing. I was thinking using like a text.contains...
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
  • JAVA public static String generatePassword(String gp)        {            String password="";       ...

    JAVA public static String generatePassword(String gp)        {            String password="";            boolean b= false;            for (int i =0;i            {                char ch = gp.charAt(i);                if (i == 0)                {                    password += Character.toUpperCase(ch);                }                if (ch == 'S' || ch == 's')           ...

  • ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and...

    ​​​​​​public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...

  • Why is my java method returning "-1" even if the string is in the array? The...

    Why is my java method returning "-1" even if the string is in the array? The method is:        public static int findWord(String wordToFind {        boolean found=false;            int i=0;            while (i<words.length&& !found)                if (words[i]==wordToFind)                    found=true;                else i++;                if (found)                    return i;                else return -1;   ...

  • hey dear i just need help with update my code i have the hangman program i...

    hey dear i just need help with update my code i have the hangman program i just want to draw the body of hang man when the player lose every attempt program is going to draw the body of hang man until the player lose all his/her all attempts and hangman be hanged and show in the display you can write the program in java language: this is the code bellow: import java.util.Random; import java.util.Scanner; public class Hangmann { //...

  • Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int...

    Write a Java method . public static boolean upChecker(char [][] wordSearch, String word, int row, int col) This method does the following: compare the first character from word to the character in puzzle[row][col] if they match subtract one from row (to check the previous character in the same column) and continue comparing characters, by advancing to the next character in word else if puzzle[row][col] is NOT part of puzzle array or the character in the cell does not match the...

  • [Java] I need help completing the method below. The instructions are also below. public int wordSearch(String...

    [Java] I need help completing the method below. The instructions are also below. public int wordSearch(String word, String filename) { } - find the first time the word appears in a text file filename and returns the position of the word in the file(if its the first word in the file, method should return 1) - assume no punctuation in file - assume all whitespace is either a new line or single space - do not include the times that...

  • JAVA QUESTION 1 The value returned by a value-returning method can be saved for further calculation...

    JAVA QUESTION 1 The value returned by a value-returning method can be saved for further calculation in the program. True False QUESTION 2 To use a predefined method you must know the code in the body of the method. True False QUESTION 3 A formal parameter is a variable declared in the method heading (ie. it's signature). True False QUESTION 4 A local identifier is an identifier that is declared within a method or block and that is visible only...

  • Need help creating a basic java string program using nested if/else, return loops or while loops...

    Need help creating a basic java string program using nested if/else, return loops or while loops or charAt methods while returning information using a console, Please leave notes as to compare my program and see where I went wrong or could've used a different method. secondsAfterMidnight Input: String that represents time of day Returns: integer number of seconds after midnight (return -1 if String is not valid time of day) General time of day format HH:MM:SS(AM/PM) These are examples where...

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of...

    JAVA: Run length encoding is a simple form of data compression. It replaces long sequences of a repeated value with one occurrence of the value and a count of how many times to repeat it. This works reasonably well when there are lots of long repeats such as in black and white images. To avoid having to represent non-repeated runs with a count of 1 and the value, a special value is often used to indicate a run and everything...

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