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.

The challenge:
I must be able to take these text files and create a collection of these items. The problems I am dealing with
are that I must be able to deal with the columns from separate txt files being in different order. For example:
file 1: Name, Cost, Location, Expense, Amount. File 2: Cost, Expense, Name, Location, Amount
The only thing that is guaranteed is that the Name and Location will not be right next to each other

The second problem is that I cannot use anything built in Java Lists or Collections libraries. This means no
ArrayLists, HashMaps, TreeMaps, etc.

Re: sorting input from text files

We are specifically told NOT to use any of the collection libraries, so an array is out of the question. Yes, there are specific datatypes. the name column will ONLY hold strings, the cost ONLY doubles etc.

After the file is read, it must be stored in memory. I will be reading multiple files and compiling them into a single thing (not sure what that will be yet since I cannot use any array or map).

All i want is a vague point in the right direction:
ie: check out a certain method or simplified algorithm.

Right now I am attempting to use something like this:

Scanner in = new Scanner(new FileReader(filename));
in.nextLine(); <--gets the column headers out of the way
while (in.hasNext()) {
if (!in.hasNextInt() && !in.hasNextDouble()){ <---checks to see if it is the Name(type String) or location(type char)
if (in.next().length > 1) { <----the name strings are always more than i character
Item.addName(in.next();
}
}

this probably has some errors in it, and I don't want you to point them out. But If you could say if I am headed the right directions, or should explore using a different method that would be great.

Re: sorting input from text files

I will be using this program to read several text files. All these should be read in and stored in memory. When storing all this in local memory I need to be updating similar items from separate txt files. ie. if i have an item named car in one file, and car in another file, i need to update say, the quantity from 1 to 2. Then when all the files are read I need to be able to write out a file similar to the txt files that were read in.

Re: sorting input from text files

Originally Posted by MikeTySUN

I will be using this program to read several text files. All these should be read in and stored in memory. When storing all this in local memory I need to be updating similar items from separate txt files. ie. if i have an item named car in one file, and car in another file, i need to update say, the quantity from 1 to 2. Then when all the files are read I need to be able to write out a file similar to the txt files that were read in.

If you are not allowed to use the Java Collection Framework - to which Arrays do not belong, it makes it hard. You might want to talk to your instructor about it. I would also recommend reading some tutorials on text manipulation. Try reading this: Java Platform SE 7

Re: sorting input from text files

Originally Posted by MikeTySUN

Thanks for the help so far. I just had a quick question about scanner. Will a scanner read a single character from a txt file as a String or a char?

There are things like the StringTokenizer class which will help you tokenize strings into seperate units but it is advisable to use the String's split method to do the same. You might want to check that out and perhaps you will be better off!

Re: sorting input from text files

Ok, another question with scanner though. Does in.next() regardless of how it is used always return the token and move to the next token?
ie: if (in.next().equalsIgnoreCase("example"));
Object.setMethod(in.next());

The reason is what if I am saying
if (thing > in.nextInt()) {
x = in.nextInt() * 5;
}

would the scanner move to the next token in the comparison and then return a separate value in the body?

Re: sorting input from text files

Does in.next() regardless of how it is used always return the token and move to the next token?

Write a small test program for testing and see what it does.
One easy way to test without having to use the console is to use a String in the constructor. For example:
Scanner scnr = new Scanner("this\nis some input \non three lines.\n");

In your code sample, each call to nextInt() would get the next token as it is executed. A problem could be if you do not clear the newline character from the buffer by calling the nextLine() method