Trimming String in Java [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 8 years ago.
Improve this question
I need to perform some operation on string.
Scenario is I need to compare that a[i] = b[i].
now say a[i] = Set Temperature
Now since a[i] contains the word Temperature, b[i] will be set to "Set Temerature (C)". (This is the business rule).
In this case a[i] will not be equal to b[i].
For my testing purpose how can trim value of b[i] to set to Set Temprature?

You could use String#contains instead.
String first = "Set Temperature";
String second = "Set Temperature (C)";
if(second.contains(first)) {
// logic
}
Ordering is important - note that if you reverse the call between first and second, it will fail, since first does not contain any characters such as " (C)".

Use String.contains(). See the java docs
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#contains(java.lang.CharSequence)

Not sure if I got your question right.
But based on what I understood - instead of checking for equality you can check
if(b[i].startsWith(a[i])

Related

Is there a way to do many similar tasks in Java? [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 5 days ago.
Improve this question
Say for instance I want to repeat the line of code
Integer int1 = new Integer(value1);
for many variables, such as int1 to int100. I am not asking about this exact task in particular - I am asking about any such situation where the code would be identical save for replacing small details like int1 and value1 with int2, value2. Is there a way to have the JVM complete this for me?
I am not even sure what approach to take on this or what term to search for more information. The only thing I can think to try is instead of typing "int1", having a loop that changes a string containing the name and attempting to pass the string as a symbol to the JVM but this of course does not work.
It was a little strange question and I don't know if I understood your meaning correctly or not.
But in this particular case, instead of repeating the code, you can use a data structure like an array. See Oracle tutorial.
int[] numvar = new int[arr.length];
for (int i = 0; i < args.length; i++) {
int someNumber = Integer.parseInt(args[i]);
numvar[i] = someNumber;
}

java: string split loose last element [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 6 months ago.
Improve this question
im sorry for the screen shot
the strange result for me,
while result of split does not contain the last element,
from my pov the correct result must be
['[','xtrue','']
am i right?
public static List<String> splitString(String source, String delimiter) {
if (Objects.equals(delimiter, "[")) {
return Arrays.asList(source.split("\\["));
}
String[] sArr = source.split(delimiter);
return Arrays.asList(sArr);
}
sure, guess im not safe with split operator, but a little search on google do not solve my question how to use for get as i want
A per documentation:
Trailing empty strings are therefore not included in the resulting array.
So the output is correct.
If you want trailing empty strings you'll have to use the two-parameters version of split passing a negative integer as the second parameter, since
If the limit is negative then the pattern will be applied as many times as possible and the [resulting] array can have any length
So, like you say in your own answer
source.split(delimiter, -1);
will include the empty string after the last " .
for the community
the solution for my case
source.split(delimiter, -1);
thx

Java error how (Cannot convert double to bolean) [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 1 year ago.
Improve this question
I have been getting back in to Java after not using it for a while and tried to make a calculator one that has more then just +. However, I don't have a lot of experience coding, so I don't understand what I did wrong. It says I cant turn a double into a boolean, but that's not what I'm doing. Here is the code I get an error on.
if (One = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Plus +"!");
}else if (Two = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Sub +"!");
}else (Three = UserInput.nextDouble()) {
System.out.println("Your answer is "+ Multi +"!");
You should use "==" for comparison in if blocks.
Single = sign is an assignment operator and does not return the boolean (logic) value that is necesery to check condition in the if ... else statement. Insteed = you should use == operator to compare values or Objects.equals method.

How to check if price of a product is between given range using assert in selenium java [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 3 years ago.
Improve this question
I have a test case where I need to go to Jewelry section and select the price range filter of $700.00 - $3000.00. After selecting the range I can see 1 product priced at $2100.00. Now, My question is how do I check using assert that the product price $2100 is between $700 - $3000? Do I need to get rid the $ signed? as actual result will contain $ signed while comparing with expected.
Please help?
Thanks in advance
It is very simple in Java
assertThat(mynum).isBetween(min, max);
Or
assertTrue(min <= mynum && mynum <= max);
Yes, you need to get rid of the $ sign, that's useless and as said in the comments, Java (and probably no other programming language) has a specific money type.
You can use Regex for that
"[0-9]+"
And for the assert, it's as simple as adding two conditions inside
assertTrue(price > 700 && price < 3000);

How to use only the last bit provided by this for loop? [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 8 years ago.
Improve this question
So, I'm trying to calculate a list of number to eventually sort, so I only want the final result of this for loop.
for (int anno=startyear; TimePeriod>=anno;anno++) {
System.out.println(anno);
}
Where anno = 1995 and I am counting to the current day, I end up getting a result that slowly counts up, where it first counts at 1995, then it counts 1995 and then 1996, and so on.
How do I only get the end result for use in my program? The result that would simply be 1995-2014. Not the repeats.
edit: Forgot to mention I need every number in between 1995-2014 as well
You shouldn't need a loop for this, assuming your TimePeriod variable equals 2014 then just do the following to print out the desired result:
System.out.println(startyear+"-"+TimePeriod);
That will print out:
1995-2014
You already know the final value: it's TimePeriod. If that's all you need, just use that and get rid of the loop:
System.out.printf("%d-%d", startyear, TimePeriod);

Categories

Resources