Solved

Files

Posted on 2010-11-08
7
441 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Manage your data center from practically anywhere

The KN8164V features HD resolution of 1920 x 1200, FIPS 140-2 with level 1 security standards and virtual media transmissions at twice the speed. Built for reliability, the KN series provides local console and remote over IP access, ensuring 24/7 availability to all servers.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet filter example 37 83
ejb entity bean example issue 2 25
batch vs regular insert in spring DAO 2 21
IBM TS2900 (3572) Tape Autoloader Java? 12 56
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

740 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