1

I'm trying to write a program for my self that will allow me to sort ArrayLists information of companies.

I don't want the program to always show me all the information, only about 8 different aspects of information at a time, such as market value, stock price, ceo, ect.. I have created a few common ones, but i wanted to create a way for the program to allow the user to create their own set of data for the program to display by inserting info. get(whatever command you want) system.

In other words, i want the program to allow me to input a method into the console and have it spit that method out. Is there anyway to do this that is simple?

Edit: Im still having trouble with this and the previous solution didnt actually work. So im going to show you exactly what im working with here:

    public ArrayList<Object> newtag = new ArrayList<Object>();
ArrayList tagname = new ArrayList();
double tagnum;
int e = 0;

public void printCreate() throws IOException{
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    Scanner scan = new Scanner(System.in);

    System.out.println("Insert the number of tags you wish to view up to 5.");

    tagnum = scan.nextDouble();

    if (tagnum > 5){
        System.out.println("Please enter a value less than or equal to 5.");
        tagnum = scan.nextDouble();
    } else {
        for (int a = 0; a < tagnum; ++a){
            ++e;
            System.out.println(e+". Insert the tag which you would like to look up:");
            String taglookup = br.readLine();

                newtag.add(taglookup);
                System.out.println("Type the name of this tag that you would like to be displayed:");
                tagname.add(br.readLine());
            }

        int c = 0;
        for(Company info: companies){

        if (tagnum == 1){    
                ++c;
                System.out.println("-------------------------------------------------------------");
                System.out.println();
                System.out.format("#%s.  %5s   Last trade: %s        Days Low-High: %s - %s    volume:%s \n", c, info.getCompanyName(), info.getRTLastTrade(), info.getDaysLow(), info.getDaysHigh(), info.getVol());
                System.out.println();
                System.out.println(tagname.get(0) + " : " + newtag.get(0));
                System.out.println();

        } else if (tagnum == 2){
                ++c;
                System.out.println("-------------------------------------------------------------");
                System.out.println();
                System.out.format("#%s.  %5s   Last trade: %s        Days Low-High: %s - %s    volume:%s \n", c, info.getCompanyName(), info.getRTLastTrade(), info.getDaysLow(), info.getDaysHigh(), info.getVol());
                System.out.println();
                System.out.println(tagname.get(0) + " : " + newtag.get(0) + "        " + tagname.get(1) + " : " + newtag.get(1));
                System.out.println();

I need to essentially have it so that the newtag.get(0) becomes whatever the user wants from a list of information that all have getters. I tried using christans method but there is an issue with calling the different companies from the Company class.

WolVes
  • 1,286
  • 2
  • 19
  • 39

6 Answers6

2

The simplest way, is get an input of the name of the method, and check it in a if conditional, then call the respective method.

Scanner input = new Scanner(System.in);
String choice = input.nextLine();

if (choice.equals("nameOfMethod1")) {
    nameOfMethod1();
} else if (choice.equals("nameOfMethod2")) {
    nameOfMethod2();
} ...

You could also try a switch (available for String in Java 7)

switch (choice)
{
    case "nameOfMethod1":
        System.out.println("111");
        break;
    case "nameOfMethod2":
        System.out.println("222");
        break;
    default:
        break;
}
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
  • that would be the best option, but OP ask if literally can a method be written in the command line to be executed. – Typo Jan 19 '14 at 02:42
  • Sry juan Manuel, Christians answer is basically what im looking for, however, the only issue with it, is i want to be able to return the new method in a string, so essentially im going to have to list some 30 different gets through if methods, which is fine, but less aesthetically pleasing than i was hoping. =D Thanks Christian I appreciate it. – WolVes Jan 19 '14 at 03:22
  • @user1302551 what do you mean by *return the new method in a string*? – Christian Tapia Jan 19 '14 at 03:30
  • System.out.println("bleh" + info.getSomething() + "belh"), your method definitely works. For that, i was just hoping i could call in an input command someway and it would recognize any one of 30 methods listed for a specified class. – WolVes Jan 19 '14 at 03:51
  • It can be done. Search on Google/StackOverflow *"How to get method names Java"*. – Christian Tapia Jan 19 '14 at 03:56
  • infact what im doing exactly is acquring input using a buffer reader, assigning that to a variable, then trying to find a way to make the inputed value (which would look like a method in the code) to print the method specified while in a system.out in the same line multiple times in the same line. but the input is from the command console – WolVes Jan 19 '14 at 04:02
2

you could use java reflection framework to invoke a particular method on the object...you could create a wrapper. http://docs.oracle.com/javase/tutorial/reflect/member/methodInvocation.html

Raj
  • 86
  • 2
0

No, the method needs to be compiled by the interpreter, this cannot be done on execution time.

Typo
  • 1,875
  • 1
  • 19
  • 32
0

If you want to call your method from command line excluding main() then at that time you can use Groovy a (REPL for a JVM lang).

You can try using command line arguments too.

public static void main(String[] args){
    if (args[0].equals("METHOD1"))
        callMethod1();
    else if(args[0].equals("METHOD2"))
        callMethod2();

    else {
        //Do other main stuff
    }
}
Vinayak Pingale
  • 1,315
  • 8
  • 19
0

You could use an interface to define how you're going to call the method, then instantiate one implementation of the interface for each name that you want to be able to interpret, and store the instances in a HashMap keyed by String.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
-4

I don't get your question.I think you wanna give an option to the user to giv input from the console application.

package MyExamples;

import java.util.Scanner;

public class GetInputFromUser {
    public static void main(String args[]) {
        int a, b, c;

        Scanner input=new Scanner(System.in);
        System.out.println("Enter first value =");
        a = input.nextInt();
        System.out.println("Enter second value =");
        b = input.nextInt();

        c = a + b;

        System.out.println("Output is " + c);
    }
}
vallentin
  • 23,478
  • 6
  • 59
  • 81
Wiki
  • 92
  • 1
  • 8