0
public void sendData(){
         try {
            URL data = new URL("http://mywebsite.net/isvalid.php?username=" + usernameField.getText() + "&password=" + passwordField.getText());
            BufferedReader in = new BufferedReader(
            new InputStreamReader(data.openStream()));

            String inputLine;
            while ((inputLine = in.readLine()) != null)
             if(inputLine.length() > 0)
              inputString = inputLine;
                in.close();
            if(!inputString.equals("Incorrect password.")){
                System.out.println("Correct password");
                run();
                dispose();
            } else 
                if(usernameField.getText().length() > 0 && passwordField.getText().length() > 0) {
                invalidUP();
                System.out.println("Invalid username or password.");
                }  else
                    if(usernameField.getText().length() < 1 || (passwordField.getText().length() < 1)) {
                        System.out.println("No password or username entered.");
                        upLength();
            }
         } catch (IOException e) {
       e.printStackTrace();
         }
     }

如何检查 usernameField 或 passwordField 中是否有空格?如果有,请将其替换为“_”。

另外,如果您认为这种方法发送数据是错误的,或者它可以更容易/更快地完成,请详细说明。

4

4 回答 4

0

使用String#replaceAll()

String uname ="abc xyz";
uname = uname.replaceAll("\\s+", "_");
于 2013-04-09T16:15:16.440 回答
0

如果您的字符串中只有空格,请尝试以下操作:

username = username.replace(" " , "_");

否则,如果您的用户名中有空格,请尝试以下操作:

username = username.replaceAll("\\s+" , "_");
于 2013-04-09T16:15:28.433 回答
0

String.replace

passwordField = passwordField.replace(' ', '_');

于 2013-04-09T16:15:30.480 回答
0

java.lang.String 有一个名为replaceAll的好方法。

于 2013-04-09T16:15:32.973 回答