Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote