Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:24:28+00:00 2026-06-01T06:24:28+00:00

So I have the following read and write file program for an Array. How

  • 0

So I have the following read and write file program for an Array. How would the following look like for an ArrayList? I am having trouble with the syntax. I know an ArrayList would be like that: private ArrayList prod list = new ArrayList();
BUt how would the read/write IO syntax be? Thank you.

    static ActionProduct[] prodlist = new ActionProduct[50];
    static String filename = System.getProperty("user.dir") + "\\src\\product.txt";
    static int pIndex=0;


public static void readFile() throws IOException {
        // input file must be supplied in the first argument
        InputStream istream;
        if (filename.length() > 0) {
            File inputFile = new File(filename);
            istream = new FileInputStream(inputFile);
        } else {
            // if no filename, use standard input stream
            istream = System.in;
        }

        // use a buffered reader for line-at-a-time
        // reading
        BufferedReader lineReader;
        lineReader = new BufferedReader(new InputStreamReader(istream));

        // read one line at a time
        String line;
        while ((line = lineReader.readLine()) != null) {

            StringTokenizer tokens = new StringTokenizer(line, "\t");

            // String tmp = tokens.nextToken();
            // System.out.println("token " + tmp);
            prodlist[pIndex] = new ActionProduct();
            String category = prodlist[pIndex].getCategory();
            category = tokens.nextToken();
            System.out.println("got category " +category);

            int item = prodlist[pIndex].getItem();
            item = Integer.parseInt(tokens.nextToken());

            String name = prodlist[pIndex].getName();
            System.out.println("got name " +name);

            double price = prodlist[pIndex].getPrice();
            price = Double.parseDouble(tokens.nextToken());

            int units = prodlist[pIndex].getUnits();
            units = Integer.parseInt(tokens.nextToken());
            pIndex++;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:24:29+00:00Added an answer on June 1, 2026 at 6:24 am

    Replace this:

    static ActionProduct[] prodlist = new ActionProduct[50];
    

    with

    static List<ActionProduct> prodlist = new ArrayList<ActionProduct>();
    

    then this:

            prodlist[pIndex] = new ActionProduct();
    

    with

            ActionProduct p = new ActionProduct();
            prodlist.add(p);
    

    Finally, replace all uses of prodlist[pIndex] with p, then get rid of pIndex everywhere else.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to write a program that read from a file that contains the
I have the following setup in Oracle, i.e.: (‘aXYZApple-Au’, ‘aXYZOrange-Au’,’aXYZApple-Readonly’,’aXYZOrange-Write’, ’aXYZApple-Write’, ’aXYZOrange-Read’) What I
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
Having read around it appears this should be possible. I have the following content
Have a look at the following code you are not required to read the
I would like to know how I could generate code from a text file
I'd like to know if there exists a program that can read DTD specifications,
I have a program, which writes data to a text file in the following
I have the following code: using (BinaryReader br = new BinaryReader( File.Open(FILE_PATH, FileMode.Open, FileAccess.ReadWrite)))
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.