Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java Problem with ArrayLists

Options
  • 30-04-2009 6:57pm
    #1
    Registered Users Posts: 342 ✭✭


    Hi,I'm making a program where i have a text file with a name and number on each line..is there a way to read the number into the arrayList as an int,or even later convert it to an int?


Comments

  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Is it a String on the first place? You could use Integer.parseInt(string);


  • Registered Users Posts: 342 ✭✭thomasjad


    well the text file looks like
    john 15
    bob -3
    etc..
    i dont know how long the text file will be,i want to read the numbers into an arraylist,and be able to add or subtract to them..


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    http://java.sun.com/docs/books/tutorial/essential/io/

    Should give you everything you need in terms of File IO and such. After that it should be pretty trivial to get your file into whatever data structure you're interested in.

    If you want more specific help I would suggest posting some of your attempt code if you run into problems.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    Check out the Java Scanner class, it's what you're looking for (you need to be in Java 6 land)

    So you're looking for something like:
    class DataPair{
        public int number;
        public String name;
    }
    
    ....
    Set<DataPair> pairs = new HashSet()<DataPair>;
    while(scanner.hasNext()){
       <make new pair, read the ints using read int and the strings using read string, and add to the set>
    }
    ...
    


Advertisement