Java Help

I created the Anagrams class that writes permutations of words in a sentence and when I run a compiled program like java Anagrams "sentence1" "sentence2" ... It should generate permutations of each sentence. How can i do this?

import java.io.*;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;

public class Anagrams
{

    ...

    public static void main(String args[])
    {
        String phrase1 = "";
        System.out.println("Enter a sentence.");
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        try { phrase1 = input.readLine(); }
        catch (IOException e) {
        System.out.println("Error!");
        System.exit(1);
        }

        System.out.println();
        new Anagrams(phrase1).printPerms();
    }


}

this is what I still just need it to work on "sentence1" "sentence2" ... when I type java Anagrams command "sentece1" "sentence2" ... ive already compiled it using javac Anagrams.java

+3
source share
3 answers

From your comment, I think your only question is how to use command line arguments to solve the problem:

:

public static void main(String args[])

public static void main(String[] args)

, , . ,

java Anagrams sentence1 sentence2

2. (args[0]) sentence1, (args[1]) sentence2.

, , :

public static void main (String[] args) {
        for (String s: args) {
            System.out.println(s);
        }
    }

.

+2

.

, "IndexOutOfBoundsException", , !

class ArgsExample {
    public static void main(String[] args) {
        System.out.println(args[0]);
        System.out.println(args[1]);

    }

}

C:\Documents and Settings\glow\My Documents>javac ArgsExample.java

C:\Documents and Settings\glow\My Documents>java ArgsExample "This is one" "This
 is two"
This is one
This is two

C:\Documents and Settings\glow\My Documents>
0

Varargs will allow you to use an indefinite number of lines in the method signature, if that is what you are looking for. Otherwise, Roflcoptr is right when it comes to passing arguments to the main one.

0
source

All Articles