0

I want to be able to type the following into console of my IDE :

reverse("a b c d")

But currently I am only able to type

a b c d 

How do I achieve this? I have tried using args[0] but I am getting an error.

public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String sentence = sc.nextLine();
            reverse(sentence);

        }
        public static void reverse(String s){
                String [] stringArray;
                stringArray = s.split(" ");
                int counter = stringArray.length;
            for (String word : stringArray) {
                counter -=1;
                System.out.print(stringArray[counter]+" ");
            }

        }
8
  • according to your main method body, you are expecting a value and you are setting that value as the method parameter Commented Oct 19, 2017 at 13:05
  • do I understand correctly that you want to call the method directly? so not just passing the arguments to it? Commented Oct 19, 2017 at 13:07
  • even if you read the data as you mentioned including method name,your reverse method body will not give you expected output Commented Oct 19, 2017 at 13:09
  • the java reflection API may help you here, see: docs.oracle.com/javase/tutorial/reflect Commented Oct 19, 2017 at 13:10
  • @Lino I want to be able to call the method and pass arguments into it. Commented Oct 19, 2017 at 13:12

2 Answers 2

2

To solve your problem you can use some regex to get the values inside reverse("get this") :

s = s.replaceAll("reverse\\(\"(.*?)\"\\)", "$1");

second instead of that loop you can just use StringBuilder::reverse to reverse your String :

public static void reverse(String s) {
    s = s.replaceAll("reverse\\(\"(.*?)\"\\)", "$1");
    System.out.println(s);
    System.out.println(new StringBuilder(s).reverse().toString());
}

Input

reverse("a b c d")

Output

d c b a

Edit

Based on your comment :

When I run the application. It will ask the user for an input. If the user enters: reverse("I am an apple") It will then output: apple an am I

In this case you have to check the name of the method, so if the String start with name reverse then call reverse method for example :

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    String sentence = sc.nextLine();

    if (sentence.startsWith("reverse")) {
        reverse(sentence);
    }
}

public static void reverse(String s) {
    s = s.replaceAll("reverse\\(\"(.*?)\"\\)", "$1");
    List<String> stringArray = Arrays.asList(s.split("\\s+"));
    Collections.reverse(stringArray);
    System.out.println(stringArray);
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you like to use the java reflection API try something like:

package Test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

public class Example {
    public static void main(String[] args) throws InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException {
        Scanner sc = new Scanner(System.in);
        String sentence = sc.nextLine();
        Class<?> c = Class.forName("Test.Example");//your package and class
                                              //get the method to invoke
        Method  method = c.getDeclaredMethod (sentence.substring(0, sentence.indexOf("(")), String.class);
                           //get the string param
        method.invoke (c, sentence.split("\"")[1]);               
    }
    public static void reverse(String s){
        String [] stringArray;
        stringArray = s.split(" ");
        int counter = stringArray.length;
        for (String word : stringArray) {
            counter -=1;
            System.out.print(stringArray[counter]+" ");
        }
    }
    public static void doNotReverse(String s){
        System.out.print(s);
    }
}

and you are able to type reverse("a b c d") or doNotReverse("a b c d") into console

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.