Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / May 2004

Tip: Looking for answers? Try searching our database.

Help w/ problem

Thread view: 
Shawn - 26 May 2004 21:12 GMT
Hi guys, my first post here so dont yell, heh.

I am trying to make a trivia program of sorts, but for this I need
help with my class that inputs data from files.  I need to set up two
String arrays to hold the answer and questions, which are read in
during the default constructor from two BufferedReader objects which
read in files. I want to try and set the size of the arrays according
to how many questions their are in the file, but yet at the same time
I need the arrays to be static so they can be accessed by the methods
that return the question and answers seperately to the main program. I
have tried pretty much everything I know and have pretty much
exhausted my brain... If you need me to post the class or more info,
just post what you need and I'll gladly post it here.
Roedy Green - 26 May 2004 21:19 GMT
>I am trying to make a trivia program of sorts, but for this I need
>help with my class that inputs data from files.  I need to set up two
[quoted text clipped - 7 lines]
>exhausted my brain... If you need me to post the class or more info,
>just post what you need and I'll gladly post it here.

You can use a BufferedReader than is invoked in static init code.

See http://mindprod.com/fileio.html for the readLine code
and http://mindprod.com/jgloss/initialisation for how initialisation
works.

Signature

Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Oscar kind - 26 May 2004 22:35 GMT
> You can use a BufferedReader than is invoked in static init code.

Instead of a static initializer, you can also the code below. It's not as
simple as a static initializer, but I find I don't have to mind the static
gotcha's either (no this, overriding, etc.).

public class YourClass implements Runnable
{
    private String[] commandLineArguments;

    public static void main(String[] args)
    {
        new YourClass(args).run();
    }

    Public YourClass(String[] args)
    {
        commandLineArguments = args;
    }

    public void run()
    {
        // Your code goes here.
    }
}

Of course, implementing Runnable is not necessary, but allows you to start
your program in a thread in the future (however unlikely that is). Also, I
find I rarely use command line arguments. In that case you can remove the
constructor and the field commandLineArguments.

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2

Shawn - 27 May 2004 03:39 GMT
I guess im not understanding what you guys are trying to say, I have
some coding skills but I don't understand a lot of it so far. I
decided to post what I have so far to see if you can better see my
problem. As it is, compiling it returns that I need to catch
FileNotFound exceptions on the qNumber method when I call it on num.
I know you can use some sort of try/catch code to fix this but so far
I havent gotten that to work.

public class FileIn
{
    private static String questionFile=null;
    private static String answerFile=null;
    private static int num=qNumber();
    private static int qnum=0;
    private static int anum=0;
    //String array variables to hold questions and answers
    static String[] questions=new String[num];
    static String[] answers=new String[num*5];
    public FileIn(String qF, String aF)throws FileNotFoundException,
IOException
    {
        questionFile=qF;
        answerFile=aF;
        //BufferedReader objects that read the question and answer files
        BufferedReader question= new BufferedReader(new
FileReader(questionFile));
        BufferedReader answer=new BufferedReader(new
FileReader(answerFile));
        //If statement to find the number of questions and deposit into an
int variable, num
        if(question!=null)
        {
            BufferedReader Q= new BufferedReader(new FileReader(questionFile));
            String numQ=null;
            numQ=Q.readLine();
            while(numQ!=null)
            {
            num++;
            numQ=Q.readLine();
            }
        }
        //Beginning to read in Questions and Answers
        int x=0, y=0;
        String Qu=null;
        String An=null;
        StringTokenizer Ans;
        Qu=question.readLine();
        An=question.readLine();
        while(Qu!=null)
        {
            questions[x]=Qu;
            x++;
        }
        while(An!=null)
        {
            answers[y]=An;
            y++;
        }
    }
    //Method to find the number of questions and deposit into an int
variable, num
    public static int qNumber()throws FileNotFoundException, IOException
    {
        int n=0;
        BufferedReader Q= new BufferedReader(new FileReader(questionFile));
        String numQ=null;
        numQ=Q.readLine();
        while(numQ!=null)
        {
        n++;
        numQ=Q.readLine();
        }
        return(n);
    }
    //Method to return the question numbered by the qnum variable
    public String getQuestion()
    {
        qnum++;
        return(questions[qnum-1]);
    }
}
Oscar kind - 27 May 2004 22:24 GMT
> I guess im not understanding what you guys are trying to say, I have
> some coding skills but I don't understand a lot of it so far. I
[quoted text clipped - 9 lines]
>        private static String answerFile=null;
>        private static int num=qNumber();

This won't work, because qNumber() is called when the class is loaded:
before the constructor is called that sets questionFile to another value
than null (hopefully).

>        private static int qnum=0;
>        private static int anum=0;
>        //String array variables to hold questions and answers
>        static String[] questions=new String[num];
>        static String[] answers=new String[num*5];

That's a lot of static fields, while you initialize them in your
constructor. In this case, don't make them static: when you create a
FileIn object, they are all initialized anyway.

Also, don't initialize "questions" and "answers" yet: you don't know the
number of questions and answers until you know the files (in the
constructor).

>        public FileIn(String qF, String aF)throws FileNotFoundException,
> IOException
[quoted text clipped - 12 lines]
>                        BufferedReader Q= new BufferedReader(new FileReader(questionFile));
>                        String numQ=null;

I know it's nitpicking, but don't call this variable "numQ"; the prefix
"num" suggests a number. My choice would be "string": a descriptive name
that tells me the variable doesn't contain anything important.

>                        numQ=Q.readLine();
>                        while(numQ!=null)
[quoted text clipped - 3 lines]
>                        }
>                }

Is "num" initialized to 0 before the while loop is reached? It will be
if you remove the assignment from the declaration, because 0 is the
starting value for a member variable of type "int".

>                //Beginning to read in Questions and Answers
>                int x=0, y=0;
>                String Qu=null;
>                String An=null;
>                StringTokenizer Ans;

Here is a good moment to initialize "questions" and "answers": you now
their length (assuming the number of answers equals the number of
questions; you don't check this).

                questions = new String[num];
                answers = new String[num];

>                Qu=question.readLine();
>                An=question.readLine();
[quoted text clipped - 9 lines]
>                }
>        }

>        //Method to find the number of questions and deposit into an int
> variable, num
[quoted text clipped - 11 lines]
>                return(n);
>        }

Remove this method after you've convinced yourself you don't use it.

>        //Method to return the question numbered by the qnum variable
>        public String getQuestion()
[quoted text clipped - 3 lines]
>        }
> }

Are you sure you want each question (and probably answer) to be given
only once? If so, you may want to name this method "nextQuestion".

Oscar

Signature

Oscar Kind                                    http://home.hccnet.nl/okind/
Software Developer                    for contact information, see website

PGP Key fingerprint:    91F3 6C72 F465 5E98 C246  61D9 2C32 8E24 097B B4E2



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.