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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:56:00+00:00 2026-06-03T19:56:00+00:00

I’m working with java Scanner trying to extract product information from a text file

  • 0

I’m working with java Scanner trying to extract product information from a text file called Inventory.txt.

This file contains data on products in this format:

“Danelectro|Bass|D56BASS-AQUA|336177|395.00Orange|Amplifier|BT1000-H|319578|899.00Planet Waves|Superpicks|1ORD2-5|301075|4.50Korg|X50 Music Synthesizer|X50|241473|735.00Alpine|Alto Sax|AAS143|198490|795.00”

I am trying to parse the strings and add them into an arraylist such that each element in the arraylist would look something like this:

"Danelectro|Bass|D56BASS-AQUA|336177|395.00"
"Orange|Amplifier|BT1000-H|319578|899.00"    
"KorPlanet Waves|Superpicks|1ORD2-5|301075|4.50"
"g|X50 Music Synthesizer|X50|241473|735.00"
"Alpine|Alto Sax|AAS143|198490|555.00”

Following is my code:

public class ItemDao {        
    public ItemDao() {
        scanFile();
    }

    public void scanFile() {
        Scanner scanner; 
        ArrayList <String> content = new ArrayList <String>();
        try {
            Pattern p1 = Pattern.compile("\\.[0-9]{2}$");
            scanner = new Scanner(new File("Inventory.txt"));

            while (scanner.hasNext(p1)) {
                content.add(scanner.next(p1));
            }

            for (String item : content) {
                System.out.println("Items:" + item);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

When I tested this code I found that the arraylist is empty. Any help would be much appreciated.

java -jar A00123456Lab5.jar

Create an ItemDAO class in a dao package
This class will contain an static inner class which implements Comparator
(DAO = Data Access Object)

  • 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-03T19:56:02+00:00Added an answer on June 3, 2026 at 7:56 pm

    I see you’re using a pattern, those can come in handy–but I’d just take each line and substring it.

    while(scanner.hasNextLine()){
        String temp = scanner.nextLine();
        while(temp.indexOf("|") != -1){
            content.add(temp.substring(temp.indexOf("|"));
            temp.substring(temp.indexOf("|")+1);
        }    
    }
    

    Just a thought–might be easier to debug with this way.

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

Sidebar

Related Questions

When working with Java from Scala, we have to account for null. HttpServletRequest getters
I'm working with java, trying to use the AudioFormat Class. I have the following
I am working on Java. I am parsing an xml file, I am getting
I'm trying out several exercises from a Java programming book. I have the code
I am using java.util.Scanner for things such as nextInt(), and all was working fine
Having problems compiling this class. I'm working through a book on java and this
I'm trying to write a scanner for my C/C++/C#/Java/D-like programming language that I'm designing
I am working in Java, inserting data from HashMaps of certain types into a
I've just started working with Java, and I've been trying to get the console
I'm trying to pull out sub-string from a string using java.Util.Scan The sub-string is

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.