Solved

Files

Posted on 2010-11-08
7
430 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 26

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
count11 challenge 6 47
mapBully challenge 6 89
base64 decode encode 12 97
thymeleaf natural templating vs JSP 2 27
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

760 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

20 Experts available now in Live!

Get 1:1 Help Now