0

my question is asking this: Write a program that compares two input strings. Output the number of characters that match in each string position. The output should use the correct verb (match vs matches) according to the character count.

Ex: If the input is: crush crash

the output is: 4 characters match

this is what i have so far:

import java.util.Scanner;

public class LabProgram 
{
   public static void main(String[] args)
   {
      Scanner in = new Scanner(System.in);

      String str1 = in.next();
      String str2 = in.next();
      int counter=0;
      if(str1.indexof(0)==str2.indexof(0)){
         counter++;
         System.out.println(counter+"character match");
      else
         System.out.println("All characters match");
   }
}

I know it doesn't look like a lot but I've tried this so many other ways but I clearly am missing something that would make this easier to do. I wanted to count the similar letters in my counter...but I don't know what to do.

f1sh
  • 11,489
  • 3
  • 25
  • 51
new coder
  • 13
  • 1
  • 1
    ```String.charAt``` is what you should be using – g00se Oct 12 '22 at 15:26
  • You'll also at least need to use a loop to loop over the characters of your strings. See: [What is the easiest/best/most correct way to iterate through the characters of a string in Java?](https://stackoverflow.com/questions/196830/what-is-the-easiest-best-most-correct-way-to-iterate-through-the-characters-of-a) – OH GOD SPIDERS Oct 12 '22 at 15:27
  • 1
    You are not looping over the whole input. Also I don't think you understand what `str1.indexof(0)` does. You want `charAt` inside a loop. – f1sh Oct 12 '22 at 15:34
  • Btw, that code doesn't look compilable. Did the closing `}` of the if-block get lost while pasting this here? – Thomas Oct 12 '22 at 15:39
  • @Thomas it's also indexOf, not indexof. So this doesn't work either way – f1sh Oct 12 '22 at 15:40
  • @f1sh true, missed that - I know why I'm relying on my IDE :) – Thomas Oct 12 '22 at 15:45

2 Answers2

0

Use a for loop to iterate across the string inputs and utilize charAt() to look at specific characters within a string

public static void main(String[] args) {
  Scanner in = new Scanner(System.in);

  String str1 = in.next();
  String str2 = in.next();
  int counter=0;
  
  // Loop across the smaller input string
  // Math.min gives us the lesser of 2 values
  for (int i = 0; i < Math.min(str1.length(), str2.length()); i++){

      // If two characters match
      if (str1.charAt(i) == str2.charAt(i)){
          
          // Increment counter
          counter++;
      
      }

  }
  
  // If counter = length of both strings
  if (counter == str1.length() && counter == str2.length()){
      System.out.println("All characters match");
  // If counter is 1
  } else if (counter == 1){
      System.out.println("1 character matches");
  // Otherwise
  } else {
      System.out.println(counter + " characters match");
  }
}

Note: To use Math.Min(), you'll need to

import java.lang.Math;

It's not completely necessary though; you could just figure out which string is longer on your own

mmartinez04
  • 323
  • 1
  • 1
  • 4
  • thank you so much!!! I've struggled so much with doing logic for this and using what I learned so far. I can see how I could apply this for future endeavors, thank you!! – new coder Oct 13 '22 at 20:11
0

Code point

Avoid using char. That type is essentially broken since Java 2, legacy since Java 5. As a 16-bit value, char is physically incapable of representing most characters. One emoji could ruin your whole day.

Instead, use code point integer numbers when working with individual characters. You’ll find code point related methods on several classes, including String, StringBuilder, and Character.

int[] codePointsX = x.codePoints().toArray() ;
int[] codePointsY = y.codePoints().toArray() ;

First, check lengths.

boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
if ( ! sameLength ) { … }

Create an array to hold results of comparing each character.

int[] results = new int[ codePointsX.length ] ;

Compare. The ternary operator ?: is a condensed alternative to using an if test.

We put a zero into results array if the two code points match. Else, we put a zero into results array. The default is a zero, so we could skip the assignment of zero. But I want our intentions to be crystal clear to the reader.

for( int index = 0 ;  index < codePointsX.length ; index ++ ) 
{
    results[ index ] = 
        codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ;  // One for a matching character, Zero for a mismatch.
}

Results.

int countMatches = Arrays.stream( results ).sum();

Full example code.

String x = "cat" ;
String y = "cot" ;

int[] codePointsX = x.codePoints().toArray() ;
int[] codePointsY = y.codePoints().toArray() ;

boolean sameLength = ( codePointsX.length == codePointsY.length ) ;
if ( ! sameLength ) { 
    throw new IllegalArgumentException( "The two input strings have different lengths. " ) ;
}

int[] results = new int[ codePointsX.length ] ;

for( int index = 0 ;  index < codePointsX.length ; index ++ ) 
{
    results[ index ] = 
        codePointsX[ index ] == codePointsY[ index ] ? 1 : 0 ;
}

int countMatches = Arrays.stream( results ).sum();

System.out.println( Arrays.toString( results ) ) ;
System.out.println( "countMatches = " + countMatches ) ;

See this code run at Ideone.com.

[1, 0, 1]
countMatches = 2
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • thank you so much for your help! It's so cool to see how arrays can be implemented into my future coding, I haven't gotten there yet but it seems so helpful! Thank you! – new coder Oct 13 '22 at 20:12