Results 1 to 5 of 5
  1. #1
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Unhappy errors with do while loop

    I am having some troubles with my code:

    Java Code:
        do {
          String title;
    
          System.out.println("What is the books title?     ");
          title = EasyInput.getln();
          // adds .txt to the string, since each book will have a text file with
          // its filename as the book title, and .txt as the file extension
    
          String title2 = title + ".txt";
          File title3 = new File(title2);
          // this code from try to e.printStackTrace() is from here:
    
          try {
    
            // Create a new Scanner object which will read the data from //the
            // file passed in. To check if there are more line to read from //it
            // we check by calling the scanner.hasNextLine() method. We
            // then read line one by one till all line is read.
    
            Scanner scanner = new Scanner(title3);
            while (scanner.hasNextLine()) {
              String line = scanner.nextLine();
              System.out.println(line);
              boolean qwerty123 = true;
            }
          } catch (FileNotFoundException e) {
            System.out
                .println("We could not find a title in the library"
                    + " matching that title, here are some books with similar "
                    + "titles: ");
    
            /*
             * this portion of code created by jordan force creates an array of
             * strings with all of the names of the files in the directory
             */
            File dir = new File("books");
            String[] ListFinal = dir.list();
            int abc = 0;
            for (int x = 0; x < ListFinal.length; x++)
    
            {
    
              String filename = ListFinal[x];
    
              // tests each filename to see if it contains the title that the user
              // inputed
              boolean hastitle = filename.contains(title);
              // if none of the files contain the given string, then tell the user
    
              if (!hastitle) {
              } else {
    
                System.out.println(filename);
              }
            }
            boolean qwerty123 = false;
          }
    
        } while (!qwerty123);
    when I run it, it gives me this error in the console:

    /Users/jordanforce/NetBeansProjects/Librarysearch/src/librarysearch/Main.java:78: cannot find symbol
    symbol : variable qwerty123
    location: class librarysearch.Main
    } while(!qwerty123);
    1 error
    BUILD FAILED (total time: 0 seconds)


    what am i doing wrong??? please help
    Last edited by Fubarable; 08-18-2009 at 10:23 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You're declaring the qwerty123 boolean variable within your catch block making it visible only in the catch block, and similarly making it non-existent outside of this block. This is known as a scope issue. The solution, declare it within the scope of where it will be needed, in this case, before the do statement. Also, I added code tags to your code to make it readable (and fixed some indentation problems). Please try to make your code presentable here and more will be willing to read it.

    Best of luck and HTH.

  3. #3
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Java Code:
         boolean qwerty123 = false;
        do {
          String title;
    
          System.out.println("What is the books title?     ");
          title = EasyInput.getln();
          // adds .txt to the string, since each book will have a text file with
          // its filename as the book title, and .txt as the file extension
    
          String title2 = title + ".txt";
          File title3 = new File(title2);
          // this code from try to e.printStackTrace() is from here:
    
          try {
    
            // Create a new Scanner object which will read the data from //the
            // file passed in. To check if there are more line to read from //it
            // we check by calling the scanner.hasNextLine() method. We
            // then read line one by one till all line is read.
    
            Scanner scanner = new Scanner(title3);
            while (scanner.hasNextLine()) {
              String line = scanner.nextLine();
              System.out.println(line);
              boolean qwerty123 = true;
            }
          } catch (FileNotFoundException e) {
            System.out
                .println("We could not find a title in the library"
                    + " matching that title, here are some books with similar "
                    + "titles: ");
    
            /*
             * this portion of code created by jordan force creates an array of
             * strings with all of the names of the files in the directory
             */
            File dir = new File("books");
            String[] ListFinal = dir.list();
            int abc = 0;
            for (int x = 0; x < ListFinal.length; x++)
    
            {
    
              String filename = ListFinal[x];
    
              // tests each filename to see if it contains the title that the user
              // inputed
              boolean hastitle = filename.contains(title);
              // if none of the files contain the given string, then tell the user
    
              if (!hastitle) {
              } else {
    
                System.out.println(filename);
              }
            }
          }
    
        } while (!qwerty123);
    That should work I guess.. I only moved the qwerty123

  4. #4
    Addez is offline Senior Member
    Join Date
    Aug 2009
    Posts
    294
    Rep Power
    0

    Default

    Sry I was completly wrong, I actually never read ur code.
    I think this one will work:

    Java Code:
    boolean qwerty123;  //Or assign it to true or false idk which u wanted..
    do {
          String title;
    
          System.out.println("What is the books title?     ");
          title = EasyInput.getln();
          // adds .txt to the string, since each book will have a text file with
          // its filename as the book title, and .txt as the file extension
    
          String title2 = title + ".txt";
          File title3 = new File(title2);
          // this code from try to e.printStackTrace() is from here:
    
          try {
    
            // Create a new Scanner object which will read the data from //the
            // file passed in. To check if there are more line to read from //it
            // we check by calling the scanner.hasNextLine() method. We
            // then read line one by one till all line is read.
    
            Scanner scanner = new Scanner(title3);
            while (scanner.hasNextLine()) {
              String line = scanner.nextLine();
              System.out.println(line);
              qwerty123 = true;
            }
          } catch (FileNotFoundException e) {
            System.out
                .println("We could not find a title in the library"
                    + " matching that title, here are some books with similar "
                    + "titles: ");
    
            /*
             * this portion of code created by jordan force creates an array of
             * strings with all of the names of the files in the directory
             */
            File dir = new File("books");
            String[] ListFinal = dir.list();
            int abc = 0;
            for (int x = 0; x < ListFinal.length; x++)
    
            {
    
              String filename = ListFinal[x];
    
              // tests each filename to see if it contains the title that the user
              // inputed
              boolean hastitle = filename.contains(title);
              // if none of the files contain the given string, then tell the user
    
              if (!hastitle) {
              } else {
    
                System.out.println(filename);
              }
            }
            qwerty123 = false;
          }
    
        } while (!qwerty123);

  5. #5
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

Similar Threads

  1. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 08:46 PM
  2. Errors.
    By rocky in forum New To Java
    Replies: 4
    Last Post: 04-09-2009, 08:05 AM
  3. Few errors with for loop/printf
    By ks1615 in forum New To Java
    Replies: 8
    Last Post: 03-12-2009, 09:47 PM
  4. Replies: 3
    Last Post: 03-08-2009, 01:51 AM
  5. help with these errors
    By oceansdepth in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 04:55 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •