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

  • Home
  • SEARCH
  • 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 encounter some problem when using inotify. I use inotify to monitor changes on
I am using Flexslider but I've encountered some kind of a problem. It's actually
I am writing a form using jQuery and encounter some difficulties. My form works
When I download some package sources, (e.g. foo-[ver].orig.tar.gz, foo-[ver].dsc), I often encounter dependencies problem
I'm working on a MVC3 web-application and encounter a problem when passing values from
Problem i encounter with outerHTML using IE Browser if i say: txt=Update Complete!; msg
I am using JAXP to generate and parse an XML document from which some
When using Visual Studio I often encounter situations when the IDE freezes while performing
I would like to compile some third-party software but I encounter a problem with
I'm programming some websites with JSP-Scripting and I encounterd a strange problem with urlencoded

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.