Home > OS >  Display elements of a String which are not found on the other
Display elements of a String which are not found on the other

Time:11-19

We were told to do a program on stings and I wasn't able to attend class because I was sick. I am asking for your help on this task that was given to us.

Create a java program that will ask the user to input two Strings. Compare the two strings and display the letters that are found on the first string but are not found on the second string.

Here is what I have at the moment https://pastebin.com/7a4dHecR

I really have no Idea what to do so any help would be appreciated!

https://pastebin.com/7a4dHecR

import java.util.*;
public class filename{
    public static void main(String[] args){
        Scanner sc =new Scanner(System.in);
        
        System.out.print("Input first string: ");
        String one=sc.next();
        System.out.println();
        System.out.print("Input second string: ");
        String two=sc.next();
    
    }
}

CodePudding user response:

There are many ways to do this. I'm going to give you some parts you can put together. They are not the shortest or simplest way to solve this particular problem, but they will be useful for other small programs you write.

Here are some hints:

First, figure out how to step through your code with a debugger. Second, figure out how to find the Javadoc for Java library classes and their methods.

You need to do something for each character in a string. Use a for loop for that:

for (int i = 0; i < one.length(); i  ) {
 // your code here
}

You need to get a particular character of a String.

String c = one.substring(i, i 1);

Read the Javadoc for String.substring to understand what the i and i 1 parameters do.

Now you need to find a way to check whether a String contains another String. Look at the Javadoc for the String class.

Then you can put all this together.

CodePudding user response:

You could try the following:

String diff: StringUtils.difference(one, two);
System.out.println(diff);
  • Related