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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:26:56+00:00 2026-05-31T15:26:56+00:00

I have a text file with about 200 item numbers and descriptions, which is

  • 0

I have a text file with about 200 item numbers and descriptions, which is formatted like this (without the bullets):

  • 1642 Pure wool t-shirt
  • 613 Red laced shoes
  • 3477 Blue hat with feather
  • …

I am trying to read and store the items numbers and descriptions into respective arrays, which is delimited by space. My issues are:

  1. I want to ignore the spaces in the description.
  2. When I sort or delete items, I want to ensure that the description gets deleted as well.

Here’s what I have tried so far but getting ArrayIndexOutOfBoundsException error and I am not even sure if it will read the description properly:

private Scanner file;
private int item = 0;
private String desc = "";
private int[] itemArr = new int[200];
private String[] descArr = new String[200];
int n = 0;


public void openFile(){

    try{
        file = new Scanner(new File("inventory.txt"));
    }

    catch(Exception e){
        System.out.println("file not found");
    }

}

public void readFile(){         

    while(file.hasNextLine()){    
        if (file.hasNextInt()){     
            item = file.nextInt();
        }

        while(!file.hasNextInt() && !file.hasNextLine()){
            desc = desc + file.next() + " ";
        }

        itemArr[n] = item;
        descArr[n] = desc;
        n++;
    }

    for (int i = 0; i < n; i++){
        System.out.println(itemArr[i] + " " + descArr[n] + "\n");
    }
    System.out.println("Total Records (n): " + n);

}

Or is there a better way to do this? I’ve read some post about Patterns and Regex, but not sure how to use that either.

Thank you!

  • 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-31T15:26:57+00:00Added an answer on May 31, 2026 at 3:26 pm

    There is no protection on n exceeding 200. If there are more that 200 iterations of the while loop then:

    itemArr[n] = item;
    

    will throw an ArrayIndexOutOfBoundsException.

    If the int at the beginning of each line is unique you could use a Map<Integer, String> to store the data. This will not place a limit of 200 on the number of items that can be read from the file and if you selected a TreeMap as the implementation it would sort them (you can either accept the natural ordering of Integer or define you own Comparator). As suggested by sethu you could use a BufferedReader to read the file.

    For example:

    BufferedReader br = new BufferedReader(new FileReader("inventory.txt"));
    Map<Integer, String> items = new TreeMap<Integer, String>();
    
    String line;
    while (null != (line = br.readLine()))
    {
        String[] line_parts = line.split(" ");
        if (line_parts.length > 1)
        {
            StringBuilder desc = new StringBuilder(line_parts[1]);
            for (int i = 2; i < line_parts.length; i++)
            {
                desc.append(line_parts[i]);
            }
            items.put(new Integer(line_parts[0]), desc.toString());
        }
    }
    
    for (Integer key: items.keySet())
    {
        System.out.println(key + " = " + items.get(key));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have about 200,000 text files that are placed in a bz2 file. The
I have a text file (bowtie alignment file) that looks like this: read_1 +
I have a text file with about 100,000 lines (5 MB), which is updated
I have a giant text file (about 1,5 gigabyte) with xml data in it.
I have about 600 text files. Each file contains 2 columns and is space
I have text file with some stuff that i would like to put into
I have text file with something like first line line nr 2 line three
I have text file, like FILED AS OF DATE: 20090209 DATE AS OF CHANGE:
I have text file with some text information and i need to split this
I have text file with entries like 123 112 3333 44 2 How to

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.