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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:41:20+00:00 2026-06-08T21:41:20+00:00

I have a strange problem when adding a value to a String array which

  • 0

I have a strange problem when adding a value to a String array which is later involved in an array sort using a hash map. I have a filename XFR900a, and the XFR900 part is added to the array using the following code;

private ArrayList<String> Types = new ArrayList<String>();

...

Types.add(name.substring(0,(name.length() - 1));
System.out.println(name.substring(0,(name.length() - 1));

I even print the line which gives “XFR900”, however the array sort later on behaves differently when I use the following code instead;

Types.add("XFR900");
System.out.println(name.substring(0,(name.length() - 1));

which is simply the substring part done manually, very confusing.

Are there any good alternatives to substring, as there must be some odd non ascii character in there?

Phil

UPDATE

Thanks for your comments everyone. Here is some of the code that later compares the string;

    for (int i=0;i< matchedArray.size();i++){

        //run through the arrays
        if (last == matchedArray.get(i)) {
            //add arrays to a data array
            ArrayList data = new ArrayList();
            data.add(matchedArray1.get(i));
            data.add(matchedArray2.get(i));
            data.add(matchedArray3.get(i));
            data.add(matchedArray4.get(i));
            data.add(matchedArray5.get(i));
            //put into hash map 
            map.put(matchedArray.get(i), data);
        }
        else {
            //TODO
            System.out.println("DO NOT MATCH :" + last + "-" + matchedArray.get(i));

As you can see I have added a test System.out.println(“DO NOT MATCH” … and below is some the output;

DO NOT MATCH :FR99-XFR900
DO NOT MATCH :XFR900-XFR900

I only run the substring on the XFR900a filename. The problem is that for the test line to be printed last != matchedArray.get(i) however they are then the same when printed out to the display.

Phil

  • 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-06-08T21:41:24+00:00Added an answer on June 8, 2026 at 9:41 pm

    You should never use the == operator to compare the content of strings. == checks if it is the same object. Write last.equals(matchedArray.get(i)) instead. The equals() method checks if to object are equal, not if they are the same. In case of String it checks if the two strings consists of the same characters. This might eliminate your strange behaviour.

    PS: The behaviour of == on string is a little unpredictable because the java virtual machine does some optimization. If two strings are equal it is possible that the jvm uses the same object for both. This is possible because String objects are immutable anyway. This would explain the difference in behaviour if you write down the substring manually. In the one case the jvm optimizes, in the other it doesn’t.

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

Sidebar

Related Questions

I've run into a strange problem. I have a string with a value containing
I have strange problem with receiving data from socket. On client im using air
I have a strange problem. I do this query in a node.js server using
I've got this strange problem using py-amqp and the Flopsy module. I have written
I have a strange problem with resharper. I have a project file which is
I have a strange problem. I have an UITextField in which the user should
I'm using emacs 24 on OS X and have the strange problem that I'm
I have discovered a strange problem when using UIActionSheet on the iPhone (iOS 4.2).
I've have this strange problem when adding a column to an existing table. The
I have a strange problem that's proving difficult to diagnose. After adding an Assembly

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.