Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:44:09+00:00 2026-05-16T05:44:09+00:00

I encounter some problem when using useDelimiter from the Scanner class. Scanner sc =

  • 0

I encounter some problem when using useDelimiter from the Scanner class.

Scanner sc = new Scanner(System.in).useDelimiter("-");
while(sc.hasNext())
{
    System.out.println(sc.next());
}

if I have this input

A-B-C

the output will be

A B

and wait until I type in another “-” for it to print out the last character

However if I instead of having user input data, and insert a String to the Scanner instead the code will work. What’s the reason for it, and how do I fix it? I don’t want to use StringTokenzier

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:44:09+00:00Added an answer on May 16, 2026 at 5:44 am

    If the Scanner didn’t wait for you to enter another - then it would erroneously assume that you were done typing input.

    What I mean is, the Scanner must wait for you to enter a - because it has no way to know the length of the next input.

    So, if a user wanted to type A-B-CDE and you stopped to take a sip of coffee at C, it woud not get the correct input. (You expect [ A, B, CDE ] but it would get [ A, B, C ])

    When you pass it in a full String, Scanner knows where the end of the input is, and doesn’t need to wait for another delimiter.

    How I would do it follows:

    Scanner stdin = new Scanner(System.in);
    String input = stdin.nextLine();
    String[] splitInput = input.split("-", -1);
    

    You will now have an array of Strings that contain the data between all of the -s.

    Here is a link to the String.split() documentation for your reading pleasure.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered a problem when running some old code that was handed down to
I frequently encounter some definitions for Win32API structures (but not limited to it) that
I encountered some code reading typedef enum eEnum { c1, c2 } tagEnum; typedef
I've encountered some errors when I tried to install an artifact manually with Maven
I've encountered lately some opinions saying that Object Oriented design/programming should not always be
Is there some rare language construct I haven't encountered (like the few I've learned
I am debugging some code and have encountered the following SQL query (simplified version):
I encounter an error of the form: The model item passed into the dictionary
I often encounter the following scenario where I need to offer many different types
During coding I frequently encounter this situation: I have several objects ( ConcreteType1 ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.