When enter input and check for the result it only show the function from else. How can i fix it. it skips if and else if and directly goes to else statement.
Here is my code
JavaScript
x
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_v);
//start for web view
webView = (WebView) findViewById(R.id.webview);
//fit screen
webView.setInitialScale(1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
//end for web view
//received data from search book
String passURL = getIntent().getStringExtra("key");
if(passURL.matches("0-9") && passURL.length()>= 10){
webView.loadUrl("http://www.librarything.com/isbn/" + passURL); //load website
}else if (passURL.matches("[a-zA-Z]")){
webView.loadUrl("http://www.librarything.com/title/" + passURL); //load website
}else {
Toast.makeText(this, "Wrong Input", Toast.LENGTH_LONG).show();
}
}
Advertisement
Answer
I think you have to change your code to the following:
JavaScript
if(passURL.matches("[0-9]+") && passURL.length()>= 10){
webView.loadUrl("http://www.librarything.com/isbn/" + passURL); //load website
}else if (passURL.matches("[a-zA-Z]+")){
webView.loadUrl("http://www.librarything.com/title/" + passURL); //load website
}else {
Toast.makeText(this, "Wrong Input", Toast.LENGTH_LONG).show();
}
Edit: (please correct me if im wrong)
As you can see in the answer from @datenwolf you have to put [0-9] in brackets so it checks if the string contains those numbers (or letters for [a-zA-Z]). The plus (+) is required to check if the regex matches (at least) one or multiple times