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 8582469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:17:59+00:00 2026-06-11T21:17:59+00:00

This problem takes a bit of explaining, I’ll try to be as concise as

  • 0

This problem takes a bit of explaining, I’ll try to be as concise as possible:

I have am trying to initalise an array of Can objects, these objects only have 2 fields (both Strings): name, manufacturer

I am trying to initialise the fields by reading from a CSV file with the following format:

Tomatoes,Heinz

Legumes,Jerry

(no space between the lines, it’s being formatted like that on this site for some reason)

The first string in each row is the value I want to be the name, the 2nd is the manufacturer.

So I’ve created a method to read each line of the CSV, which passes each line to a tokenizer method to extract single values:

private void readFile (String inFilename) {
    FileInputStream fileStrm = null;
    InputStreamReader rdr;
    BufferedReader bufRdr;
    int lineNum;
    String line;
        try {
            fileStrm = new FileInputStream(inFilename);
            rdr = new InputStreamReader(fileStrm);
            bufRdr = new BufferedReader(rdr);
            lineNum = 0;
            line = bufRdr.readLine();
            while {line != null) {
                lineNum++;
                processLine(line);  //passes line to tokenizer
                line = bufRdr.readLine();
                 }
            fileStrm.close();
        }
        catch (IOException e) {
            if (fileStrm != null) {
                try { fileStrm.close(); } catch (IOException ex2) { }
             }
             System.out.println("Error in file processing: " + e.getMessage());
        }   
    }

The lines are passed to this tokenizer method:

private String processLine(String csvRow) {
    String thisToken = null;
    StringTokenizer strTok;
    strTok = new StringTokenizer(csvRow, ",");
        while (strTok.hasMoreTokens()) {
            thisToken = strTok.nextToken();
        }
}

And that’s where I get a bit stuck. To initialise my array I think I’d need a for loop, something like

for (int i=0; i<=array.length;i++)
    {
    array[i].name = readFile("filename.csv");
    array[i].manufacturer = readFile("filename.csv");
    }

But obviously this will not work. Can anyone suggest how I can go about this? I’d prefer to keep the code mostly intact and figure out a solution using the existing code.

Thanks

  • 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-11T21:18:00+00:00Added an answer on June 11, 2026 at 9:18 pm

    First thing: –

    You are calling processLine(line);, but are not returning the token read from this method.. So, the token obtained in this method in engulped there only.. So, you should return something from that method..

    Second:-

    array[i].name = readFile("filename.csv");
    array[i].manufacturer = readFile("filename.csv");
    

    In the above code, you are calling readFile() each time for the two attributes.. So, even if you return somthing, these two attributes will be initialized to same value.. Because each time you are starting reading file from scratch..

    Third thing: –

    In fact your above code will not compile.. Because you are assigning the value of readFile() (which is actually not returning anything) to array.. So give a return type to this method.. It would be String.. And returning the tokens read..

    EDIT: –
    *
    I would suggest, you can use split() method of String class.. Tokenizer is not needed here, for justsplittingaround a singlecomma(,)`

    Also, rather than using an array, you can use ArrayList, in which you can add your newly created object on the fly.. That way, you will not have to fix the size of array.. (And this is what you will want, as you don’t know how much line you will have in your file right?)

    Here’s what you can do: –

    • Call the method readFile from somewhere, probably main()

      readFile("filename.csv")
      
    • In your readFile() method, you can iterate over file to create an ArrayList like this: –

      List<Can> yourList = new ArrayList<>();
      while ((line = reader.readLine()) != null) {
      
          String[] wordRead = line.split(',');
          yourList.add(new Can(wordRead[0], wordRead[1]));
      }
      

    I assume, Can is the name of your class as you stated in your problem..

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

Sidebar

Related Questions

Problem Hello all! I have this code which takes my jpg image loops through
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
This problem is driving me a bit insane - I have spent the best
I'm having a problem with this query that takes several seconds to complete. I
I need some help in getting this right, problem Write a function which takes
could anyone take a look at this for me? Problem: trying to output a
This is my first post here. I have a problem. I need to take
I have problem with python multithreaded Queues. I have this script, where producer take
I have this problem with a link in an HTML page where the first
The title sucks, I'm sorry. It takes a little bit to setup my problem,

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.