-3

I'm trying to have the user input 2 strings into this function so that they can be compared.

I am not too familiar with java more familiar with c++ and I'm not a dev.

public class Levenshtein {

    public static int distance(String a, String b) {
        a = a.toLowerCase();
        b = b.toLowerCase();
        // i == 0
        int [] costs = new int [b.length() + 1];
        for (int j = 0; j < costs.length; j++)
            costs[j] = j;
        for (int i = 1; i <= a.length(); i++) {
            // j == 0; nw = lev(i -1, j)
            costs[0] = i;
            int nw = i - 1;
            for (int j = 1; j <= b.length(); j++) {
                int cj = Math.min(1 + Math.min(costs[j], costs[j - 1]), a.charAt(i - 1) == b.charAt(j - 1) ? nw : nw + 1);
                nw = costs[j];
                costs[j] = cj;
            }
        }
        return costs[b.length()];
    }

    public static void main(String [] args) {
        String [] data = { "kitten", "Mitten" };
        for (int i = 0; i < data.length; i += 2)
            System.out.println("distance(" + data[i] + ", " + data[i+1] + ") = " + distance(data[i], data[i+1]));
    }
}
jpganz18
  • 4,518
  • 12
  • 48
  • 103
Patryk100
  • 19
  • 5
  • 3
    This looks like a duplicate of [this question](https://stackoverflow.com/questions/2506077/how-to-read-integer-value-from-the-standard-input-in-java), and maybe [this one](https://stackoverflow.com/questions/11871520/how-can-i-read-input-from-the-console-using-the-scanner-class-in-java). – Avi Aug 07 '19 at 13:39
  • @Avi Nice catch. Side note: don't forget that you have enough reputation to flag questions as duplicates as well, so that people can vote on it in the close vote queue. – EJoshuaS - Reinstate Monica Aug 07 '19 at 13:43
  • I was feeling nice enough to answer it today instead of flagging. – Avi Aug 07 '19 at 13:44

4 Answers4

2

just use the args in main

public static void main(String [] args) {
    for (int i = 0; i < args.length; i += 2)
        System.out.println("distance(" + args[i] + ", " + args[i+1] + ") = " + distance(args[i], args[i+1]));
}

and run it with java -jar app.jar kitten mitten

munHunger
  • 1,617
  • 1
  • 20
  • 42
1

Here's an example of how to use Scanner to read inputs in Java.

Scanner s = new Scanner(System.in);
String first = s.nextLine();
String second = s.nextLine();
String[] nextTwo = s.nextLine().split(" ");
System.out.println(first);
System.out.println(second);
System.out.println(nextTwo[0]);
System.out.println(nextTwo[1]);
s.close();

Sample input

I am a teapot
Short and stout
Here is my handle

Sample output

I am a teapot
Short and stout
Here
is

As for how to apply this in your program, simply do the following:

public static void main(String [] args) {
    // Using this construct, the "try-with-resources" block, will automatically
    // close the Scanner resource for you
    try(Scanner s = new Scanner(System.in) {
        System.out.println("Enter first word:");
        String first = s.nextLine();
        System.out.println("Enter second word:");
        String second = s.nextLine();
        System.out.println(String.format("The distance is: %d",distance(first, second)));
    }//Scanner s is automatically closed here
}

Note that you should generally NOT close the System.in stream, as it will disallow you from reading input in the rest of the program. However, as your program terminates in the scope of the try-with-resources block, it is acceptable to do so in this scenario.
One approach you can take to close Scanners linked to your System.in stream is to wrap System.in in a CloseShieldInputStream, as seen here.

Avi
  • 2,359
  • 1
  • 9
  • 21
  • 1
    As I noted to IlikeSahne you should not close the `System.in` stream or you will cause issues if you attempt to use it again. This includes using it with `try-with resources`. If one attempts to use `Scanner s = new Scanner(System.in); s.nextLine()` later in the program you will get an exception and be unable to read a line (because you are unable to ever open it again) – Nexevis Aug 07 '19 at 13:55
  • If you want to see this easily just try throwing your code into a method and call the method twice and you will see that code fails. – Nexevis Aug 07 '19 at 14:00
  • @Nexevis I agree absolutely; I have added a note of caution :) – Avi Aug 07 '19 at 14:01
  • Nevermind I thought you only added the `CloseShieldInputStream` part, I think your warning is fine. – Nexevis Aug 07 '19 at 14:04
  • @Nexevis That is assuming you know that you're dealing with the `System.in` stream. You may be dealing with arbitrary input streams, in which case you will need to close your Scanners after creation. `CloseShieldInputStream` is one such method to keep your Scanners stream type agnostic. – Avi Aug 07 '19 at 14:05
0

You can use the scanner object:

Scanner myObj = new Scanner(System.in);  // Create a Scanner object
System.out.println("Enter username");

String userName = myObj.nextLine();  // Read user input
0

I use Scanner for inputs from the Console.

U can do:

Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sx.nextLine();
System.out.println("distance(" + s1 + ", " + s2 + ") = " + distance(s1, s2));
sc.close();
ILikeSahne
  • 164
  • 8
  • Besides the typo of `sx` note that you also should not close the `System.in` stream. – Nexevis Aug 07 '19 at 13:46
  • Why should I not close it? – ILikeSahne Aug 07 '19 at 13:47
  • 1) Because it cannot be reopened. 2) Because you don't need to close it. – Stephen C Aug 07 '19 at 13:48
  • @ILikeSahne Here is some [info](https://stackoverflow.com/questions/14142853/close-a-scanner-linked-to-system-in) on it. Basically you close `System.in` along with the scanner doing this. For example you would be unable to open a new `Scanner` for the rest of the program execution using `System.in` since you closed it. – Nexevis Aug 07 '19 at 13:48
  • Oh ty, I didn't know that. A few programs of me had this problem and I couldn't fix it. – ILikeSahne Aug 07 '19 at 13:50