What delimiter do I need to use to separate a string by \"?\" For example, I wan
ID: 3809794 • Letter: W
Question
What delimiter do I need to use to separate a string by "?"
For example, I want it to print the question and answer in a separate line:
What is your First Name?
Kelly
import java.util.Scanner;
public class Question {
public static void main(String[] args) {
String s = "What is your First Name? Kelly";
Scanner s2 = new Scanner(s);
s2.useDelimiter(" ");
while(s2.hasNext()){
System.out.println(s2.next());
}
s2.close();
}
}
Explanation / Answer
Hi
I have updated the code and highlighted the code changes below
Question.java
import java.util.Scanner;
public class Question {
public static void main(String[] args) {
String s = "What is your First Name? Kelly";
Scanner s2 = new Scanner(s);
s2.useDelimiter("\?");
while(s2.hasNext()){
System.out.println(s2.next());
}
s2.close();
}
}
Output:
What is your First Name
Kelly
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.