I am trying to write a java method that takes two string parameters from a user and then returns the first input after removing the letters that appear in the second input.
For example:
if input1 = fortune and input2 = tune
then the method returns "for"
seems simple but I am not understanding exactly how to tackle this
thanks!!
import java.util.*;
class Solution
{
// return true if ch is present in str
public static boolean find(String str , char ch)
{
int i;
// traverse the string
for( i = 0 ; i < str.length() ; i++ )
{
// get character at index i using charAt(i)
// if the current character is the required character
if( str.charAt(i) == ch )
return true;
}
// if ch is not found in str
return false;
}
public static void main(String[] args)
{
// create a Scanner class object to get user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter first string : ");
// read string from user
String str1 = sc.next();
System.out.print("Enter second string : ");
// read string from user
String str2 = sc.next();
String ans = "";
int i;
// traverse the first string
for( i = 0 ; i < str1.length() ; i++ )
{
// get character at index i using charAt(i)
// if the character at index i is not present in str2
if( find( str2 , str1.charAt(i) ) == false )
ans += String.valueOf( str1.charAt(i) );
}
System.out.println("\nResultant String : " + ans);
}
}
Sample Output
I am trying to write a java method that takes two string parameters from a user...
Write a method in java named isValidEmail that takes a string as input parameter, and returns true if that string represents a valid email address, or false otherwise. An email address is considered valid if it follows this format “[email protected]”, where: user123 represents a sequence of word characters (i.e., letters, digits, or underscore) whose length is between 1 and 10 (inclusive), but the first character must be a letter domain represents a sequence of alphanumeric characters (i.e., letters...
Write a Java method called compare that takes two integers as input parameters and returns 1 if the first parameter is larger than the second parameter returns – 1 if the second parameter is larger than the first parameter, and returns 0 if the two parameters are equal.
Write a Java program which takes a string as a user input. Create 2 functions. The first function expects a string as argument and returns void. It converts all upper case characters to lower case and converts all lower case characters to upper case. It then prints out the converted string to a console. The second function also expects a string as argument and returns void. It will find first charactor that is repeated exactly 2 times in the string....
JAVA Array 3. Write a method called noVowels that takes a String as input and returns true if it doesn't contain any vowels (a, e, i, o, u)
Write a Java method that returns a String representing a file name entered by the user. Use the BufferedReader class to obtain input.
I need to write a method in java that returns void and takes a String element. ex :public void addelemt(String element) the method job is to add elements to an ArrayList as many times as the person wants. please, no use of a switch or do.
JAVA Project Please have the code written in JAVA This is a game I am trying to develop. Please let me know if its possible and what the base code is. Prompt User “Hello! Welcome to the game.” Prompt User “To begin the game, please enter a letter” Random word generator, will set the String to a new word After the user inputs a letter, the computer will enter a loop Once in the loop the computer will test each...
[JAVA] Suppose we need to write code that receives String input from a user, and we expect the String input to contain a double value (inside quotes). We want to convert the String to a double using the static method Double.parseDouble(String s) from the Double class. The parseDouble method throws a NumberFormatException if the String s is not a parsable double. Write a method printSum that takes two String parameters (that hopefully have parsable doubles in them), and either prints...
Using Java write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word.
Write a Java method that takes a String as its input and prints on the first line the odd characters (1st, 3rd, etc) and on the second line the even characters (2nd, 4th, etc).