0
public BookmarkList(String bookmarkFileName) {
    File file = new File(bookmarkFileName); 
    int lineCount = 0; 
    int index = 0;      
    Scanner input = new Scanner(System.in);
    try {
        input = new Scanner(file);
    }
    catch(Exception e) {
        
        System.out.println("Unknown BookmarkList data File");
    }
    while(input.hasNext()) {
        
        String line = input.nextLine();
        
        if(line.length() != 0) {
            
            if(!(line.charAt(0) == '/')) {
                lineCount++; 
            }
        }
    }       
    bookmarks = new Bookmark[lineCount];    
    try {
        input = new Scanner(file);
    }
    catch(Exception e) {
        System.out.println(e);
    }
    while(input.hasNext()) {
        String line = input.nextLine();
        String[] el = new String[5]; 
        if(line.length() != 0) { 
            if(!(line.charAt(0) == '/')) { 
                if(line.contains(",")) { 
                    el = line.split(",",-1); 
                }
                else if(line.contains(";")) { 
                    el = line.split(";",-1); 
                }
                
                for(int i = 0; i < el.length; i++) {
                    el[i] = el[i].replace(" ","");
                }
                Bookmark bookmark = new Bookmark(el[0], el[1], el[2], el[3], el[4]); 
                bookmarks[index] = bookmark; 
                index++; 
            }
        }
    }
}   
public Bookmark(String name, String time, String url, String groupName, String memo) {
    try {
        this.name = name;
        this.time = time;
        this.url = url;
        this.groupName = groupName;
        this.memo = memo;           
        if(this.url.length() == 0 || !checkDateFormat(this.time)) {
            throw new Exception();
        }
    } catch(Exception e) {      
        if(this.url.length() == 0) {
            System.out.print("MalformedURLException: wrong URL - No URL ; invalid Bookmark info line: ");
            System.out.println(this.name + "," + this.time + "," + this.url + "," + this.groupName + "," + this.memo);
        }       
        if(!checkDateFormat(this.time)) {
            System.out.print("Date Format Error -> No Created Time invalid Bookmark info line: ");
            System.out.println(this.name + "," + this.time + "," + this.url + "," + this.groupName + "," + this.memo);
        }
    }
}

So the Bookmark Class defines the bookmarks which are defined by Group, Name, URL, Time, and Memo. The BookmarkList Class saves these bookmarks in an array, and it get's it's data from a text file, which contains some bookmarks. I want to add a new bookmark to BookmarkList, but it returns an error since I've defined Bookmarklist as bookmarks = new Bookmark[lineCount];, which limits the number of possible bookmarks to the ones in the text file. I thought of changing lineCount to 100, but that makes it difficult to get the length of BookmarkList, which I need to use, since .length will return 100. Can anybody help?

Dagun
  • 41
  • 5

1 Answers1

0

An array is fixed sized, you can't change the size later, however there is a class providing the functionality you are searching for. You can simply use an ArrayList. Define it like that ArrayList<Bookmark> bookmarks; to init it use bookmarks = new ArrayList(); and with bookmarks.add(bookmark); you can add new bookmarks to the list and then access it by bookmarks.get(index);

Sebastian
  • 153
  • 1
  • 10