Question

Write a Java statement to compare the string in s1 to the string in s2 for...

  1. Write a Java statement to compare the string in s1 to the string in s2 for equality of content.
  2. What a Java statement to determine the length of the string in s1.
  3. Write a Java statement to split the string in s1 into tokens, using " " as the delimiter, and stored the results in a variable named tokens.

Homework Answers

Answer #1

Answer(1): Compare content of strings-

String S1 = "Hello";
String S2 = "Hello";
System.out.println(S1.equals(S2));  //compare the contents of strings S1 and S2.

Output : true

Answer(2): length of a string-

String S1 = "This is a string";
System.out.println(Str1.length()); //this will print the length of string S1,that is: 16

Output : 16

Answer(3): tokenize the string by delimiter " " -

String S1 = "This is a string";
String[] tokens = S1.split(" ");  //array of strings
for (String token : tokens)
{
    System.out.println(token);   //this will print all tokens one by one 
}

Output : This

is

a

string

Hope this helps!

Thumbsup;)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function to check whether string s1 is a substring of string s2. The function...
Write a function to check whether string s1 is a substring of string s2. The function returns the first index in s2 if there is a match. Otherwise, return -1. For example, the function should return 2 if s1 is "to" and s2 is "October". If s1 is "andy" and s2 is "candy", then the function should return 1. The function prototype is as follows: int indexOf(const char *s1, const char *s2) Write a program to test the function.
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by...
Python Practice Sample: Write a function shuffle (s1, s2) which creates a new string formed by “shuffling” the letters of two strings s1 and s2. If the strings are not of equal lengths, concatenate the remaining letters of the longer string to the result. Your program should work with the code below: s1 = "abcd" s2 = "1234" s3 = "abcdefg" s4 = "123456" print(f"{s1:10s}\t{s2:10s}\t{shuffle (s1,s2)}") print(f"{s3:10s}\t{s2:10s}\t{shuffle (s3,s2)}") print(f"{s1:10s}\t{s4:10s}\t{shuffle (s1,s4)}") Output: abcd 1234 a1b2c3d4 abcdefg 1234 a1b2c3d4efg abcd 123456 a1b2c3d456
The String Class: 1a.) Given the following delcaration: String modification1; Write a statement that stores the...
The String Class: 1a.) Given the following delcaration: String modification1; Write a statement that stores the uppercase equivalent of the string object called "city" in modification1. 1b.) Write a statement that declares a String variable named city. The variable should be initialized with the string literal "Los Angeles". 1c.) Given the following declaration: int stringSize; Write a statement that stores the length of the string object called "city" in stringSize. 1d.) Given the following declaration: char oneChar; Write a statement...
python Suppose city is a variable that holds a string of at least 5 characters. Write...
python Suppose city is a variable that holds a string of at least 5 characters. Write one statement that assigns to a variable named letter the third letter of the string stored in city as an upper case letter. (Note: We'll assume the third letter of city is a letter)
Java Lab Assignment Write a method called convertToArray. The method will take a String as an...
Java Lab Assignment Write a method called convertToArray. The method will take a String as an argument and it will return an array of chars. Each index of the String will be stored in each index of the array. method must be well documented using JavaDoc, example: /** * The find method checks if the target in the array * @param String [] a - array of Strings * @param String t - value to be searched for * @return...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity...
IN JAVA Methods*: Calorie estimator Write a method ActivityCalories that takes a string indicating an activity (sit, walk, jog, bike, swim) and duration in minutes (integer), and returns the estimated calories expended (double). Calories per minute for a 150 lb person (source): sit: 1.4 walk: 5.4 run: 13.0 bike: 6.8 swim: 8.7 If the input is sit 2, the output is 2.8 Hints: Use an if-else statement to determine the calories per minute for the given activity. Return the calories...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using...
Java Please [(1)] A palindrome is a string that reads the same forwards as backwards. Using only a fixed number of stacks and queues, the stack and queue ADT functions, and a fixed number of int and char variables, write an algorithm to determine if a string is a palindrome. Assume that the string is read from standard input one character at a time. The algorithm should output true or false as appropriate [(2)] Let Q be a non-empty queue,...
is there anything wrong with the solution. the question are from java course Write a main...
is there anything wrong with the solution. the question are from java course Write a main method that will request the user to enter Strings using a JOptionPane input dialog. The method should continue accepting strings until the user types “STOP”.       Then, using a JOptionPane message dialog, tell the user how many of the strings begin and end with a digit. Answer: import javax.swing.*; public class IsAllLetters {     public static void main(String[] args) {         String input;         int count =...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT
Active Questions
  • Consider insertsort. Suppose that the input array A has 1% probability to be monotonically decreasing. Show...
    asked 7 minutes ago
  • Your company is thinking of introducing a Bring Your Own Device (BYOD) policy. You have been...
    asked 14 minutes ago
  • Attached is the file GeometricObject.java. Include this in your project, but do not change. Create a...
    asked 16 minutes ago
  • Suppose the number of cars in a household has a binomial distribution with parameters n =...
    asked 19 minutes ago
  • HR needs some information on the new interns put into a database. Given an id, email,...
    asked 40 minutes ago
  • Problem solving strategies Questions years = input("Enter a number of years and I'll tell you how...
    asked 44 minutes ago
  • Calculate ?Hrxn for the following reaction: CH4(g)+4Cl2(g)?CCl4(g)+4HCl(g) Use the following reactions and given ?H?s. C(s)+2H2(g)?CH4(g)?H=?74.6kJC(s)+2Cl2(g)?CCl4(g)?H=?95.7kJH2(g)+Cl2(g)?2HCl(g)?H=?184.6kJ Express...
    asked 51 minutes ago
  • ASCII (American Standard Code for Information Interchange) has an encoding for every character of the alphabet,...
    asked 1 hour ago
  • Is home confinement with electronic monitoring a deterrent? Are there negatives to being confined to one’s...
    asked 1 hour ago
  • Social hostility can have severe lasting effects of interperpersonal relationship during our adolescence years, which if...
    asked 1 hour ago
  • - A series RLC circuit has R=15 ?, L=1.5 H, and C=15 ?F. (a) For what...
    asked 1 hour ago
  • TV Circuit has 30 large-screen televisions in a warehouse in Erie and 60 large-screen televisions in...
    asked 1 hour ago