The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Output some data from a txt file?

I have an assignment at school in which i have to make a program that reads a "Product number" and outputs the "Product Name". Those two pieces of info is located in a txt file, separated by a "," like this:

10002,iron
10003,gold
10003,cobber

Right now I'm able to read the txt file and store it in an array and output the entire array. This is my 2 classes:

Re: Output some data from a txt file?

what is a Map?
My specific question is: how should i do this? can i create a list of 2 arrays, one containing numbers, and the other containing names and then the the index of the product number (etc. [1]) is corresponding to the index of the product name?

Re: Output some data from a txt file?

Map is a java interface that has several classes that implement it. See the API doc for details.
Basically you store objects in a Map using a key and can retrieve them using that key. For example: If the number is the key, the name would be the value.
Using one of the Map classes will be easier than using two "parallel" arrays.

Re: Output some data from a txt file?

Okay, i've been trying some different things now and gone through a lot of different tutorials on this subject, and yes, mapping is the method i will use.

I've written a class that reads the txt file puts it into columns and then out prints the map. Then a user is able to input the product number, and the class should print out the product name, but it only prints out "null" - where did i go wrong?

Re: Output some data from a txt file?

With the above statement you are searching for a key namedprodNo which doesn't exist. You actually want to search for prodNo which is entered by the user (ie 10001, etc.). Can you see the difference between