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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:07:28+00:00 2026-06-15T17:07:28+00:00

I have to find the last word in a string and can’t understand why

  • 0

I have to find the last word in a string and can’t understand why my code isn’t working. This is what I have:

int i, length;
String j, lastWord;
String word = "We the people of the United States in order to form a more perfect union";    
length = word.length();    



for (i = length - 1; i > 0; i--)
{
  j = word.substring(i, i + 1);
  if (j.equals(" ") == true);
  {
    lastWord = word.substring(i);
    System.out.println("Last word: " + lastWord);
    i = -1; //to stop the loop
  }
}

However, when I run it, it prints the last letter. I know I could use

String lastWord = word.substring(word.lastIndexOf(” “) + 1)

But I’m pretty sure my teacher doesn’t want me to do it this way. Any help?

  • 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-15T17:07:29+00:00Added an answer on June 15, 2026 at 5:07 pm

    You need to remove the ; after the if to make it work:

    if (j.equals(" ")) // <== No semicolon, and no == true
    {
        lastWord = word.substring(i);
        System.out.println("Last word: " + lastWord);
        i = -1; //to stop the loop
    }
    

    You do not need == true for booleans inside control statements, either.

    Finally, making single-character substrings is more expensive than using single characters. Consider using charAt(i) instead:

    if (word.charAt(i) == ' ') // Single quotes mean one character
    {
        lastWord = word.substring(i+1);
        System.out.println("Last word: " + lastWord);
        break; // there is a better way to stop the loop
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have string like #ls/?folder_path=home/videos/ how i can find last text from string? this
i have a string %/O^/O%/O . I want to find the last / to
i have googled in the last 3 hours but i can't seem to find
Great guys have helped me a lot in this other post: Get last word
I am writing a shell script in which I have to find the last
I have an unordered list, and am using jquery to find the last ul
I am trying to have a macro, on command, find the last used cell
For the last few days I have been attempting to find a method to
I want to find out which tables have been modified in the last hour
I have this string: City - This is some text. This is some more

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.