0

I know it's reading the file because I have it print the contents of the txt file to the console but everytime I try .equals the variable doesn't change to true/false just stays at true any ideas?

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();
      }
 }

Edit:

That worked it's able to read it now, but my variable isn't updating to true/false. It stays at true or so this says in the console.

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

How my variable is made:

 public static boolean stop = false;

That's how I've made the variable and it should = false but stop nor - change it. I searched my java files for something that may be triggering it to true and couldn't find anything.

Reimeus
  • 158,255
  • 15
  • 216
  • 276

1 Answers1

1

There is a trailing space after the - character in the remote text file. You could use a whitespace delimiter rather than a line terminator delimiter. Replace

scanner.useDelimiter("\\z");

with

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

so that your text will match your .equals checks.

Edit:

From the edit, you have an assignment in your if statement expression:

if (stop = false) {

replace with

if (stop == false) {

or better

if (!stop) {

Overall this if statement is unnecessary, you can simply write

System.out.println("stop = " + stop);
Reimeus
  • 158,255
  • 15
  • 216
  • 276