为什么此 PrintWriter 不填充此文件?
Why won't this PrintWriter populate this file?
我有一个程序,它从一个文本文件中获取 Giraffe
s,从中创建一个 ArrayList
和 LinkedList
(冗余,我知道... LinkedList
是作业第二部分的要求,我喜欢我的 ArrayList
),找出他们的 parents 是谁,然后应该将 VBA 宏打印到文件中.
文件 (D:\Java\Macro.txt
) 已创建,但未填充任何内容。 为什么没有任何内容打印到文件中? 我认为创建 return 数组到主函数时存在问题。
我怀疑问题出在 Herd
Class 中的 GetYearGroup
方法中,以及它与位于第 51 行的 PrintWriter
接口的方式-83 的 Main
class.
代码如下:
HW4_Name.java(主要)
package giraffe;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class HW4_Name {
public static void main(String[] args) throws IOException {
ArrayList<Giraffe> giraffes = new ArrayList<>();
String temp[] = new String[13];
String header = "";
String fileLocation = "theHerd.txt";
File textFile = new File(fileLocation);
Scanner in = new Scanner(textFile);
if (textFile.canRead()) {
header = in.nextLine();
while (in.hasNextLine()) {
temp = in.nextLine().split("\t", 13);
giraffes.add(new Giraffe(temp));
}
}
Herd herd = new Herd();
for(int i = 0; i < giraffes.size(); i++){
Giraffe g = giraffes.get(i);
herd.Add(g);
}
int nGiraffes = herd.Size();
for(int i = 0; i < nGiraffes; i++){
Giraffe g = herd.GetAt(i);
int nSire = g.getSireId();
if (nSire != -1){
g.setSire(herd.Find(nSire));
}
int nDam = g.getDamId();
if (nDam != -1){
g.setDam(herd.Find(nDam));
}
}
in.close();
PrintWriter pw = new PrintWriter("C:\Java\Macro.txt");
int nHeight = 500;
int nWidth = 900;
int nYearHeight = nHeight / 13;
int nRectangle = 0;
for(int i = 0; i < 13; i++){
int nLowYear = 50 + 5 * i;
Giraffe[] ThisGroup = herd.GetYearGroup(nLowYear, nLowYear + 5);
int nThisGroup = ThisGroup.length;
int nXSpacing = nWidth / (nThisGroup + 1);
int nYPos = 10 + nYearHeight * i;
for(int j = 0; j < nThisGroup; j++){
nRectangle++;
int nXPos = 10 + nXSpacing/2 + nXSpacing * j;
Giraffe g = ThisGroup[j];
g.setRectangle(nRectangle);
String strName = g.getName();
pw.println("Call Box(" + nXPos + ", " + nYPos + ", \"" +strName + "\")");
}
}
for(int i = 0; i < nGiraffes; i++){
Giraffe g = herd.GetAt(i);
Giraffe gSire = g.getSire();
if (gSire != null){
int nParentRectangle = gSire.getRectangle();
nRectangle = g.getRectangle();
if (nParentRectangle > 0 && nRectangle > 0){
pw.println("Call DadLine(" + nParentRectangle + ", " + nRectangle + ")");
}
}
Giraffe gDam = g.getDam();
if(gDam != null){
int nParentRectangle = gDam.getRectangle();
if (nParentRectangle > 0 && nRectangle > 0){
pw.println("Call MomLine(" + nParentRectangle + ", " + nRectangle + ")");
}
}
}
pw.close();
}
}
Giraffe.java
package giraffe;
import java.util.ArrayList;
public class Giraffe extends Object {
private String birthLocation, subSpecies, zoo, city, state, event, name,
localId, sex, eachGiraffe;
private int gId, birthYear, sireId, damId, gRectangle;
private Giraffe gSire, gDam;
public Giraffe(String array[]){
this.sex = String.format("%-1s",array[1]);
this.birthLocation = String.format("%-12s",array[5]);
this.localId = String.format("%-7s",array[6]);
this.name = String.format("%-20s",array[7]);
this.subSpecies = String.format("%-14s",array[8]);
this.zoo = String.format("%-35s",array[9]);
this.city = String.format("%-17s",array[10]);
this.state = String.format("%-13s",array[11]);
this.event = String.format("%-7s",array[12]);
this.gId = Integer.parseInt(array[0]);
this.birthYear = Integer.parseInt(array[2].substring(0,4));
if(array[3].equals("WILD") || array[3].equals("UNK")){
this.sireId = -1;
}
else{
this.sireId = Integer.parseInt(array[3]);
}
if(array[4].equals("WILD") || array[4].equals("UNK")){
this.damId = -1;
}
else{
this.damId = Integer.parseInt(array[4]);
}
}
public String getName(){
return this.name;
}
public int getId(){
return gId;
}
public int getBirthYear(){
return birthYear;
}
public int getSireId(){
return sireId;
}
public int getDamId(){
return damId;
}
public Giraffe getSire(){
return gSire;
}
public int getRectangle(){
return gRectangle;
}
public Giraffe getDam(){
return gDam;
}
public void setSire(Giraffe nSire){
this.gSire = nSire;
}
public void setDam(Giraffe nDam){
this.gDam = nDam;
}
public void setRectangle(int nRectangle){
this.gRectangle = nRectangle;
}
public String toString(){
eachGiraffe = ("" + this.gId);
return eachGiraffe;
}
}
Herd.java
package giraffe;
public class Herd extends LinkedList{
public Herd(){
}
public void Add(Giraffe g){
InsertRight(g);
}
public Giraffe Find(int idNumber){
Giraffe g = null;
Node currNode = start;
while(currNode != null && currNode.o.getId() != idNumber){
currNode = currNode.next;
}
if(currNode.o.getId() == idNumber){
g = currNode.o;
}
return g;
}
public Giraffe GetAt(int nIndex){
Giraffe g = null;
Node currNode = start;
for(int i = 0; i < nIndex; i++){
if(currNode != null){
currNode = currNode.next;
}
}
g = currNode.o;
return g;
}
public Giraffe[] GetYearGroup(int nBegin, int nEnd){
int nGiraffes = Size();
int nInGroup = 0;
for(int i = 0; i < nGiraffes; i++){
Giraffe g = GetAt(i);
int nBirthYear = g.getBirthYear();
if (nBegin <= nBirthYear && nBirthYear < nEnd){
nInGroup++;
}
}
Giraffe[] gary = new Giraffe[nInGroup];
nInGroup = 0;
for(int i = 0; i < nGiraffes; i ++){
Giraffe g = GetAt(i);
int nBirthYear = g.getBirthYear();
if(nBegin <= nBirthYear && nBirthYear < nEnd){
gary[nInGroup] = g;
nInGroup++;
}
}
return gary;
}
}
LinkedList.java
package giraffe;
public class LinkedList {
protected Node start;
private int errorReturn;
public LinkedList(){
start = null;
}
public LinkedList(int errorValue){
start = null;
errorReturn = errorValue;
System.out.println(errorReturn);
}
public boolean isEmpty(){
return (start == null);
}
public void InsertRight(Giraffe data){
Node currNode;
if (start == null){
start = new Node(data,start);
}
else{
currNode = start;
while (currNode.next != null){
currNode = currNode.next;
}
currNode.next = new Node (data, null);
}
}
public int Size(){
int length = 0;
Node currNode = start;
while(currNode != null){
length++;
currNode = currNode.next;
}
return length;
}
public void Display(){
Node currNode = start;
System.out.println("List contents: ");
while (currNode != null){
currNode = currNode.next;
}
System.out.println("--------------------------");
}
}
Node.java
package giraffe;
public class Node {
Giraffe o;
Node next;
public Node(Giraffe giraffe, Node nextNode){
o = giraffe;
next = nextNode;
}
}
解决方案
感谢 durron597 指出 birthYear 函数与 GetYearGroup 函数不可比。
正在替换:
for(int i = 0; i < 13; i++){
int nLowYear = 50 + 5 * i;
与:
for(int i = 0; i < 13; i++){
int nLowYear = 1950 + 5 * i;
问题已解决。
我看到两个问题,这两个问题都可能导致您所看到的情况:
- 您正在输出到
C:\Java\Macro.txt
目录,但您使用的是相对路径来查找您的 theHerd.txt
。尝试将您的 theHerd.txt
文件更改为绝对路径,或者如果该文件位于您的 class 路径中,则使用 Class.getResourceAsStream()
。
- 使用调试器查看是否完全创建了
Giraffe
个对象
你的出生年份检查看起来很奇怪:
for(int i = 0; i < 13; i++){
int nLowYear = 50 + 5 * i;
Giraffe[] ThisGroup = herd.GetYearGroup(nLowYear, nLowYear + 5);
这只会检查 50、55 到 115 范围内的年份。但是,在您的 Giraffe
class 中,您可以这样做:
this.birthYear = Integer.parseInt(array[2].substring(0,4));
这仅适用于 4 位数的日期年份(即不抛出 ArrayIndexOutOfBounds
异常),其中 none 在 50-115 范围内。但是,如果您将年份设置为类似 0073
的值(您必须放置前导零),那么您的程序将打印类似 Call Box(235, 162, "GiraffeName ")
的结果。 您可能想将 50
更改为 1950
。
您的程序有其他代码味道;我建议阅读以下内容(按时间投入的顺序):
- Java Encapsulation Concept not clear
- Java naming conventions
- Single Responsibility Principle
- Clean Code, by Robert Martin
但是这两段代码应该可以帮助您入门。
我有一个程序,它从一个文本文件中获取 Giraffe
s,从中创建一个 ArrayList
和 LinkedList
(冗余,我知道... LinkedList
是作业第二部分的要求,我喜欢我的 ArrayList
),找出他们的 parents 是谁,然后应该将 VBA 宏打印到文件中.
文件 (D:\Java\Macro.txt
) 已创建,但未填充任何内容。 为什么没有任何内容打印到文件中? 我认为创建 return 数组到主函数时存在问题。
我怀疑问题出在 Herd
Class 中的 GetYearGroup
方法中,以及它与位于第 51 行的 PrintWriter
接口的方式-83 的 Main
class.
代码如下:
HW4_Name.java(主要)
package giraffe;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class HW4_Name {
public static void main(String[] args) throws IOException {
ArrayList<Giraffe> giraffes = new ArrayList<>();
String temp[] = new String[13];
String header = "";
String fileLocation = "theHerd.txt";
File textFile = new File(fileLocation);
Scanner in = new Scanner(textFile);
if (textFile.canRead()) {
header = in.nextLine();
while (in.hasNextLine()) {
temp = in.nextLine().split("\t", 13);
giraffes.add(new Giraffe(temp));
}
}
Herd herd = new Herd();
for(int i = 0; i < giraffes.size(); i++){
Giraffe g = giraffes.get(i);
herd.Add(g);
}
int nGiraffes = herd.Size();
for(int i = 0; i < nGiraffes; i++){
Giraffe g = herd.GetAt(i);
int nSire = g.getSireId();
if (nSire != -1){
g.setSire(herd.Find(nSire));
}
int nDam = g.getDamId();
if (nDam != -1){
g.setDam(herd.Find(nDam));
}
}
in.close();
PrintWriter pw = new PrintWriter("C:\Java\Macro.txt");
int nHeight = 500;
int nWidth = 900;
int nYearHeight = nHeight / 13;
int nRectangle = 0;
for(int i = 0; i < 13; i++){
int nLowYear = 50 + 5 * i;
Giraffe[] ThisGroup = herd.GetYearGroup(nLowYear, nLowYear + 5);
int nThisGroup = ThisGroup.length;
int nXSpacing = nWidth / (nThisGroup + 1);
int nYPos = 10 + nYearHeight * i;
for(int j = 0; j < nThisGroup; j++){
nRectangle++;
int nXPos = 10 + nXSpacing/2 + nXSpacing * j;
Giraffe g = ThisGroup[j];
g.setRectangle(nRectangle);
String strName = g.getName();
pw.println("Call Box(" + nXPos + ", " + nYPos + ", \"" +strName + "\")");
}
}
for(int i = 0; i < nGiraffes; i++){
Giraffe g = herd.GetAt(i);
Giraffe gSire = g.getSire();
if (gSire != null){
int nParentRectangle = gSire.getRectangle();
nRectangle = g.getRectangle();
if (nParentRectangle > 0 && nRectangle > 0){
pw.println("Call DadLine(" + nParentRectangle + ", " + nRectangle + ")");
}
}
Giraffe gDam = g.getDam();
if(gDam != null){
int nParentRectangle = gDam.getRectangle();
if (nParentRectangle > 0 && nRectangle > 0){
pw.println("Call MomLine(" + nParentRectangle + ", " + nRectangle + ")");
}
}
}
pw.close();
}
}
Giraffe.java
package giraffe;
import java.util.ArrayList;
public class Giraffe extends Object {
private String birthLocation, subSpecies, zoo, city, state, event, name,
localId, sex, eachGiraffe;
private int gId, birthYear, sireId, damId, gRectangle;
private Giraffe gSire, gDam;
public Giraffe(String array[]){
this.sex = String.format("%-1s",array[1]);
this.birthLocation = String.format("%-12s",array[5]);
this.localId = String.format("%-7s",array[6]);
this.name = String.format("%-20s",array[7]);
this.subSpecies = String.format("%-14s",array[8]);
this.zoo = String.format("%-35s",array[9]);
this.city = String.format("%-17s",array[10]);
this.state = String.format("%-13s",array[11]);
this.event = String.format("%-7s",array[12]);
this.gId = Integer.parseInt(array[0]);
this.birthYear = Integer.parseInt(array[2].substring(0,4));
if(array[3].equals("WILD") || array[3].equals("UNK")){
this.sireId = -1;
}
else{
this.sireId = Integer.parseInt(array[3]);
}
if(array[4].equals("WILD") || array[4].equals("UNK")){
this.damId = -1;
}
else{
this.damId = Integer.parseInt(array[4]);
}
}
public String getName(){
return this.name;
}
public int getId(){
return gId;
}
public int getBirthYear(){
return birthYear;
}
public int getSireId(){
return sireId;
}
public int getDamId(){
return damId;
}
public Giraffe getSire(){
return gSire;
}
public int getRectangle(){
return gRectangle;
}
public Giraffe getDam(){
return gDam;
}
public void setSire(Giraffe nSire){
this.gSire = nSire;
}
public void setDam(Giraffe nDam){
this.gDam = nDam;
}
public void setRectangle(int nRectangle){
this.gRectangle = nRectangle;
}
public String toString(){
eachGiraffe = ("" + this.gId);
return eachGiraffe;
}
}
Herd.java
package giraffe;
public class Herd extends LinkedList{
public Herd(){
}
public void Add(Giraffe g){
InsertRight(g);
}
public Giraffe Find(int idNumber){
Giraffe g = null;
Node currNode = start;
while(currNode != null && currNode.o.getId() != idNumber){
currNode = currNode.next;
}
if(currNode.o.getId() == idNumber){
g = currNode.o;
}
return g;
}
public Giraffe GetAt(int nIndex){
Giraffe g = null;
Node currNode = start;
for(int i = 0; i < nIndex; i++){
if(currNode != null){
currNode = currNode.next;
}
}
g = currNode.o;
return g;
}
public Giraffe[] GetYearGroup(int nBegin, int nEnd){
int nGiraffes = Size();
int nInGroup = 0;
for(int i = 0; i < nGiraffes; i++){
Giraffe g = GetAt(i);
int nBirthYear = g.getBirthYear();
if (nBegin <= nBirthYear && nBirthYear < nEnd){
nInGroup++;
}
}
Giraffe[] gary = new Giraffe[nInGroup];
nInGroup = 0;
for(int i = 0; i < nGiraffes; i ++){
Giraffe g = GetAt(i);
int nBirthYear = g.getBirthYear();
if(nBegin <= nBirthYear && nBirthYear < nEnd){
gary[nInGroup] = g;
nInGroup++;
}
}
return gary;
}
}
LinkedList.java
package giraffe;
public class LinkedList {
protected Node start;
private int errorReturn;
public LinkedList(){
start = null;
}
public LinkedList(int errorValue){
start = null;
errorReturn = errorValue;
System.out.println(errorReturn);
}
public boolean isEmpty(){
return (start == null);
}
public void InsertRight(Giraffe data){
Node currNode;
if (start == null){
start = new Node(data,start);
}
else{
currNode = start;
while (currNode.next != null){
currNode = currNode.next;
}
currNode.next = new Node (data, null);
}
}
public int Size(){
int length = 0;
Node currNode = start;
while(currNode != null){
length++;
currNode = currNode.next;
}
return length;
}
public void Display(){
Node currNode = start;
System.out.println("List contents: ");
while (currNode != null){
currNode = currNode.next;
}
System.out.println("--------------------------");
}
}
Node.java
package giraffe;
public class Node {
Giraffe o;
Node next;
public Node(Giraffe giraffe, Node nextNode){
o = giraffe;
next = nextNode;
}
}
解决方案
感谢 durron597 指出 birthYear 函数与 GetYearGroup 函数不可比。
正在替换:
for(int i = 0; i < 13; i++){
int nLowYear = 50 + 5 * i;
与:
for(int i = 0; i < 13; i++){
int nLowYear = 1950 + 5 * i;
问题已解决。
我看到两个问题,这两个问题都可能导致您所看到的情况:
- 您正在输出到
C:\Java\Macro.txt
目录,但您使用的是相对路径来查找您的theHerd.txt
。尝试将您的theHerd.txt
文件更改为绝对路径,或者如果该文件位于您的 class 路径中,则使用Class.getResourceAsStream()
。- 使用调试器查看是否完全创建了
Giraffe
个对象
- 使用调试器查看是否完全创建了
你的出生年份检查看起来很奇怪:
for(int i = 0; i < 13; i++){ int nLowYear = 50 + 5 * i; Giraffe[] ThisGroup = herd.GetYearGroup(nLowYear, nLowYear + 5);
这只会检查 50、55 到 115 范围内的年份。但是,在您的
Giraffe
class 中,您可以这样做:this.birthYear = Integer.parseInt(array[2].substring(0,4));
这仅适用于 4 位数的日期年份(即不抛出
ArrayIndexOutOfBounds
异常),其中 none 在 50-115 范围内。但是,如果您将年份设置为类似0073
的值(您必须放置前导零),那么您的程序将打印类似Call Box(235, 162, "GiraffeName ")
的结果。 您可能想将50
更改为1950
。
您的程序有其他代码味道;我建议阅读以下内容(按时间投入的顺序):
- Java Encapsulation Concept not clear
- Java naming conventions
- Single Responsibility Principle
- Clean Code, by Robert Martin
但是这两段代码应该可以帮助您入门。