• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

loops

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hyi i am currently working on a project at the moment its a little program that reads in a file and is then stored as as array but the loop will only read 7 items of the array how can i extended so it reads more ps the stuff on the file is details of endangered species i,e there name, count, etc so if you could please have a look at the code at make some modification i would be very greatfull

package myprojects.testarray;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
class Testarray {

public static void main(String args[]) {
// A simple example of how to read in animal descriptions and plonk
// them in an array.

// Remember that count should really be an integer and a bit of conversion will have
// to occur.


// Also note that this example reads in 7 animals, but your version should detect
// the end of the file (i.e. use a different sort of loop).

String name[ ] = new String[1000];
String count[] = new String[1000];
String species[] = new String[1000];
String country[] = new String[1000];
try {
File inFile = new File("F:\\animals.dat");
// Create a file stream, wrapping the file
FileReader fileReader = new FileReader(inFile);
// Create a second wrapper,
BufferedReader bufReader = new BufferedReader(fileReader);
// Read in the first line...
String s;
// Read in 7 animals (how can this be improved?)
for (int i=0; i<7; i++) {
name[i] = bufReader.readLine();
count[i] = bufReader.readLine();
species[i] = bufReader.readLine();
country[i] = bufReader.readLine();
System.out.println("Name "+i+" is "+name[i]);
System.out.println("Count "+i+" is "+count[i]);
System.out.println("species "+i+" is "+species[i]);
System.out.println("Found in "+i+" is "+country[i]);


}

System.out.println("Starting Testarray...");
Testarray mainFrame = new Testarray();

}
catch (IOException e) {
System.out.println("IOException "+e);
}

}
}
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This sounds like homework To read all the entries in the file, read until bufReader.readLine() returns null.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anish,
You must know Robert, he is also posting a question about converting C to Java and reading Endangered Species from a file. What a coincidence! Must be a homework assignment ...
You can just check to make sure all values are not null . Since the readLine routine will return null if it has reached the end of line. Since you require all four items that should be a good check. You might also want to use temporary variables and only place them into the object variables once you know you have all 4.

Regards,
Manfred.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
I would use this:

Regards, Dirk
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dirk,
A good rule of thumb in developing code:

Never assume anything!


No matter what you assume, it will not happen that way in all cases...
Regards,
Manfred.
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic