Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Files

Posted on 2010-11-08
7
Medium Priority
?
445 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 2000 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

721 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