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();
          }
      }
}

Recommended Answers

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

Jump to Post

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?

Jump to Post

All 5 Replies

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.

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?

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

Possibly another.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.