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

    Unhappy Problems with code

    So, I am making a book organizer in java, and I have this part which does a title search and I keep getting errors when I run the code:




    import java.io.*;
    import java.util.Scanner;
    import java.util.ArrayList;
    import javax.swing.JFileChooser;

    public class Main
    {
    public static void main(String[] args) {

    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);
    }
    } 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 */


    This Is where I start to have issues:


    String[] list = new list();
    int length = list.length;
    int i = 0;
    for (int x = 0; x<=length; x++)
    {
    i++;
    //tests each filename to see if it contains the title that the user inputed
    boolean hastitle = list.contains(title);
    //if none of the files contain the given string, then tell the user
    if(!hastitle)
    {
    System.out.println("sorry, none of the books were related to your query");
    }else{
    System.out.println(String[i]);
    }
    };
    System.out.println("now type in the title of the book from the list, exactly as it is written");
    String title9 = EasyInput.getln();
    String title10 = title9 + ".txt";
    File title11 = new File(title10);
    //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(title11);
    while (scanner.hasNextLine()) {
    String line = scanner.nextLine();
    System.out.println(line);
    }
    } catch (FileNotFoundException j) {
    j.printStackTrace();
    }


    }
    }
    }


    here are the errors I get from netbeans:


    Compiling 1 source file to /Users/jordanforce/NetBeansProjects/Librarysearch/build/classes
    /Users/jordanforce/NetBeansProjects/Librarysearch/src/librarysearch/Main.java:51: cannot find symbol
    symbol : class List
    location: class librarysearch.Main
    String[] list = new List();
    /Users/jordanforce/NetBeansProjects/Librarysearch/src/librarysearch/Main.java:58: cannot find symbol
    symbol : method contains(java.lang.String)
    location: class java.lang.String[]
    boolean hastitle = list.contains(title);
    /Users/jordanforce/NetBeansProjects/Librarysearch/src/librarysearch/Main.java:64: cannot find symbol
    symbol : variable String
    location: class librarysearch.Main
    System.out.println(String[i]);
    3 errors
    BUILD FAILED (total time: 0 seconds)

    Can someone please help???

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

    Default

    What are you expecting new list() to do? You appear to be trying to call a constructor on a non-existent class and expect it to return an array of String which goes against basic Java precepts.

  3. #3
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    /Users/jordanforce/NetBeansProjects/Librarysearch/src/librarysearch/Main.java:51: cannot find symbol
    symbol : class List
    location: class librarysearch.Main
    String[] list = new List();

    You are trying yo assign a new instance of List class which you did not even declare or import to the variable list which is an array of type String. When you have declared an object of a certain type, you have to assign to it a value of the same type. For example use its constructor or constructors of the related class to initialize it. A String and List classes are different. So in this case maybe the correct declaration should be:

    Java Code:
    String[] list = new String[5]; where 5 is the size of the array.
    Is that what you are trying to achieve maybe?
    Tshegofatso Manakana
    a.k.a Untouchable

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

    Default

    What im trying to do is list the names of the files a directory, and put them into an array of strings

    I found the list() thing on the sun website:

    java.sun.com/j2se/1.4.2/docs/api/java/io/File.html#list%28%29

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

    Default Worked out most of the bugs

    I worked out most of the bugs in the code

    heres the new code:

    File dir = new File("books");
    String[] list = dir.list();
    int length = list.length;
    int i = 0;
    for (int x = 0; x<=length; x++)
    {
    String filename = list[i];
    i++;
    //tests each filename to see if it contains the title that the user inputed
    boolean hastitle = list.contains("thing");
    //if none of the files contain the given string, then tell the user
    if(!hastitle)
    {
    System.out.println("sorry, none of the books were related to your query");
    }else{
    System.out.println(filename);
    }
    };



    the only error is that contains("thing") isn't working, it says it cant find the symbol for contains().

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

    Default

    okay now the only issue is when I try to get the length of an array

    I have this peice of code:
    int length = list.length;

    and when run it returns this:

    Exception in thread "main" java.lang.NullPointerException
    at librarysearch.Main.main(Main.java:53)

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

    Default

    Your problem is that you're trying to get the length field of a null object. But why is the array null? Answer: You appear to be trying to call list() on a File that is not a directory. Please read the File API to explain why list() is returning null.

    File (Java Platform SE 6)

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

    Default

    thanks, I think this is gonna be the last issue that I will have

    When I run the String filename = ListFinal[x], it gives me this exception:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5

    for (int x = 0; x <= LengthFinal; x++)
    {

    String filename = ListFinal[x];

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

    Default

    Arrays are 0 based and go from 0 to Array.length - 1. Thus your for loop should look like:
    Java Code:
    for (int x = 0; x < list.length; x++) 
    {

Similar Threads

  1. Convert java code to midlet code
    By coldvoice05 in forum New To Java
    Replies: 1
    Last Post: 08-12-2009, 11:14 AM
  2. Convert java code to midlet code
    By coldvoice05 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 01:21 PM
  3. Why doesn't this code accept my code?
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:39 PM
  4. problems with java code! (very new - need help asap!)
    By sumkindafreek in forum New To Java
    Replies: 1
    Last Post: 01-07-2009, 05:00 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 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
  •