Hi I have a problem in my code, I have to count the variables used in an expression and when two or more variables are same it should be counted as 1. For example, a+ab = the total number of variable used: 2. The problem is when I inputted a+a =total number of variable used: 2.
Here’s my code:
public void simplify(String strexp){
int ex =strexp.length();
for (int a=0;a<=ex-1;a++){
if(a==0)
{
if (Character.isLetter(strexp.charAt(a)))
{
b++;
}
}
else{
for(int c=0;c<=a-1;c++){
if (Character.isLetter(strexp.charAt(c))==Character.isLetter(strexp.charAt(a)))
{
System.out.println("equal");
break;
}
else if (Character.isLetter(strexp.charAt(c))!=Character.isLetter(strexp.charAt(a)))
{
//if(c==a-1)
//{
b++;
System.out.println("nomatch");
// }
}
}
}
}
JOptionPane.showMessageDialog(null, b);
}
Use a list to count the total amount of variables
This prints out