2014-01-19 42 views
1

我正在嘗試爲自己編寫一個程序,該程序將允許我排序公司的ArrayLists信息。如何在運行程序時通過命令行調用java類的方法?

我不希望程序一直向我顯示所有信息,一次只能查看8個不同的信息,比如市場價值,股票價格,首席執行官等。我創建了一些常見的信息,但我想爲程序創建一種方法,允許用戶通過插入信息來創建自己的一組數據以供程序顯示。得到(無論你想要的命令)系統。

換句話說,我希望程序允許我在控制檯中輸入一個方法並讓它吐出這個方法。無論如何要做到這一點很簡單嗎?

編輯:我仍然遇到麻煩,以前的解決方案實際上並沒有工作。所以我要去告訴你在這裏到底是什麼進出口工作:

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(); 

我需要基本上有它,以便newtag.get(0)成爲無論從用戶的信息列表,所有有干將希望。我嘗試過使用克里斯坦方法,但是從公司類中調用不同的公司有一個問題。

回答

2

最簡單的方法是獲取方法名稱的輸入,並在if條件中檢查它,然後調用相應的方法。

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

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

你也可以嘗試switchavailable for String in Java 7

switch (choice) 
{ 
    case "nameOfMethod1": 
     System.out.println("111"); 
     break; 
    case "nameOfMethod2": 
     System.out.println("222"); 
     break; 
    default: 
     break; 
} 
+0

這將是最好的選擇,但OP詢問是否字面上可以在要執行的命令行中寫入方法。 – Typo

+0

Sry juan曼努埃爾,基督徒的答案基本上是什麼即時尋找,但是,唯一的問題是,我想能夠返回一個字符串中的新方法,所以本質上即時通訊將不得不列出一些30個不同的獲取如果方法很好,但不如我希望的那樣美觀。 = D謝謝克里斯蒂安我很欣賞它。 – WolVes

+0

@ user1302551你是什麼意思*返回字符串中的新方法*? – Christian

-4

我沒有得到你的question.I想你想給一個選項,用戶可以從控制檯應用程序連鎖行業的輸入。

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); 
    } 
} 
+0

有時您可以猜測或做出一些假設,但必須確保99%。 – Christian

0

不,該方法需要由解釋器編譯,這不能在執行時完成。

0

如果你想從除main()以外的命令行調用你的方法,那麼在那個時候你可以使用Groovy a (REPL for a JVM lang)

你也可以嘗試使用命令行參數。

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

    else { 
     //Do other main stuff 
    } 
} 
0

你可以使用一個接口來定義你打算如何調用該方法,然後實例化一個實施要能夠解釋,並將實例存儲在鍵控HashMap每個名字的接口通過String

相關問題