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是否有空格?如果有,请将其替换为“_”。
另外,如果您认为此方法发送数据有误或者可以更容易/更快地完成,请详细说明。
答案 0 :(得分:0)
String uname ="abc xyz";
uname = uname.replaceAll("\\s+", "_");
答案 1 :(得分:0)
如果您的String中只有空格,请尝试:
username = username.replace(" " , "_");
否则,如果您的用户名中有空格,请尝试以下操作:
username = username.replaceAll("\\s+" , "_");
答案 2 :(得分:0)
查看String.replace
:
passwordField = passwordField.replace('','_');
答案 3 :(得分:0)
java.lang.String有一个名为replaceAll的好方法。