Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am a fairly new programmer in Java. My question is...for the following text file contents:

http://textuploader.com/ga7x

I am trying to parse the book reference numbers (the integers) and the book titles (the strings) using a bufferedreader into separate arraylists and then later on transfer the contents of the arraylists into separate arrays.

I have tried the following:
Java
ArrayList<integer> tempBooksReference = new ArrayList<>();
ArrayList<string> tempBooksTitle = new ArrayList<>();

BufferedReader br = null;
try {

    br = new BufferedReader(new FileReader("booklist.txt"));
    String nextLine;
    String bookTitle;
    int bookNum;

    while ((nextLine = br.readLine()) != null) {

        bookNum = Integer.parseInt(nextLine);
        tempBooksReference.add(bookNum);
        bookTitle = nextLine;
        tempBooksTitle.add(bookTitle);

    }
} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        br.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Integer[] booksReference = new Integer[tempBooksReference.size()];
String[] booksTitle = new String[tempBooksTitle.size()];
tempBooksReference.toArray(booksReference);
tempBooksTitle.toArray(booksTitle);

Unfortunately it doesn't seem to work. Can someone please tell me how I would go about doing this using BufferedReader (I would like to use BufferedReader rather than a Scanner to accomplish this task, although if I can figure out how to do it using BufferedReader, then I would be more than happy to learn how to do this via a Scanner also)?

Thank you very much!
Posted
Updated 5-May-15 2:40am
v3
Comments
Shubhashish_Mandal 7-May-15 3:38am    
"Unfortunately it doesn't seem to work" .. What kind of o/p you have getting?

1 solution

List<Integer> tempBooksReference = new ArrayList<>();
List<String> tempBooksTitle = new ArrayList<>();
 
BufferedReader br = null;
try 
{
 
    br = new BufferedReader(new FileReader("C:\\Users\\SGGS\\Documents\\NetBeansProjects\\JavaApplication3\\src\\javaapplication3\\input"));

\*Here you can give full path of your input file or you have to resolve it using URL class method check documentation of URL class and pay attention to cases it String and Integer in Generics*/


    String nextLine;
    String bookTitle;
    int bookNum;
 
    while ((nextLine = br.readLine()) != null) {
 
        bookNum=Integer.parseInt(nextLine);
        tempBooksReference.add(bookNum);
        bookTitle=br.readLine();
        tempBooksTitle.add(bookTitle);
 
    }
    System.out.println(tempBooksTitle.size());
     br.close();
     Integer[] booksReference = new Integer[tempBooksReference.size()];
     String[] booksTitle = new String[tempBooksTitle.size()];
     tempBooksReference.toArray(booksReference);
     tempBooksTitle.toArray(booksTitle);
     System.out.println(Arrays.toString(booksTitle));
     
}
catch (IOException e)
{
    System.out.println(e.getMessage());
} 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900