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 Answers2

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.

  • 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 . – Siegfraud245 Sep 22 '16 at 00:22
  • I have edited my answer to read the csv file. See the last section. – Arjun Doijode Sep 22 '16 at 00:51
  • also the format should match the one on my output above that is shown @ArjunDoijode – Siegfraud245 Sep 22 '16 at 00:54
  • 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: – Siegfraud245 Sep 22 '16 at 00:55
  • just a question, should i do the testing first with the .csv edit you did then the class above? – Siegfraud245 Sep 22 '16 at 01:06
  • using jgrasp to do my program – Siegfraud245 Sep 22 '16 at 01:18
  • @Siegfraud245: I am not here to do your homework. I had given sufficient information to solve your problem. You had to slightly modify my given code to match your output. Since I have spent so much time, I have again modified the code to exactly suit your output. Check the Edit section of my answer. – Arjun Doijode Sep 22 '16 at 01:28
  • You have to import the needed classes which I assumed you would do. Just add these line at the top of Testing class: import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; – Arjun Doijode Sep 22 '16 at 01:34
  • @Siegfraud245: should i do the testing first with the .csv edit you did then the class above? Yes you should do that way. – Arjun Doijode Sep 22 '16 at 01:38
  • all works except for 1 error: Testing.java:22: error: constructor HawaiiNativeForestBirds in class HawaiiNativeForestBirds cannot be applied to given types; HawaiiNativeForestBirds Hawaiinbird= new HawaiiNativeForestBirds(bird[0],bird[1],bird[2],Integer.valueOf(bird[3])); ^ required: no arguments found: String,String,String,Integer reason: actual and formal argument lists differ in length 1 error – Siegfraud245 Sep 22 '16 at 01:39
  • I have added the constructor into the HawaiiNativeForestBirds class.. Copy HawaiiNativeForestBirds class again – Arjun Doijode Sep 22 '16 at 01:45
  • Exception in thread "main" java.lang.NumberFormatException: For input string: "population" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) – Siegfraud245 Sep 22 '16 at 01:49
  • Can you share your csv content? – Arjun Doijode Sep 22 '16 at 01:52
  • Just remove the first line of your csv file. That would solve your problem.If this solves your problem just vote it as useful. So that others can use the solution – Arjun Doijode Sep 22 '16 at 02:02
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]);
}
}
  • It is a .csv file yes with 10 rows for each of the columns and yep 3 strings and 1 integer – Siegfraud245 Sep 22 '16 at 01:16
  • 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. – Siegfraud245 Sep 22 '16 at 01:20
  • 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? – Siegfraud245 Sep 22 '16 at 01:21
  • 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 ;_; – Siegfraud245 Sep 22 '16 at 01:27
  • 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 – Siegfraud245 Sep 22 '16 at 02:45
  • @Siegfraud245 check this out http://stackoverflow.com/questions/16027229/reading-from-a-text-file-and-storing-in-a-string – Zachary Cheshire Sep 22 '16 at 02:57
  • @Siegfraud245 When you find out how to grab each individual word from the txt file then save it to an array(Like the temp array I made) and execute the logic I posted. I don't know if this is the most efficient, but it will suffice. – Zachary Cheshire Sep 22 '16 at 03:00