how to make an if statement then send a message [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 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
}

Related

Different if statements Java [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have some questions in which I would like to ask.
First off:
int id = 1;
So, we can have:
if (id == 1)
{
//TODO:
}
But we can also have:
if (id == 1)
//TODO
I was wondering what's the difference between these two?
In the second if statement only first statement after if is inside if block. In the first if statement you can put as many statements as you like between { and } and all of them are inside if block.

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 can I read a single word from a txt file and make it a variable in my code? [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 text file with a single word say "fish " but it will have spaces after the word. is there anyway I can pull the word fish as a variable but leave off the spaces?
Scanner in = new Scanner(new File("path_to_file.txt"));
String var = in.next();
var = var.trim();
in.close();
Not the most efficient way, but one of the simplest.
string Text = File.ReadAllText("path_to_file.txt").trim();

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;

Simple if statement in Java [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 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.

Categories