0

I am trying to create program that shows 2 outputs to the screen.

The first one being null and the 2nd one, shows values from the input file that was stored in an array of objects.

Here is my code so far:

javax.swing.JOptionPane;
import java.util.Scanner;
import java.io.File;



public class Testing {
   public static void main(String[] args) {
    //error checking for commandline input
      if(args.length != 1){
         System.out.println("Please enter at least one input file into the argument.");
         //terminates the program if more than 1 is entered
         System.exit(1);
      }
      //make an array of bird objects
      final Integer SIZE = 9;
      HawaiiNativeForestBirds array[] = new HawaiiNativeForestBirds[SIZE];
      //output array of Nature objects to the screen (should be "null" for all elements)
      System.out.println("Hawai'i Native Forest Birds ");
      System.out.println("index   element ");
      for (int i = 0; i < SIZE; i++) {
        
         System.out.println("  " + i + "       " + array[i] );
         
         System.out.println();
         
         //read from file and store data from file in your Nature array of objects
         //by initializing each array element using the constructor
         
         File file = new File(args[0]);
         Scanner inputFromFile = null;
         
       
         array[0] = new HawaiiNativeForestBirds("'akiapola'au"," hemignathus munroi"," yellow", 800);
         array[1] = new HawaiiNativeForestBirds("akepa"," loxxops coccineus"," red", 9301);
         array[2] = new HawaiiNativeForestBirds("hawai'i creeper"," oreomystis mana"," yellow green", 2501);
         array[3] = new HawaiiNativeForestBirds("i'iwi"," vestiara conccinea"," red green", 2501);
         array[4] = new HawaiiNativeForestBirds("apapane"," himatione sanguinea"," white red", 5001);
         array[5] = new HawaiiNativeForestBirds("hawai'ian amakihi"," hemignathus virens"," yellow brown", 3001);
         array[6] = new HawaiiNativeForestBirds("hawaii'an hawk"," buteo  solitarius"," white gray", 1100);
         array[7] = new HawaiiNativeForestBirds("puaiohi"," myadestes palmeri"," brown", 125);
         array[8] = new HawaiiNativeForestBirds("anianiau"," magumma parva"," light yellow", 2000);
      
                    
         //use toString() to display the array again with data from input file
         System.out.println("index        name       Scientific Name       Color          Population");
         for(int x=0;x<SIZE;x++){
            System.out.println("  " + i + "     " + array[i]);
         }
      
             
      }//end of main() method
   }// end of class LastnameFirstname08
   

   


      /**
   * Class HawaiianTheme stores and displays the data for each HawaiianTheme object
   * 
   * 
   */
class HawaiiNativeForestBirds {
      // data fields that store each object's data
   private String name;
   private String scientificname;
   private String color;
   private Integer population;
       //constructor - used to initialize the three data fields
      /**
    * Stores the name,scientific name, color and population of the Hawaiian Birds
    *  This is a Constructor, which is used to Create EAch Object & Initialize DAta Fields.
    * 
    * @param 
    * @param 
    * @param 
    * @param    
    */
   public HawaiiNativeForestBirds(String birdName, String scientificName,String birdColor, Integer birdPopulation) {
      name = birdName;
      scientificname = scientificName;
      color = birdColor;
      population = birdPopulation; 
   }//end of constructor
      //toString() method - returns a String with the 4 data fields
   public String toString() {
      String output = name +"     "+ scientificname + "     "+ color +"     "+ population;
      return output;
   }//end of toString()      
}//end of class HawaiianTheme

The only thing missing now is the method that reads from file and stores the array of objects by initializing the arrays.

I'm still no good at combining both of these and as you can see from the code, I don't have the method yet nor I know how the format would look like to combined both.

edit 2: I finally fixed my output . I initiliazed stuff, now how to read from file and store to the array? ;_; Output:

Hawai'i Native Forest Birds 
index   element 
  0       null
  1       null
  2       null
  3       null
  4       null
  5       null
  6       null
  7       null
  8       null
  9       null

index        name       Scientific Name       Color          Population
  0     'akiapola'au      hemignathus munroi      yellow     800
  1     akepa      loxxops coccineus      red     9301
  2     hawai'i creeper      oreomystis mana      yellow green     2501
  3     i'iwi      vestiara conccinea      red green     2501
  4     apapane      himatione sanguinea      white red     5001
  5     hawai'ian amakihi      hemignathus virens      yellow brown     3001
  6     oma'o      myadester obscurus      gray     17001
  7     hawaii'an hawk      buteo  solitarius      white gray     1100
  8     puaiohi      myadestes palmeri      brown     125
  9     anianiau      magumma parva      light yellow     2000

All I want is to show two outputs but I gotta read and store the array of objects for the 2nd output

1.Display HawaiiNativeForestBirds array array[] without initializing elements:

2.Display HawaiiNativeForestBirds array[] again but shows the array values from the file after initializing elements:

edit:

My CSV Content:

birds.csv

2
  • 1
    Add the java tag to your question, else it's unlikely anyone will answer it (most people filter questions based on tags and neither array nor object are usefull filter tags as they exist in well, most languages) Commented Sep 21, 2016 at 23:40
  • Thanks ! @RonanThibaudau Commented Sep 21, 2016 at 23:43

2 Answers 2

0

Try reading in each String and storing it into its own index in a array of Strings. Then check out this algorithm and see if it works. I am sure this is not the best way to handle the situation, but I am in class and thought I would give it a intuitive shot. I am going off of the base that your constructor takes 4 parameters, and also assuming that the parameters in the txt file are in order of how they would be put into the constructor. You will also have to cast the 4th parameter into a int so it matches the expected argument type for your constructor.

//Create String array and use for loop to fill temp array with words from txt file
temp[i] = scan.next()
for(int i = 0; i < temp.length; i++) {
if (i != 0) {
i = i + 3;
HawaiiNativeForestBirds[i - (3*i/4)] = new HawaiiNativeForestBirds(temp[(i-4)+4], temp[(i-4)+5], temp[(i-4)+6], temp[(i-4)+7)];

}
else {
HawaiiNativeForestBirds[i] = new HawaiiNativeForestBirds(temp[i],temp[i+1], temp[i+2], temp[i+3]);
}
}
Sign up to request clarification or add additional context in comments.

7 Comments

It is a .csv file yes with 10 rows for each of the columns and yep 3 strings and 1 integer
as you can see above with my output, that is how it is supposed to look like. Only thing missing in my code now is reads from file and stores the array of objects by initializing the arrays.
pretty sure i already initialized them and the only missing part is reading and storing it or am i already storing it by initializing each array of element using the constructor?
so "Try reading in each String and storing it into its own index in a array of Strings" is this a method class then? how would I go about doing that ? sorry if i had to ask ;_;
Arjun already gave a slightly modified one but what if I wanted to use the code I have? I already initiliazed my constructors i think only thing missing now is reading and storing it @ZacharyCheshire
|
0

I would solve this problem in following way.

Create a HawaiiNativeForestBirds java class

class HawaiiNativeForestBirds {
private String name;
private String scientificname;
private String color;
private Integer population;
public HawaiiNativeForestBirds(){

  }
  public HawaiiNativeForestBirds(String name, String scientificname,
        String color, Integer population) {
    super();
    this.name = name;
    this.scientificname = scientificname;
    this.color = color;
    this.population = population;
  }  


  public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getScientificname() {
    return scientificname;
}
public void setScientificname(String scientificname) {
    this.scientificname = scientificname;
}
public String getColor() {
    return color;
}
public void setColor(String color) {
    this.color = color;
}
public Integer getPopulation() {
    return population;
}
public void setPopulation(Integer population) {
    this.population = population;
}

  public String toString() {
  String output = name +"     "+ scientificname + "     "+ color +"     "+        population;
  return output;
}      
}

Edit: If you want read a csv file then you cans solve it as below:

Assuming csv file contains data in following format

puaiohi,myadestes palmeri,brown,125
puaiohi,magumma parva,yellow,2000

I have modified Testing class to read the csv file

 public class Testing {

 public static void main(String[] args) {

    String csvFile = "birds.csv";
    String line = "";
    String cvsSplitBy = ",";

    List<HawaiiNativeForestBirds>  listofBirds = new ArrayList<HawaiiNativeForestBirds>();
    try (BufferedReader br = new BufferedReader(new FileReader(csvFile))) {

        while ((line = br.readLine()) != null) {

            // use comma as separator
            String[] bird = line.split(cvsSplitBy);
            HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3]));
            listofBirds.add(Hawaiinbird);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
// First display null values
 HawaiiNativeForestBirds[]  hbirds=new        HawaiiNativeForestBirds[listofBirds.size()];
    System.out.println("index   " + "element   ");  
    int i=0;
    for (HawaiiNativeForestBirds hbird:hbirds){
        i++;
        System.out.println(i+"   "+hbird);
        }
    // Now display actual values
    hbirds= listofBirds.toArray(new HawaiiNativeForestBirds[listofBirds.size()]);

    System.out.println("index   " + "name   "+ "Scientific Name    "+ "Color   " + "Population   ");        
    i=0;
    for (HawaiiNativeForestBirds hbird:hbirds){
        i++;
        System.out.println(i+"   "+hbird.toString());
        }
 }
 }

Note: HawaiiNativeForestBirds class would remain as it is.

14 Comments

i actually have birds.csv file which is my input file that contains 10 rows of bird data and 4 columns containing the birdname,birdscientificname,color and population .
I have edited my answer to read the csv file. See the last section.
also the format should match the one on my output above that is shown @ArjunDoijode
I am able to show what I need to do now but the issue is how do I Display HawaiiNativeForestBirds array[] again but shows the array values from the file after initializing elements:
just a question, should i do the testing first with the .csv edit you did then the class above?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.