Solved

Files

Posted on 2010-11-08
7
432 Views
Last Modified: 2012-05-10
I need to make a code that

Ask the user for the number of days to run the simulation. Make sure the user inputs at least a 1 for the number of days to run the simulation.  (If not, keep asking for the number, until they give a proper value.) Then, the program should read a text file for the name of the organism, the starting number of organisms, and the average daily population increase (as a percentage).  For each organism, the program should use a loop to display the size of the population each day.  The file may contain many organisms, with each organism on a separate line.  Continue to read the file and run the simulation for each organism in the file, until the end of file is reached.
Each line in the input file would have the format (separated by white space or tabs):
genus      species      startingNumber      averageDailyIncrease
Example  input file:
Fruit            fly            2      .50      
Bison            bison            1000      .1
Salt-creek       tiger-beetle      50      -.3

I have no idea how to begin
help please!!!!
0
Comment
Question by:liberty21
7 Comments
 

Author Comment

by:liberty21
ID: 34085579
this is what I have so far,


String genus,
            testToken;
            String species;
            int startingNumber;
            double averageDailyIncrease;


            FileReader freader = new FileReader("PopulationSimulator.txt");
            BufferedReader inputFile = new BufferedReader(freader);


            String days;
            days = JOptionPane.showInputDialog(null,"Please enter the number of days to run the simulation, or "
                        + "-1 if you are finished","English Word"
                        ,JOptionPane.QUESTION_MESSAGE);

            //read the first name from the file
            genus=inputFile.readLine();

            //if a name was read, display it and
            //read the remaining names.

            while (genus !=null)
            {
                  //display the last name read.
                  System.out.println(genus);

                  //read the next name.
                  genus=inputFile.readLine();
            }


            //close the file.
            inputFile.close();

      }


i have to use the stringtokenizer class
help please!!!!!!!
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34086131
You'd be better off using a Scanner - you'd get the correct data type for each field as well as making it a lot easier generally?
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 34088711
see the following for how to read the file
http://helpdesk.objects.com.au/java/using-scanner-to-read-words-from-text-file

or to read it line by line see here
http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line

or using a StringTokenizer

StringTokenizer st = new StringTokenizer(line, " ");
while (st.hasMoreTokens()) {
    String name = st.nextToken();
    String tag = st.nextToken();
    int count = Integer.parseInt(st.nextToken());
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:gordon_vt02
ID: 34093268
Since your spec says they can be separated by spaces or tabs, use the following constructor for StringTokenizer:

new StringTokenizer(line, " \t");
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34093298
StringTokenizer is a legacy class and unless there is a good reason for preferring it, Sun recommends that String.split be used instead. That's still an inferior approach to using a Scanner in this case though
0
 
LVL 27

Expert Comment

by:mrcoffee365
ID: 37439593
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
There are numerous questions about how to setup an IBM HTTP Server to be administered from WebSphere Application Server administrative console. I do hope this article will wrap things up and become a reference for this task. You need three things…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now