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 7218899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T21:32:47+00:00 2026-05-28T21:32:47+00:00

I’m new to Java, so bear with me if I say anything stupid! I’m

  • 0

I’m new to Java, so bear with me if I say anything stupid! I’m having a few problems, which I think are Unicode-related.

I’m using Scanner to read in tokenised commands from a text file, saved with UTF-8 encoding. Basically I want to first check that the command isn’t equal to either “command1” or “command2” (I do something else in these cases), then otherwise read in a character. If the token isn’t a single character, I’m going to output an error.

Here’s my code:

public static void main(String[] args) throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(args[0]));
    while (scanner.hasNext()) {
        String command = scanner.next();
        if (command.equals("command1")) {
            System.out.println("command: command1");
            // do something
        } else if (command.equals("command2")) {
            System.out.println("command: command2");
            // do something
        } else {
            if (command.length() == 1) {
                char c = command.charAt(0);
                System.out.println("character: " + c);
                // do something with c
            } else {
                System.err.println("error (string was " + command
                        + " with length " + command.length() + ")");
            }
        }
    }
}

And the contents of the text file whose filename I’m passing in args[0] for testing:

command1
x
y
command2
z
└
command1
╒
═

Expected output is:

command: command1
character: x
character: y
command: command2
character: z
character: └
command: command1
character: ╒
character:  ═

Actual output is:

command: command1
character: x
character: y
command: command2
character: z
error (string was └ with length 3)
command: command1
error (string was ╒ with length 3)
error (string was ═ with length 3)

As you can see, the non-standard characters are being seen as a 3-character string by Java. Strangely, if I copy/paste the one of the characters from the terminal output into a System.out.println("└".length()) statement, it correctly prints 1.

Any ideas on where I’m going wrong?
Thanks

  • 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-28T21:32:47+00:00Added an answer on May 28, 2026 at 9:32 pm

    When you open files in Java, the encoding (if you don’t specify one) is taken from the file.encoding system property. This is almost never set to something that you want (if you’re like me, you always want UTF-8).

    To fix, explicitly specify your character set when you create your Scanner:

    Scanner scanner = new Scanner(new File(args[0]), "UTF-8");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.