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 20 of 20

Thread: code set up question

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default code set up question

    making a portable text based trivia game, whats the best way for the question and answer system set up. TXT files, DB., ECT. also the PC that will run this will not have access to any network connection (theatrically) and can be stored in side of the .jar file. any good source you know that will be good to look at please post them and your opinions also.

    newbitmapimagejna.jpg
    http://img821.imageshack.us/img821/9...apimagejna.png
    Last edited by op117; September 8th, 2012 at 05:41 PM. Reason: english fix / GUI pic added


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

    Default Re: code set up question

    A text file would be the simplest and could easily be included in a jar file.
    If you don't understand my answer, don't ignore it, ask a question.

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

    op117 (September 9th, 2012)

  4. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    whats the best set up for the text files 5 of them like this? answers1.txt, answers2.txt, answers3.txt, answers4.txt, questions.txt, or run it all in the same file?

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

    Default Re: code set up question

    What's the easiest to program and the easiest to make changes to? I'd say put it all in one file.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: code set up question

    Norm has a great idea and with good reasoning.
    I come from the other side of the fence on this however.
    I would suggest taking the object oriented approach, and maintain a separate file for each question.
    Within that file I would probably place the question itself on the first line,
    the correct answer on the second line,
    a series of seemingly correct answers on the next several lines,
    a delimiter on the next line,
    and a series of comical, obviously wrong answers on the next few lines.

    Then I would program it to select questions from the possible (available) files, be sure to include the correct answer, and on occasion a comical answer. (I assume there will be some choices anyway, not much said about what your game is)

  7. The Following User Says Thank You to jps For This Useful Post:

    op117 (September 9th, 2012)

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    Could having separate files for each question be a maintenance problem?
    How do you add more questions? How does the program find all the files it needs to read?
    It can be done, but its more complicated than having it all in one file. Perhaps an index file that lists all the question files? Again a maintenance problem.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: code set up question

    Quote Originally Posted by Norm View Post
    Could having separate files for each question be a maintenance problem?
    Not that I can forsee given the OP's OP. No need to modify during runtime, just read. No troubles there. Any time they are modified, the new jar will be distributed with the mods, so far so good.


    Quote Originally Posted by Norm View Post
    How do you add more questions?
    Drop a new txt file in the questions folder.


    Quote Originally Posted by Norm View Post
    How does the program find all the files it needs to read?
    Read the file names in the questions folder.


    Quote Originally Posted by Norm View Post
    It can be done, but its more complicated than having it all in one file.
    I agree your idea is good, just different than my approach.


    Quote Originally Posted by Norm View Post
    Perhaps an index file that lists all the question files? Again a maintenance problem.
    I do not see this extra file as a necessary step, and therefore not a maintenance problem.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    Read the file names in the questions folder.
    Is that a bit harder in a jar file? No File class to do the work.

    Separate files can be done, just a lot more work.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: code set up question

    Quote Originally Posted by Norm View Post
    Is that a bit harder in a jar file?
    I will admit it is different, but please explain how it is harder. What is it I do not see? Reading the file names do you mean?


    Quote Originally Posted by Norm View Post
    No File class to do the work.
    How is reading an all in one file allowed to use a class you can not use by multiple files? Or have I misunderstood the statement?


    Quote Originally Posted by Norm View Post
    Separate files can be done, just a lot more work.
    I can agree it is 'some' more work. But I can not go with a lot more. Plus I say the flexibility and ease of maintenance stands tall enough to outweigh more code to get the file names vs less code filtering the all in one file to be read. But truthfully in the end that would depend on the specifics. Maybe each question only needs one answer, in which case I would go with the all in one file. But the impression I get from the OP is that there are multiple answers for the questions. In which case I disagree with the all in one.

    Flexibility/Maintenance:
    Adding questions/answers is as easy as adding a file to the folder.
    Removing questions/answers is as easy as removing a file from the folder.
    Modifying questions/answers is as easy as replacing a file in a folder.
    Each question can have a different pool of wrong answers with a different number of answers in it's pool.

    I agree it can be done either way, I just do not see the advantage to an all in one file besides the extra work to read file names associated with the multiple file approach.

  12. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    I've forgotten the coding used to read the contents of a folder in a jar file. For a disk folder, the File class's list() method does it.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #11
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    Quote Originally Posted by jps View Post
    Norm has a great idea and with good reasoning.
    I come from the other side of the fence on this however.
    I would suggest taking the object oriented approach, and maintain a separate file for each question.
    Within that file I would probably place the question itself on the first line,
    the correct answer on the second line,
    a series of seemingly correct answers on the next several lines,
    a delimiter on the next line,
    and a series of comical, obviously wrong answers on the next few lines.

    Then I would program it to select questions from the possible (available) files, be sure to include the correct answer, and on occasion a comical answer. (I assume there will be some choices anyway, not much said about what your game is)
    Your approche sounds nice, also I'm making this for a compintition, and will latter realse this to teachers for their class room reviews as free were. How wood a jTable to create and manage all the files / edit the files work?

    New Bitmap Image (2).jpg

    Also this may make a sticky Becous it can benefit outher programers also.
    Last edited by op117; September 10th, 2012 at 05:32 PM. Reason: English also double posted by axcedent due to phone, on the rode right now

  14. #12
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation Re: code set up question

    Quote Originally Posted by Norm View Post
    I've forgotten the coding used to read the contents of a folder in a jar file. For a disk folder, the File class's list() method does it.

    here is a explanation i found for this.
    java - How do I access the content of folders inside a jar file? - Stack Overflow

    import java.util.*;
    import java.net.*;
    import java.util.jar.*;
    class I18n {
        public static void main( String ... args ) {
            getLocaleListFromJar();
        }
        private static List<Locale> getLocaleListFromJar() {
            List<Locale> locales = new ArrayList<Locale>();
            try {
                URL packageUrl = I18n.class.getProtectionDomain().getCodeSource().getLocation();
                JarInputStream jar = new JarInputStream(packageUrl.openStream());
                while (true) {
                    JarEntry entry = jar.getNextJarEntry();
                    if (entry == null) {
                        System.out.println( "entry was null ");
                        break;
                    }
                    String name = entry.getName();
                    System.out.println( "found : " +name );
                    /*if (resourceBundlePattern.matcher(name).matches()) {
                        addLocaleFromResourceBundle(name, locales);
                    }*/
               }
            } catch (Exception e) {
                System.err.println(e);
                return null;
                //return getLocaleListFromFile(); // File based implementation in case resources are not in jar
            }
            return locales;
        }
    }

    that is the example code off link can i get some what of a explanation of whats happening?


    here is another example witch youes different code.

    http://stackoverflow.com/questions/3...rrent-jar-file
    Last edited by op117; September 10th, 2012 at 05:25 PM.

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    My point was that reading a single file from a jar file would be easier than reading the contents of a folder in a jar file. As jps says, the other design would be more OOP. Which way is better for a student is up to the student.
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    if you could youse a jTable wood it be a possible solution? How wood that work?

  17. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    A JTable for what? I was talking about where to store the questions and answers so they would be easily read and updated.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #16
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    jTable for the questions / answers can that store it efficiently? also can it be called from another part of the application, was doing some research and found little a bout jTables.

  19. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    JTables are used when the program executes. Not for storing data between executions.
    If you don't understand my answer, don't ignore it, ask a question.

  20. #18
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    can a jTable manage like text files, creation, deletion, moding of the files? and reference the variables, or have it saved in all one file so the jTable can lode up with the content of the file in to the jTable and thin dump it all in to that file for the rest of the program to youes it?

    Extract File data into JTable


    what if you have the jTable save by 5 text files each file by column and thin each line a cell, like excel.

    New Bitmap Image (2).jpg
    Last edited by op117; September 10th, 2012 at 07:04 PM.

  21. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: code set up question

    JTables are not connected to files. You would have to write the code to connect a JTable to a file.
    If you don't understand my answer, don't ignore it, ask a question.

  22. #20
    Junior Member
    Join Date
    Aug 2012
    Posts
    26
    My Mood
    Nerdy
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: code set up question

    i will search the bowels of the internet and will post links latter on of what i find search time < 2 weeks probably of google pages ect...

Similar Threads

  1. Replies: 4
    Last Post: October 24th, 2011, 08:09 AM
  2. [SOLVED] Question about inline code segment
    By brighamandrew in forum Java Theory & Questions
    Replies: 2
    Last Post: September 1st, 2011, 04:40 AM
  3. This is my code and i would like to ask a question..
    By savvas in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 24th, 2011, 01:01 PM
  4. Newbie Question on a Code - I don't know if this is Java
    By fresca in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2011, 08:39 PM
  5. Swin/Awt Code Question
    By Pulse_Irl in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 30th, 2010, 11:26 AM