Solved

Files

Posted on 2010-11-08
7
443 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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 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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

626 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