Parse String statement into a boolean expression [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
Improve this question
I have a string statement String s = sb.contains("is");
I want to use this in an if condition like
if (s) {
//do something
}
How can I achieve this functionality?
Edit: Basically I get a boolean logic along with strings as an input. Ex: "Stack & over & (is | flow)". I have an array of sentences and I have to pick every sentence that follows this logic. I thought I would construct a string like "sb.contains(stack) && sb.contains(over) && (sb.contains(is) || sb.contains(flow))" and I thought I would run this boolean logic over all the sentences. Is there any other way of doing this?

The contains method returns a boolean value, so you can just simply set the type of variable 's' to a boolean.
public class Main
{
public static void main(String[] args) {
String sb = "crisis";
boolean s = sb.contains("is");
if(s){
System.out.println("S is true");
System.out.println(s);
}
}
}
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

Related

How to correct this text processing programme? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
import java.util.Scanner;
public class TextProcessing
{
public static void main(String[] args)
{
String sentence, wordToBeTargeted, wordToBeReplaced, output;
boolean wordToCheck;
Scanner myInput = new Scanner(System.in);
sentence = myInput.nextLine();
do
{
wordToCheck = true;
System.out.println("Please enter the word for replacement:");
wordToBeTargeted = myInput.nextLine;
if(sentence.toLowerCase().indexOf(wordToBeTargeted.toLowerCase()) == -1)
{
wordToCheck = false;
System.out.println("This word cannot be found in the sentence!")
}
else
{
System.out.println("Please enter the word you would like to replace with:");
wordToBeReplaced = myInput.nextLine();
}
}while(wordToCheck = false);
}
}
}
Write a file named TextProcessing.java that will replace all occurrences of a word in a string with another word
The expected outcome is like this:
I won't write the code out - you should still get something out of the exercise, but will give some direction. The first approach that comes to mind:
Split the first input on ' ' into a list
Iterate through the list, conditionally changing values based based on two input strings
As you're iterating you can either output into a new string / stringbuilder, or directly write to console depending on the requirements

Java - Event handle data protection for Strings with 2 data types [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
For event handling how can you check a string that has a combination of integers and a character?
For example - 1234p
If the user enters the example above, how can you check if the user enters integers first and then a character at the end? What kind of exception will be thrown if the data type input is not an integer or char?
You can use REGEX [0-9]+[a-zA-Z] to match if the string contains chars and integers otherwise throw an IllegalArgumentException
public void check(String input) {
if (!input.matches("[0-9]+[a-zA-Z]")) {
throw IllegalArgumentException("Not valid string");
}
// do other logic
}
So from your question it sounds like you expect a string that has all numbers except the last character that has to be an alphabet. You can check if the given string matches this condition the following way too:
String string = "1234p";
int length = string.length();
boolean numsFirst = string.substring(0, length - 1).chars().allMatch(x -> Character.isDigit(x));
boolean lastChar = Character.isDigit(string.charAt(length - 1));
if(numsFirst && lastChar)
return true;
else
return false;

What can I use instead of lcs (Longest common substring) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I am trying to calculate the common string using lcs, but this algorithm only calculates 1 string. What can I use instead?
LCS = "aaabbbcccxxx" and "aaadddccc" result: "aaa"
but what i want= "aaaccc"
help please:)
You can apply your LCS algorithm once to get the "aaa" result, then remove this result from both strings, and re-apply your LCS algorithm to get the "ccc" result. Finally you will concatenate the temporary results.
Your java code in the main class may look like the following (assuming that you have a method LCS(String string_1 ,String string_2) performing yourLCS algorithm:`
public static ArrayList<String> temp_results;
public static String string_1,string_2,temp_result,final_string;
public static void main(String args[]) {
while (temp_result != null && !temp_result.equals("")) {
temp_result = LCS(string_1,string_2);
string_1.replaceAll(temp_result,"");
string_2.replaceAll(temp_result,"");
temp_results.add(temp_result);
}
for (String iterator_string : temp_results){
final_string = final_string + iterator_string;
}
System.out.println("This is the result "+final_string);
}
public static String LCS(String string_1, String string_2){
return ""; //put your actual LCS logic here, you should not return an empty string!
}

The result returned by getPalterly method is wrong. (hint: do not return the correct string ) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have this task:
Write a class with Guttery name specifier and the public
which contain visible public static method called getPalterly
and the arguments dhangar, puccinoid type string and
which returns string. The method checks getPalterly
if dhangar string is part of puccinoid and if
the above proposal is verified returns the first 3
characters string dhangar. If the above link does not
verified then returns as a string the length of the string
dhangar.
I have written this code:
public class Guttery {
public static String getPalterly(String dhangar , String puccinoid) {
int g = dhangar.length();
if (puccinoid.contains(dhangar)) {
return dhangar.substring(g - 3 , g);
} else {
int a = dhangar.length();
String b = Integer.toString(a);
return b;
}
}
}
But i have this error:
The result returned by getPalterly method is wrong. (hint: do not return the correct string )
Any ideas?
Did you write you should return the first 3 characters ?
Then :
if (puccinoid.contains(dhangar)) {
return dhangar.substring(0 , 3);

Check a string for consecutive repeated characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
It is asked in an interview to write the code in Java to display the string which doesn't have consecutive repeated characters.
E.g.: Google, Apple, Amazon;
It should display "Amazon"
I wrote code to find continues repeating char. Is there any algorithm or efficient way to find it?
class replace
{
public static void main(String args[])
{
String arr[]=new String[3];
arr[0]="Google";
arr[1]="Apple";
arr[2]="Amazon";
for(int i=0;i<arr.length;i++)
{
int j;
for(j=1;j<arr[i].length();j++)
{
if(arr[i].charAt(j) == arr[i].charAt(j-1))
{
break;
}
}
if(j==arr[i].length())
System.out.println(arr[i]);
}
}
}
Logic : Match the characters in a String with the previous character.
If you find string[i]==string[i-1]. Break the loop. Choose the next string.
If you have reached till the end of the string with no match having continuous repeated character, then print the string.

Categories

Resources