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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:50:26+00:00 2026-06-10T01:50:26+00:00

I have to write a program in Java that compares two strings for dashes.

  • 0

I have to write a program in Java that compares two strings for dashes. The test only returns true if each string has the same amount of dashes in the same position in the strings.

Example:

Comparing the following two strings

String string1 = “two-twenty-nine”
String string2 = “ten-fourty-five and I’m hungry.”

with the above criteria would return true. It doesn’t matter whether one string is longer than the other.

Any assistance would be appreciated!

I have tried:
– converting the strings to char arrays and then comparing the indexes
– Using String.indexOf() for each string, and then creating a variable int newStart = String.indexOf() with the index of the previous dash as the new starting point to look from

`   public static void sameDashes(String string1, String string2) {
    int count = 0;
    char index1 = ' ';
    char index2 = ' ';
    char dash = '-';
    char[] string1Array = string1.toCharArray();
    char[] string2Array = string2.toCharArray();
    while (count < string1Array.length && count < string2Array.length) {
        if (string1Array[index1] == dash && string2Array[index2] == dash) {
            System.out.println("true");
        }
    }
}
  • 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-10T01:50:27+00:00Added an answer on June 10, 2026 at 1:50 am

    Since I suspect this is homework, I’m just going to outline a solution.

    • Iterate through string1 using the indexOf method, and get a List of Integers representing the positions of the dashes.
    • Iterate through string2 in the same manner. (You could call a general method twice. Once with string1 and once with string2.)
    • Compare your Lists, and see if they have the same size().
    • If they’re the same size, loop through both lists and see if the positions are the same.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a Java program that tells what coins to give out
I have to write a little Java program that stands in the middle of
My question is I have to write a program in java that creates different
I'm trying to write a really small program in Java that takes a string
I have made a program in Java that calculates powers of two, but it
So for my java class I have to write a program that accepts toppings
I have been asked to write a java program on linux platform. According to
How to write a java program which will tell me whether I have internet
I have to write a program that sniffs network packets (part1-the simple part). And
I have to write a program to check if any random string is present

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.