Hello, I have been working in my java textbook and I am supposed to find the err
ID: 3692491 • Letter: H
Question
Hello, I have been working in my java textbook and I am supposed to find the error in segments of code. I have done all of the homework except this last question. I just need better help understanding the error in this code along with the correct and simplest way to correct it. I have looked on the textbook solutions on this website but the answer wasn't clear enough for me.
So the instructions to: find the error in each of the following code segments:
4.
// tokenize a string that is delimited
// with semicolons. The string had 3 tokens.
String str = “one;two;three”;
String tokens = str.split(“;”);
System.out.println(tokens);
Explanation / Answer
The string is takes as an array ,but here you are declaring as a string so it will throw incompatible type exception
The correct solution is
import java.io.*;
public class Test{
public static void main(String args[]){
String[] tokens = "one;two;three".split(";");
for (String token : tokens)
{
System.out.println(token);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.