我正在尝试创建向屏幕显示2个输出的程序。
第一个为null,第二个为null,显示存储在对象数组中的输入文件中的值。
到目前为止,这是我的代码:
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
&#13;
现在唯一缺少的是从文件中读取并通过初始化数组来存储对象数组的方法。
我仍然不擅长将这两者结合起来,正如您从代码中看到的那样,我还没有这种方法,也不知道格式如何组合两者。
编辑2:我终于修复了我的输出。我初衷了什么,现在如何从文件读取并存储到数组? ; _; 输出:
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
&#13;
我想要的只是显示两个输出,但我必须读取并存储第二个输出的对象数组
1.在没有初始化元素的情况下显示HawaiiNativeForestBirds数组array []:
2.再次显示HawaiiNativeForestBirds数组[],但在初始化元素后显示文件中的数组值:
编辑:
我的CSV内容:
答案 0 :(得分:0)
我会通过以下方式解决这个问题。
创建一个HawaiiNativeForestBirds java类
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;
}
}
编辑:如果你想读一个csv文件,你可以解决它,如下所示:
假设csv文件包含以下格式的数据
puaiohi,myadestes palmeri,brown,125
puaiohi,magumma parva,yellow,2000
我修改了测试类来读取csv文件
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());
}
}
}
注意:HawaiiNativeForestBirds类将保持不变。
答案 1 :(得分:0)
尝试读取每个String并将其存储在Strings数组中的自己的索引中。然后查看此算法,看看它是否有效。我相信这不是处理这种情况的最佳方式,但我在课堂上并认为我会给它一个直观的镜头。我将从构造函数获取4个参数的基础开始,并且还假设txt文件中的参数按照它们将如何放入构造函数的顺序排列。您还必须将第4个参数转换为int,以便它与构造函数的预期参数类型匹配。
//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]);
}
}