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

  • Home
  • SEARCH
  • 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 8741327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:17:35+00:00 2026-06-13T11:17:35+00:00

I am trying to read a file of multiple data types into an ArrayList

  • 0

I am trying to read a file of multiple data types into an ArrayList object using Scanner with delimiter “\s\s”, however it doesn’t seem to work as intended. I am using printf to view if data would be stored correctly, data which I will use for calculations later on. The data seem to display correctly however I am still getting an “Incorrect File Format” exception. Also there seems to be a problem with loop. I always get stuck when using ArrayList of objects.

Sample text file:

item  item descr  100  1.50
item2  item descr  250  2.50
item2  item descr  250  3.50

Code:

import java.io.*;
import java.util.*;

public class ReadItems
{

    private Scanner input;
    ArrayList<Item> item = new ArrayList<Item>();

    //open text file
    public void openFile()
    {
        try
        {
            FileReader in = new FileReader("Items.txt");
            input = new Scanner(in).useDelimiter("\\s\\s");
        }
        catch( FileNotFoundException fileNotFound)
        {
            System.err.println( "Error opening file.");
            System.exit(1);
        }
    }

    //read file
    public void readFile()
    {
        try
        {
            while ( input.hasNextLine())
            {
                item.add( new Item(input.next(), input.next(), input.nextInt(), input.nextFloat() ));                                       
                for (Item list : item) 
                {
                    System.out.printf("%-10s%-48s$%5.2f\n", list.getCode(), (list.getDecription()+ ", "+ list.getWeight()+ "g"), + list.getPrice());
                    //System.out.println(item);
                }

            }

        }
        catch ( NoSuchElementException elementEx)
        {
            System.err.println( "Incorrect file format.");
            System.exit(1);
        }
        catch ( IllegalStateException stateEx )
        {
            System.err.println( "Error reading from file.");
            System.exit(1);
        }

    }

    public void closeFile()
    {
        if (input != null)
            input.close();      
    }

}

Output:

item      item descr, 100g                                $ 1.50
item      item descr, 100g                                $ 1.50
item2     item descr, 250g                                $ 2.50
item      item descr, 100g                                $ 1.50
item2     item descr, 250g                                $ 2.50
item2     item descr, 250g                                $ 3.50

Incorrect file format.

Sorry it seems i was doing a dumb thing. I wasn’t running the program through my test class where main is.

Test Class:

public class TestReadItems
{

public static void main(String[] args) 
{
ReadItems application = new ReadItems();
application.openFile();
application.readFile();
application.closeFile();
}
}

The program runs without errors however i can’t seem to get the while loop to work properly. Output is tripled.

  • 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-13T11:17:36+00:00Added an answer on June 13, 2026 at 11:17 am

    This is because your for loop to print the output is inside the while loop. So, it reads each line of the file and returns the output. So, to correct, replace the output for loop from within the while statement and write it instead after the while loop completes.

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

Sidebar

Related Questions

I am trying to read file headers using java, I want to get file
I'm trying to read a file in my maven project at /src/main/resources/file.txt. I'm using
I am trying to read 2D Data matrix barcode using zxing library(GenericMultipleBarcodeReader). I have
I'm learning R and am simply trying to read in a stata data file
Need to load data from a single file with a 100,000+ records into multiple
Using Python (3.1 or 2.6), I'm trying to read data from binary data files
I'm trying to read a text file with multiple lines and then display it
I'm trying to read data from a.csv file to ouput it on a webpage
I am trying to read file and split its line to get some context(Computer
I'm trying to read a file line by line starting from a specific line

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.