Simple if statement in Java [closed] - java

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How do i write an if statement in Java that displays Goodbye! if the variable word contains a letter d?
Thanks to everyone.

if (word.contains("d")) {
System.out.println("Goodbye!");
}

Use:
if(word.indexOf("d") >= 0) {
System.out.println("Goodbye!");
}

Look up the Java API docs to see what is available in the String class. There are several options including the indexOf() method that returns -1 if the given character is not in the String and an index of the character if it is found in the String.
int ans = mystring.indexOf(mychar);
You can then use an if statement to check the ans variable.

if (word.contains("d")) System.out.println("Goodbye!");
Well, that was in Java!!

if(word.compareTo("d") == 0)
System.out.println("Goodbye!");
or
if(word.equals("d"))
System.out.println("Goodbye!");
That's assuming of course that word is a String and not a char.

Related

How to display a portion of a string at an index of a string array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string array
String[] albumnames;
now how to take a string from particular index position with limited number of charachters.
For example,
if, albumnames[position] have value "abcdefghijk"
then i want to take the first 5 characters only.
That is "abcde".
The substring method of String can be used to achieve this. Try
String s = albumnames[position].substring(0,5);
See substring docs
albumnames[position].substring(0,5);
you can see the methods in String class, like substring, indexof

How to create random cell number for testing purpose [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to create a random cellnumber 07939393914 for automation testing purpose.
Last 079393(5 digits) digits should change randamly.. each time test runs..
Can any one suggest JAVA code for this ? or else Selenium Java code ?
Thanks
Is this a number or a String? I ask as it has a leading zero.
Take you initial number as 7939300000
then add to it Math.round(Math.Random()*10000)
If you want it as a String, take your string as "079393" and use Integer.toString on the result above, then concatentate them
Use RandomStringUtils class.
String randomNumbers = RandomStringUtils.randomNumeric(5);
String phNo = 079393+randomNumbers;
var num=Math.ceil(Math.Random()*100000)
for random first 5 digit numbers
and add
var your_last_5digits; //as string to your last 5 digits
cellNumber='0'+num+your_last_5digits;

Why can this not be done? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to shorten my code using a loop. I have for example 5 zombies in my game. So I thought I could do this
Image zombie;
for(int i = 0; i < 5; i++){
if (zombie.getZombieRect().intersects(zombie + i + .getZombieRect())) {
}}
Why can this not be done? adding i to the end of zombie. zombie being an image. The oother variables are zombie1, zombie2 etc.
Thanks for all help.
This is what arrays are for:
Zombie zombies[] = {zombie, zombie1, zombie2, zombie3, zombie4};
for (int i = 0; i < zombies.length; i++) {
if (zombie.getZombieRect().intersects(zombies[i].getZombieRect())) {
}
}
Make an Array of Objects and then u can call them by using zombie[i] etc whatever you want to do.
The thing of adding you are trying to do is suitable in case of strings only
"zombie"+i;
etc.
To answer the question,
zombie + i
Is a compile-time error because java does not allow an Image object to be used in combination with an int in the '+' operator.

how to make an if statement then send a message [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How would i make it so that if the input from the user is yes, then send the messaage You Successfully hunted a cow!
System.out.println("Would you like to hunt a cow?");
System.out.print("yes or no?");
String a = kbReader.next();
System.out.println("You successfully hunted a cow!")
if (a.equalsIgnoreCase("yes")) {
...
}
(or equals if you want to match "yes" exactly)
Is it really what you need:
if ("yes".equals(a)) {
System.out.println("You successfully killed a cow!")
}
Note the reverse check of string literal versus the input to avoid NPE.
user the equals method. for Strings, it simply checks if the strings have the same value
if(a.equals("yes")){
System.out.println("You successfully killed a cow!")
}
else{
//do whatever
}

Check if all chars match? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
Basically, I have to write a method that determines whether all the characters in a string are the same, using only library String methods.
Use matches. Please read up the documentation of Pattern class to understand how to use matches function to check your input. It is quite useless for me to give you a straight answer, since you don't learn much from it.
That method will give you one-liner solution after you have understood regex.
public static void main(String[] args) {
String str = "aaaaa";
int count = str.length() - str.replaceAll(String.valueOf(str.charAt(0)), "").length();
if(count == str.length())
System.out.println("All characters in a given String are same");
}
Try this : This is the simplest and best solution for this kind of problem.
One way to solve this is without looping or recursion is by [ab]using String.replace1.
The actual implementation is left as an exercise.
1 It does not require that your code loops, but something must loop.

Categories