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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:37:13+00:00 2026-05-14T20:37:13+00:00

I have CSV file with sample data in this form : 220 30 255

  • 0

I have CSV file with sample data in this form :

220 30    255   0   0     Javascript
200 20      0 255 128     Thinking in java

, where the first column is height, second thickness, next three are rgb values for color and last one is title. All need to be treated as separate variables. I have already written my own solution for this, but I’m wondering if there are no better/easier/shorter ways of doing this. Extracted data will then be used to create Book object, throw every Book into array of books and print it with swing.
Here’s the code :

private static Book[] addBook(Book b, Book[] bookTab){
        Book[] tmp = bookTab;
        bookTab = new Book[tmp.length+1];
        for(int i = 0; i < tmp.length; i++){
                bookTab[i] = tmp[i];
        }
        bookTab[tmp.length] = b;

        return bookTab;
}

public static void main(String[] args) {

    Book[] books = new Book[0];

    try {
        BufferedReader file = new BufferedReader(new FileReader("K:\\books.txt"));

        String s;
        while ((s = file.readLine()) != null) {
            int hei, thick, R, G, B;
            String tit;

            hei = Integer.parseInt(s.substring(0, 3).replaceAll(" ", ""));
            thick = Integer.parseInt(s.substring(4, 6).replaceAll(" ", ""));
            R = Integer.parseInt(s.substring(10, 13).replaceAll(" ", ""));
            G = Integer.parseInt(s.substring(14, 17).replaceAll(" ", ""));
            B = Integer.parseInt(s.substring(18, 21).replaceAll(" ", ""));

            tit = s.substring(26);

            System.out.println(tyt+wys+grb+R+G+B);

            books = addBook(new Book(wys, grb, R, G, B, tyt),books);
        }
        file.close();
    } catch (IOException e) {
        //do nothing
    }
}
  • 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-05-14T20:37:14+00:00Added an answer on May 14, 2026 at 8:37 pm

    I have CSV file with sample data in this form

    That’s not a CSV file. That’s a “fixed-width formatted” file.

    I’m wondering if there are no better/easier/shorter ways of doing this

    Use a real CSV file format. Then parsing/formatting would be easy with lot of available Java CSV API’s. For example OpenCSV. You can even use it to convert between a List of beans (like as Book in your case) and a CSV file.

    (from a comment) the file is already created and I must keep it in that form. What about regex ?

    Regex would only make things worse, since it’s not in a regular format, but in a fixed format! If you can’t change the format, even not to CSV, then, well, your approach is as far fine. I would only replace replaceAll(" ", "") by trim() since that’s efficienter (the one is regex, the other is just parsing). Replacing Book[] by List<Book> is also a good suggestion, it’s more easy to add another book. You can then just do books.add(book). Also see the Collections tutorial.

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

Sidebar

Ask A Question

Stats

  • Questions 408k
  • Answers 408k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Create a CIImage for the CGImage of the bitmap image… May 15, 2026 at 6:45 am
  • Editorial Team
    Editorial Team added an answer From your requirements I think you might be looking for… May 15, 2026 at 6:45 am
  • Editorial Team
    Editorial Team added an answer The hibernate-c3p0.jar provides the class org.hibernate.connection.C3P0ConnectionProvider, a C3P0-based implementation of… May 15, 2026 at 6:45 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.