0

我知道它正在读取文件,因为我让它将 txt 文件的内容打印到控制台,但每次我尝试 .equals 时,变量都不会更改为 true/false,只是保持 true 有什么想法吗?

public static void readWebPage() {
      URLConnection connection;
      try {
       connection = new URL("https://dl.dropbox.com/u/40562795/List.txt").openConnection();
       @SuppressWarnings("resource")
       Scanner scanner = new Scanner(connection.getInputStream());
       scanner.useDelimiter("\\z");
       String text = scanner.next();
        System.out.println(text);
        if(text.equals("stop")){
            stop = true;
            System.out.println("Successfully stopped.");
        }else{ if(text.equals("-"))
            stop = false;
            System.out.println("Successfully started.");
        }
      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
 }

编辑:

那行得通,它现在可以读取它,但是我的变量没有更新为真/假。它保持在 true 左右,这在控制台中说。

if(stop = false){
    System.out.println("stop = false.");
}else
if(stop = true){
    System.out.println("stop = true.");
}

我的变量是如何制作的:

 public static boolean stop = false;

这就是我制作变量的方式,它应该 = false 但停止也不 - 更改它。我在我的 java 文件中搜索了可能触发它的东西,但找不到任何东西。

4

1 回答 1

1

-远程文本文件中的字符后有一个尾随空格。您可以使用空格分隔符而不是行终止符分隔符。代替

scanner.useDelimiter("\\z");

scanner.useDelimiter("\\s+");

以便您的支票text与您的.equals支票相符。

编辑:

从编辑中,您的if语句表达式中有一个赋值:

if (stop = false) {

用。。。来代替

if (stop == false) {

或更好

if (!stop) {

总的来说,这个if语句是不必要的,你可以简单地写

System.out.println("stop = " + stop);
于 2013-02-02T22:06:30.740 回答