Computer Applications
A shopping website offers a special discount if the order ID has the sequence 555 anywhere in it. For example, 158545553031, 198555267140, …. .
Fill in the blanks (a) and (b) in the given Java Method to convert the order ID (a long integer) into a string and check if the sequence 555 is present in it.
void checkOrder(long oid)
{
String str = _______(a)_________;
if(______(b)_______)
{
System.out.println("Special Discount Eligible: " + oid);
}
}
Java String Handling
3 Likes
Answer
(a) String str = String.valueOf(oid);
(b) if (str.contains("555"))
Reason
To check if "555" appears in the order ID, we need to convert the long
number into a String
. String.valueOf(oid)
correctly converts a long
to a String
. contains("555")
checks if the substring "555" is present in str.
Answered By
2 Likes
Related Questions
Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
The following code to compare two strings is compiled, the following syntax error was displayed – incompatible types – int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }
Two strings,
city1
andcity2
, are compared usingcity1.compareTo(city2)
, and the result is less than zero. What does this indicate?Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.