2013-10-20 1 views
0

코딩의이 섹션이 작동하지 않는 이유와 프로그램이 스위치에서 동일한 오류를 계속 발생시키는 이유를 알고 싶습니다.디스플레이 파일 이름 readline with switch case android

내가 TXT 파일로 저장 한 파일을 읽고에 표시 할

을 (컨버터블 INT 값 또는 열거 형 변수 만. 1.7 아래의 소스 레벨에 대한 String 형의 값을 전환 할 수 허용됩니다) 프로그램.

예 : "이메일"을 사례로 써서 원하는 내용을 썼다가이 스위치에서 읽을 수 있도록 txt 파일에 저장하십시오.

누구든지이 문제를 해결할 수 있습니까? 깊이 감사드립니다. 감사. 여기

내 코드입니다 :

 private void ExecuteCommands(String filename) { 
    //Find the directory for the SD Card using the API 
    //*Don't* hardcode "/sdcard" 
    File sdcard = Environment.getExternalStorageDirectory(); 

    //Get the text file 
    File file = new File(sdcard, filename + ".txt"); 

    //Read text from file 
    StringBuilder text = new StringBuilder(); 

    try { 
     BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 

     while ((line = br.readLine()) != null) { 
      String[] tmp = line.split(" "); 

         //this switch case giving me problem 
         switch(tmp[0]){ 

      case "Email": 
       String subject = sbj.getText().toString(); 
       String message = messageBody.getText().toString(); 
       String to = destinationAddress.getText().toString(); 

       Intent emailActivity = new Intent(Intent.ACTION_SEND); 

       //set up the recipient address 
       emailActivity.putExtra(Intent.EXTRA_EMAIL, new String[] { to }); 

       //set up the email subject 
       emailActivity.putExtra(Intent.EXTRA_SUBJECT, subject); 

       //you can specify cc addresses as well 
       // email.putExtra(Intent.EXTRA_CC, new String[]{ ...}); 
       // email.putExtra(Intent.EXTRA_BCC, new String[]{ ... }); 

       //set up the message body 
       emailActivity.putExtra(Intent.EXTRA_TEXT, message); 

       emailActivity.setType("message/rfc822"); 

       startActivity(Intent.createChooser(emailActivity, "Select your Email Provider :")); 
       break; 

      case "SMS message": 
      String phoneNo = textPhoneNo.getText().toString(); 
       String sms = textSMS.getText().toString(); 

       try { 
       SmsManager smsManager = SmsManager.getDefault(); 
       smsManager.sendTextMessage(phoneNo, null, sms, null, null); 
       Toast.makeText(getApplicationContext(), "SMS Sent!", 
          Toast.LENGTH_LONG).show(); 
       } catch (Exception e) { 
       Toast.makeText(getApplicationContext(), 
        "SMS faild, please try again later!", 
        Toast.LENGTH_LONG).show(); 
       e.printStackTrace(); 


       break; 
       } 
      } 
     } 


    } 



     catch (IOException e) { 
     //You'll need to add proper error handling here 
    } 

} 당신이 당신의 스위치 케이스 스트링 수에 포함하는 방법, (7)에서 Java 버전 문자열에 스위치/케이스를 사용 열거를 고려,하지만 어떻게

답변

1

공백은 열거 형 상수를 검색 할 수 없지만 특정 문자열을 기준으로 해당 열거 형을 검색하는 자체 메서드를 추가 할 수 있습니다. 이렇게.

enum Type { 

EMAIL { 
    @Override 
    public boolean counterpart(String value) { 
     if (value.equals(EMAIL)) { 
      return true; 
     } 
     return false; 
    } 
}, 
SMS { 
    @Override 
    public boolean counterpart(String value) { 
     if (value.equals(SMS_TAG)) { 
      return true; 
     } 
     return false; 
    } 
}; 
private static final String EMAIL_TAG = "Email"; 
private static final String SMS_TAG = "SMS Message"; 

public abstract boolean counterpart(String value); 

} 및 문자열 값을 기준으로 맞습니다 형식을 반환 어디서나 공공 정적 방법.

public static Type getType(String value) { 
    for (Type t : Type.values()) { 
     if (t.counterpart(value)) { 
      return t; 
     } 
    } 
    return Type.EMAIL; 
} 

그런 다음이

Type type = getType(param[ 0 ]); 

    switch(type){ 
     case EMAIL: 
      break; 
     case SMS: 
      break; 
     default: 
      break; 
    } 
같은 swicth을 가져야한다