Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I tried to correct it please let me know if I'm in the right direction in writing my method in general, is its logic correct , and then which parts need to be rewritten and hints for them. in this sway I can understand better

Java
public class check{
public static void main(String[] args){
String a = "abc d";
String b= "abcd fe";

System.out.println(compareString(a,b));

}

public static boolean compareString(String a, String b){
// here i try to declare array size i dont know if i can do it in this way ?
String c[]= new String [a.length];
String d[]= new String [b.length];
int i=0;
int j=0;
// here i try to split them and store each char in arrays
for ( i=0; i<=a.length;i++){
String c[i] =a.split[" "];
}
for (j=0;j<=b.length;b++){
String d [j]=b.split()[" "];
}

// here i tried to count matches and add them up plus counting the numer of words 
int matches=0;
int numberOfWords=0;
for (i=0;i<=c.length();i++){
for( j=0;j<=d.length;j++){
if (c[i]==d[j]){
matches++;
}
numberOfWords++;
}
}
// here is it correct to write it this way ? 
if (match/numberOfwords*100>0.9*100){
return true;
{ 
return fales;
}
}

}
}



// I tried to fix the errors but it gives another erros ..
2 errors found:
File: C:\Users\Acer\Desktop\check.java [line: 17]
Error: Syntax error on token "i", delete this token
File: C:\Users\Acer\Desktop\check.java [line: 20]
Error: Syntax error on token "j", delete this token






...

Write a method compare Strings (java) which takes as input two Strings and returns a boolean representing whether your program concludes that the two strings are very similar to each other. A boolean result of true indicates the Strings are very close to each other and false indicates otherwise. Your method should determine this is based on the following rule:

If at least 90% of the words in the first String appear in the second String AND at least 90% of the words in the second String appear in the first String, your method should return true. Otherwise your method should return false. case sensitive,(hello!=HELLO)

To test you method as you write it, we can write a main method so call the method from the main method on various inputs and then print the results. Nowhere in the code should we be using the Scanner class.belwo is what I tried :

I tried to declare a string [] , and by using split method store the chcars and then compare them but I don't know how to compare 90% I compared the whole arrys..

thanks

What I have tried:

Quote:
public class check{
public static void main(String[] args){
String a = "abc d";
String b= "abcd fe";

System.out.println(compareString(a,b));

}

public static boolean compareString(String a, String b){
// here i try to declare array size i dont know if i can do it in this way ?
String c[]= new String [a.length];
String d[]= new String [b.length];
int i=0;
int j=0;
// here i try to split them and store each char in arrays
for ( i=0; i<=a.length;i++){
String c[i] =a.split[" "];
}
for (j=0;j<=b.length;b++){
String d [j]=b.split()[" "];
}

// here i tried to count matches and add them up plus counting the numer of words
int matches=0;
int numberOfWords=0;
for (i=0;i<=c.length();i++){
for( j=0;j<=d.length;j++){
if (c[i]==d[j]){
matches++;
}
numberOfWords++;
}
}
// here is it correct to write it this way ?
if (match/numberOfwords*100>0.9*100){
return true;
{
return fales;
}
}

}
}


// I tried to fix the errors but it gives another erros ..
2 errors found:
File: C:\Users\Acer\Desktop\check.java [line: 17]
Error: Syntax error on token "i", delete this token
File: C:\Users\Acer\Desktop\check.java [line: 20]
Error: Syntax error on token "j", delete this token








Java
import java.util.array;
public class check{
  public static void main(String[] args){
    String a = aaaaa aaa;
    String b= bbbbbbaa aa;
    
    Sysytem.out.println( compareString(a,b);
                        }
}
public sttaic boolean compareString(string a, String b){
  String c[]= new String [a.length];
  String d[]= new String [b.length];
  
  for ( i=0; i<=a.length;i++0){
    String c[i] =a.split[" "];
  }
  for (j=0;j<=b.length;b++){
    String d [j]=b.split([" "]);
  }
  
  if ( Arrays.equals(c,d);{
    return true;
  }
    else{
      return false;
    }
Posted
Updated 25-Feb-18 2:00am
v6

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
private int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on your line:
C#
String d[]= new String [b.length];
and run your app. Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?

This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Member 13676546 24-Feb-18 19:29pm    
thanks for the explanation , below I tried to improve it.
Being a programmer is also a matter of reading carefully the requirements:
Quote:
If at least 90% of the words in the first String appear in the second String AND at least 90% of the words in the second String appear in the first String, your method should return true.

Quote:
but I don't know how to compare 90%

You don't compare 90%, you compare every words one by one and count the matches.
matches / number of words is your percentage.
 
Share this answer
 
Comments
Member 13676546 24-Feb-18 20:05pm    
thanks for the hint. I tried to correct it please let me know if I'm in the right direction in writing my method in general, is its logic correct , and then which parts need to be rewritten and hints for them. in this sway I can understand better

public class check{
public static void main(String[] args){
String a = "abc d";
String b= "abcd fe";

System.out.println(compareString(a,b));

}

public static boolean compareString(String a, String b){
// here i try to declare array size i dont know if i can do it in this way ?
String c[]= new String [a.length];
String d[]= new String [b.length];
int i=0;
int j=0;
// here i try to split them and store each char in arrays
for ( i=0; i<=a.length;i++){
String c[i] =a.split[" "];
}
for (j=0;j<=b.length;b++){
String d [j]=b.split()[" "];
}

// here i tried to count matches and add them up plus counting the numer of words
int matches=0;
int numberOfWords=0;
for (i=0;i<=c.length();i++){
for( j=0;j<=d.length;j++){
if (c[i]==d[j]){
matches++;
}
numberOfWords++;
}
}
// here is it correct to write it this way ?
if (match/numberOfwords*100>0.9*100){
return true;
{
return fales;
}
}

}
}


// I tried to fix the errors but it gives another erros ..
2 errors found:
File: C:\Users\Acer\Desktop\check.java [line: 17]
Error: Syntax error on token "i", delete this token
File: C:\Users\Acer\Desktop\check.java [line: 20]
Error: Syntax error on token "j", delete this token
Patrice T 24-Feb-18 22:44pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
Member 13676546 25-Feb-18 0:57am    
thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900