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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:53:37+00:00 2026-05-27T20:53:37+00:00

I am newbie in java but I think I have done well teaching myself

  • 0

I am newbie in java but I think I have done well teaching myself in these few weeks. But now I am stuck at this loop.

Here is a method from one of my class. To help me debug, I have added “myString” string and “syntax” list inside this method to demonstrate what is happening and to keep it simple, at least for now.

public void getIndex(){
    String myString = "2 2 + 3 5";
    String[] syntax = myString.split(" ");

    for (int index = 0; index < syntax.length; index++){
        System.out.println("current index is: " + index);
        System.out.println("It has: " + syntax[index]);
        // these print statements are made to help me debug
        if (syntax[index] == "+"){
            indexNeeded = index;
            break;
        }
    }
    System.out.println("Index Needed: " + indexNeeded);   

As you can see inside the loop, I want to break the “for loop” when the element of the list, “syntax” is “+”.
(I am showing “+” here but it can be anything in the actual program.)

Here is the output, when run this method:

current index is: 0
It has: 2
current index is: 1
It has: 2
current index is: 2
It has: +
current index is: 3
It has: 3
current index is: 4
It has: 5
Index Needed: 0

The loop should have stopped when it found “+” but it seems that “if statement” is not working at all, and hence “indexNeeded” hasn’t changed.

It’s a simple method but what am I doing wrong here?

  • 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-27T20:53:38+00:00Added an answer on May 27, 2026 at 8:53 pm

    You’re trying to compare strings with ==. That doesn’t work, you need to use .equals():

    change:

    syntax[index] == "+"
    

    to

    syntax[index].equals("+")
    

    == only returns true when both objects refer to the same instance. equals() will return true when the contents of the string are the same. This is what you want.

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

Sidebar

Related Questions

I think that this is a newbie type question but I have quite understood
Well, I'm having this newbie problem right now. I have 3 JFrames. JFrame1 opens
Ok so this is a newbie question on java, but i can't seem to
Sorry if this sounds like a newbie question, but the other day a Java
i'm a total newbie to java and Alfresco and i have this simple problem:
Java Newbie here. I have a JFrame that I added to my netbeans project,
I am a newbie to Java Web Application development. So far all I have
I am a newbie to Java. I have an array of objects that have
Java newbie here, I'm having trouble setting a new line in this code: String
Note: The author of this question has some Java background, but is a complete

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.