2016-09-19 8 views
0

私は初心者ですが、私はルータのシンプルなシミュレータになるプログラムを書いています。文字列を2にスライスし、部分文字列を別の関数に転送する方法はありますか?

コマンド "ipA 192.0.0.1"(differents IP)を入力し、 "ipA"または "ipB"などを検出するステートメントを切り替えたいとします。コマンドに割り当てられたすべての場合、IP(String)他の機能にここに私の簡単なコードがあります。どんな解決策ですか?あなたが始める必要があります:(

public class Main { 

private static Scanner scan; 
private static Commands com; 

public static void main(String[] args) { 

    com = new Commands();  
    scan = new Scanner(System.in); 

    while(true){ 


     String s = scan.nextLine(); 

     switch(s){ 
     case("help"): 
      com.help(); 
     break; 

     case(s.substring(0, 3).equals("ipA")): 
      com.ipA(s.substring(5)); 
     break; 

     case(s.substring(0, 4).equals("ipB1")): 
      com.ipA(s.substring(6)); 
     break; 

     case(s.substring(0, 4).equals("ipB2")): 
      com.ipA(s.substring(6)); 
     break; 

     case(s.substring(0, 3).equals("ipC")): 
      com.ipA(s.substring(5)); 
     break; 

     default: 
      System.out.println("Wrong!!!"); 
     break; 
     } 







    } 
} 

}

+1

Javaのような 'switch'を使うことはできません。代わりに 'if'文を使うことができます。 – khelwood

+0

さて、if文で試してみます。ありがとう! –

+1

'String'で' startsWith'を見てください。 – Mark

答えて

1

を働いていない:。

public static void main(String[] args) { 


    com = new Commands(); 
    scan = new Scanner(System.in); 


    while (true) { 

     String o = scan.nextLine(); 
     String s = o.split(" ")[0]; 

     switch (s) { 
     case ("help"): 
      com.help(); 
      break; 

     case ("ipA"): 
      com.ipA(o.substring(4)); 
      break; 

     case ("ipB1"): 
      com.ipA(o.substring(5)); 
      break; 

     case ("ipB2"): 
      com.ipA(o.substring(5)); 
      break; 

     case ("ipC"): 
      com.ipA(o.substring(4)); 
      break; 

     default: 
      System.out.println("Wrong!!!"); 
      break; 
     } 

    } 
} 

あなたはcase内部で使用される式はブール式であるしかし、あなたのスイッチを持つように文字列に基づいていますswitch(s)ですので、あなたの場合もStringでなければなりません。

1

ifステートメントによる解決方法です。if() 0123すべての可能な条件をカバーするために、、最後にelse {}。最初のtrueの結果が実行され、残りは実行時には実行されません。

package stackoverflow_Question39568263; 

import java.util.Scanner; 

public class Main { 

    public static void main(final String[] args) { 
     final Scanner scan; 
     final Commands com; 

     com = new Commands(); 
     scan = new Scanner(System.in); 

     while (true) {  
      final String s = scan.nextLine(); 

      if (s == "help") { 
       com.help(); 
      } 
      else if (s.substring(0, 3).equals("ipA")) { 
       com.ipA(s.substring(5)); 
      } 
      else if (s.substring(0, 4).equals("ipB1")) { 
       com.ipA(s.substring(6)); 
      } 
      else if (s.substring(0, 4).equals("ipB2")) { 
       com.ipA(s.substring(6)); 
      } 
      else if (s.substring(0, 3).equals("ipC")) { 
       com.ipA(s.substring(5)); 
      } else { 
       System.out.println("Wrong!!!"); 
      } 
     } 
    } 
} 
0

ストリーミング処理を実装できます。主な利点は - 取り扱いのあなたのコードは、スイッチケースの代わりに実施した後、変更されません。

クラスActionがのマッチャーで入力文字列に適用あなたのアクションです:

abstract class Action { 
    public static final Action WRONG_ACTION = new Action() { 
     @Override 
     public void apply(String s) { 
      System.out.println("Wrong"); 
     } 
    }; 

    private Pattern pattern; 

    public Action() { 
    } 
    public Action(String pattern) { 
     this.pattern = Pattern.compile(pattern); 
    } 
    public boolean isApplicable(String s) { 
     return pattern.matcher(s).matches(); 
    } 

    abstract public void apply(String s); 

}

取り扱い

private List<Action> actions; 

actions.stream() 
     .filter(x -> x.isApplicable(s)) 
     .findFirst() 
     .orElse(Action.WRONG_ACTION) 
     .apply(s); 

Action例:

のように見えます
関連する問題