1,105,263 Community Members

problem import text and conbined with ArrayList

Member Avatar
chmo
Newbie Poster
3 posts since Nov 2010
Reputation Points: -2 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi everyone....
I want to develop a music lexicon (tyoe band name --> give description) and I want to add terms (bandName and description)
I use ArrayList but I want to change. I have also the code that read a text file (bands.txt) but I dont know haw it works...
Any suggestion?

private void addTerms () 
    {
        addTerm("Anathema","A music band from Liverpool, UK.");
        
    }
import java.io.*;
   
   public class ReadFile
   {
       public static void main(String[] args)
       {
           try 
           {
              //Sets up a file reader to read the file passed on the command line one character at a time
              FileReader input = new FileReader("bands.txt");
              //Filter FileReader through a Buffered read to read a line at a time 
              BufferedReader bufRead = new BufferedReader(input);
              
              String line;    // String that holds current file line
              int count = 0;  // Line number of count 
              
               while((line = bufRead.readLine()) != null){
                   System.out.println(count+": "+line);
                   line = bufRead.readLine();
                   count++
                  }
                  
              bufRead.close();
           }
           catch (ArrayIndexOutOfBoundsException e){
              /* If no file was passed on the command line, this expception is
              generated. A message indicating how to the class should be
              called is displayed */
              System.out.println("Usage: java ReadFile filename\n");          
  
          }catch (IOException e){
              // If another exception is generated, print a stack trace
              e.printStackTrace();
          }
      }
}
Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Yeah, actually learn the language (there are plenty of tutorials and API docs) rather than simply copying random blocks of code.

Why don't you tell us what you think that code is doing (line for line), see the API docs for help. At least that way you will have to actually analyse the code and learn what it does so that you are actually capable of using and modifying it, rather than just having others provide you with code that won't help you in future endeavors.

Member Avatar
Eric Cute
Posting Whiz in Training
239 posts since Jun 2010
Reputation Points: 33 [?]
Q&As Helped to Solve: 20 [?]
Skill Endorsements: 3 [?]
 
1
 

So you want to prompt your user to provide a band name and band description, then after you want to add it into your bands.txt?

Member Avatar
Taywin
Posting Maven
2,632 posts since Apr 2010
Reputation Points: 134 [?]
Q&As Helped to Solve: 378 [?]
Skill Endorsements: 17 [?]
 
0
 

Line 10, you hard-coded the file you want to read in, but in line 29, your usage requires a file name? :P

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Essentially crossposts
Number 1
Number 2
Number 3
Number 4

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

Possibly another.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: