Welcome to the Java Programming Forums


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.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: sorting input from text files

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default sorting input from text files

    Here is what I am dealing with:
    Name Cost Location Expense Amount <------- at the top of each text file are these column headers
    String double char double int <------ inside these columns are these data types each row is an "item"
    String double char double int
    String double char double int
    String double char double int


    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.

    thanks for any help


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sorting input from text files

    Are you expected to use arrays to hold the data?
    Is there a datatype associated with each column? eg Name is always String and Cost is always double.

    What is to be done with the data after it is read from the file?


    How much help does your instructor expect/allow you to get from a forum? Are you supposed to design this yourself or are you allowed to get a design from someone else?
    Last edited by Norm; January 17th, 2012 at 07:55 PM.

  3. The Following User Says Thank You to Norm For This Useful Post:

    MikeTySUN (January 19th, 2012)

  4. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sorting input from text files

    an array is out of the question
    That will make it tougher. An array is not part of the collections package. Its a basic programming construct.

    What is to be done with the data after it is read from the file?

  6. The Following User Says Thank You to Norm For This Useful Post:

    MikeTySUN (January 19th, 2012)

  7. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  8. #6
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: sorting input from text files

    Quote Originally Posted by MikeTySUN View Post
    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

  9. The Following User Says Thank You to elisha.java For This Useful Post:

    MikeTySUN (January 19th, 2012)

  10. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: sorting input from text files

    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?

  11. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: sorting input from text files

    I don't think the Scanner class will read a single char. Read the API doc to be sure.
    The next method will read the next token. tokens are separated by delimiters

  12. The Following User Says Thank You to Norm For This Useful Post:

    MikeTySUN (January 19th, 2012)

  13. #9
    Member
    Join Date
    Dec 2011
    Location
    United States
    Posts
    94
    My Mood
    Amused
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default Re: sorting input from text files

    Quote Originally Posted by MikeTySUN View Post
    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!

  14. The Following User Says Thank You to elisha.java For This Useful Post:

    MikeTySUN (January 19th, 2012)

  15. #10
    Junior Member
    Join Date
    Jan 2012
    Posts
    5
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?

  16. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default 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

Similar Threads

  1. Your input with written code (Sorting and searching within array)
    By GalBenH in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 11th, 2012, 09:19 AM
  2. Need help to parse text files.
    By Num1701 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: November 18th, 2011, 02:11 PM
  3. Convert Text files to XML
    By HelloAll in forum Java Theory & Questions
    Replies: 1
    Last Post: February 2nd, 2011, 05:01 AM
  4. [SOLVED] Selection Sorting a text array
    By ComputerSaysNo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 30th, 2011, 06:56 PM
  5. Automate files input
    By JMaste in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 3rd, 2010, 08:46 AM