Could you please explain me why I have this output but not other one! Thank you
ID: 3553435 • Letter: C
Question
Could you please explain me why I have this output but not other one! Thank you very much!
public class PracSplit
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow.";
String [] all = s3.split(" ");
System.out.printf(all.length + " AND " + all[3]);
}
}
Question options:
1)
4 AND snow.
2)
19 AND snow.
3)
snow. AND snow.
4)
19 AND snow.
5)
19 AND crips
6)
4 AND crisp
Give the output of the following code:
public class PracSplit
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow.";
String [] all = s3.split("n");
System.out.printf(all.length + " AND " + all[1]);
}
}
Question options:
1)
4 AND Cold
2)
4 AND and
3)
3 AND and
4)
19 AND and
5)
3 AND Cold a
6)
3 AND d crisp s
7)
3 AND ow.
Give the output of the following code:
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow.";
StringTokenizer st = new StringTokenizer(s3);
System.out.print(st.countTokens());
System.out.print(" ");
System.out.println(st.nextToken());
}
}
Question options:
1)
0 Cold
2)
1 Cold
3)
4 Cold
4)
4 and
5)
3 and
6)
2 and
7)
2 crisp
8)
2 snow.
9)
1 crisp
Give the output of the following code:
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow.";
StringTokenizer st = new StringTokenizer(s3);
String word = st.nextToken();
String w2 = st.nextToken();
System.out.print(st.countTokens());
System.out.print(" ");
System.out.println(st.nextToken());
}
}
Question options:
1)
0 Cold
2)
1 Cold
3)
4 Cold
4)
4 and
5)
3 and
6)
2 and
7)
2 crisp
8)
2 snow.
9)
1 crisp
Give the output of the following code:
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow.";
StringTokenizer st = new StringTokenizer(s3, "o");
String word = st.nextToken();
String w2 = st.nextToken();
System.out.print(st.countTokens());
System.out.print(" ");
System.out.println(st.nextToken());
}
}
Question options:
1)
2 crisp
2)
2 snow.
3)
0 w.
4)
1 w.
5)
2 ld and crisp sn
6)
3 C
1)
4 AND snow.
2)
19 AND snow.
3)
snow. AND snow.
4)
19 AND snow.
5)
19 AND crips
6)
4 AND crisp
Explanation / Answer
I added line number comments to corresponding code in each question so you know which line I'm referring to. Let me know if you need more clarification and I'd be glad to explain more! :)
1.
public class PracSplit
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow."; // Line 1
String [] all = s3.split(" "); // Line 2
System.out.printf(all.length + " AND " + all[3]); // Line 3
}
}
The answer is 4 AND snow. As you can see in Line 2, we are making an array called "all" with the contents of Line 1 split on a space character (" ").
When we split Line 1 on a space character, our "all" array has the contents: ["Cold", "and", "crisp", "snow"].
In line 3, we first print the length of our new array. As you can see, the length is 4, so this is where the 4 came from. Then we print "AND" along with the fourth item in our array, since our array is zero-indexed. As you can see, the fourth item is "snow", so that is where the "snow" part came from in the answer.
2.
public class PracSplit
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow."; // Line 1
String [] all = s3.split("n"); // Line 2
System.out.printf(all.length + " AND " + all[1]); // Line 3
}
}
The answer is 3 AND d crisp s. As you can see in Line 2, we are making an array called "all" with the contents of Line 1 split on an "n" character. This is very similar to how we solved the previous question, just a little more difficult to visually see because we have to keep track of "n"s instead of " "s.
When we split Line 2 on an "n" character, our "all" array has the contents: ["Cold a", "d crisp s", "ow."] since we see 3 "n"s.
The length is 3 and the second item in the array is "d crisp s" which is where the answer came from.
3.
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow."; // Line 1
StringTokenizer st = new StringTokenizer(s3); // Line 2
System.out.print(st.countTokens()); // Line 3
System.out.print(" "); // Line 4
System.out.println(st.nextToken()); // Line 5
}
}
The answer is 4 Cold. In line 2, we make a new StringTokenizer from our Line 1 string. StringTokenizer breaks a string into tokens based on spaces (like question 1) by default. Since we don't enter any other string delimiter (such as "n" like we used as a string delimiter in question 2), we use the default one. This is a little different than what we did in the first question because instead of getting an array of values to work with, we basically get a pointer to work with. We print 4 because StringTokenizer has a countTokens() method that returns the number of remaining tokens we can advance to, which is 4 because we have 4 words and we haven't advanced the pointer yet. We call this method in Line 3. We can visualize our string and pointer like this (even though we don't work with an array):
["Cold", "and", "crisp", "snow"]
^
In Line 5, we print nextToken() from our StringTokenizer. When we call nextToken(), we print the first word in our String, "Cold", since we've never called this method before. This is why "Cold" is printed.
Thus, the answer is 4 Cold.
4.
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow."; // Line 1
StringTokenizer st = new StringTokenizer(s3); // Line 2
String word = st.nextToken(); // Line 3
String w2 = st.nextToken(); // Line 4
System.out.print(st.countTokens()); // Line 5
System.out.print(" "); // Line 6
System.out.println(st.nextToken()); // Line 7
}
}
The answer is 2 crisp. We create a StringTokenizer like we did in Question 3. Here is how our string looks before we call any methods (basically after Line 2):
["Cold", "and", "crisp", "snow"]
^
When we call Line 3, we advance the pointer 1 spot (and save the word "Cold" as String word) so now our pointer is here:
["Cold", "and", "crisp", "snow"]
^
When we call Line 4, we advance the pointer another spot (and save the word "and" as String w2) so now our pointer is here:
["Cold", "and", "crisp", "snow"]
^
Now in Line 5, we have 2 more tokens we can call ("crisp" and "snow") so this is why our countTokens() method returns 2.
In Line 7, we call nextToken() again. This returns "crisp" since that's where our pointer is right now. Thus, our answer is 2 crisp.
5.
import java.util.*;
public class PracTokenizer
{
public static void main(String [] args)
{
String s3 = "Cold and crisp snow."; // Line 1
StringTokenizer st = new StringTokenizer(s3, "o"); // Line 2
String word = st.nextToken(); // Line 3
String w2 = st.nextToken(); // Line 4
System.out.print(st.countTokens()); // Line 5
System.out.print(" "); // Line 6
System.out.println(st.nextToken()); // Line 7
}
}
This is the same as question 4, except we split on "o" rather than on spaces. Thus, our StringTokenizer can be visualized like the below diagram after Line 2:
["C", "ld and crisp sn", "w."]
^
In Line 3, we save "C" as String word because it's the next token and advance our pointer:
["C", "ld and crisp sn", "w."]
^
In Line 4, we save "ld and crisp sn" as String w2 because it's the next token and advance our pointer:
["C", "ld and crisp sn", "w."]
^
In Line 5, we print 1 because as you can see, we have 1 more token left that we can print (which is "w.").
In Line 7, we print the next token, which is "w." This is how our answer is 1 w.