下午好,我还有另一个容易的人。我无法从try-catch循环中获取数组的内容并将其放在循环外部声明的数组中。我想这样做的原因是我可以从外部的main函数调用它。有什么指针吗?
看看我的代码:
我正在尝试获取Array votesPerPosition []的内容并将其放入ArrayVotesPosition [];
任何指针?谢谢!非常感谢您的帮助。
public class Vote{
int voter = 0;
Scanner sc;
BufferedReader br;
String line;
FileInputStream fis;
public int j = 0;
private int voterCount = 0;
public Arrays VotesPosition[];
public Vote() {
sc = new Scanner(System.in);
System.out.println("Enter file name > ");
String filename = sc.next();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
} catch (FileNotFoundException ex) {
//Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
do{
voter++;
try {
filename = br.readLine();
} catch (IOException ex) {
Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
String votesPerPosition[] = filename.split(",");
for(String vote: votesPerPosition){
Integer.parseInt(vote);
for (j = 0; j < votesPerPosition.length; j++);{
System.out.println("voter " + voter + "voted for candidate #" + vote);
voterCount++;
}
}
return String votesPerPosition; //<-------------- This is what I am trying//
}while(filename != null);
}
/**
* @return the voterCount
*/
public int getVoterCount() {
return voterCount;
}
/**
* @param voterCount the voterCount to set
*/
public void setVoterCount(int voterCount) {
this.voterCount = voterCount;
}
/**
* @return the VotesPosition
*/
public Arrays[] getVotesPosition() {
return VotesPosition;
}
/**
* @param VotesPosition the VotesPosition to set
*/
public void setVotesPosition(Arrays[] VotesPosition) {
this.VotesPosition = VotesPosition;
}
}
答案 0 :(得分:0)
将您的功能更改为public String[] Vote()
并使用return votesPerPosition;
返回数据。
(A String[]
是Java中引用计数Object
的类型,因此返回在函数中创建的实例是完全正常的。)
答案 1 :(得分:0)
试试:)
public class Vote{
int voter = 0;
Scanner sc;
BufferedReader br;
String line;
FileInputStream fis;
public int j = 0;
private int voterCount = 0;
public Arrays VotesPosition[];
public String[] Vote() {
sc = new Scanner(System.in);
System.out.println("Enter file name > ");
String filename = sc.next();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
} catch (FileNotFoundException ex) {
//Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
do{
voter++;
try {
filename = br.readLine();
} catch (IOException ex) {
Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
String votesPerPosition[] = filename.split(",");
for(String vote: votesPerPosition){
Integer.parseInt(vote);
for (j = 0; j < votesPerPosition.length; j++);{
System.out.println("voter " + voter + "voted for candidate #" + vote);
voterCount++;
}
}
return votesPerPosition; //<-------------- This is what I am trying//
}while(filename != null);
}
/**
* @return the voterCount
*/
public int getVoterCount() {
return voterCount;
}
/**
* @param voterCount the voterCount to set
*/
public void setVoterCount(int voterCount) {
this.voterCount = voterCount;
}
/**
* @return the VotesPosition
*/
public Arrays[] getVotesPosition() {
return VotesPosition;
}
/**
* @param VotesPosition the VotesPosition to set
*/
public void setVotesPosition(Arrays[] VotesPosition) {
this.VotesPosition = VotesPosition;
}
}
答案 2 :(得分:0)
如何更改您的实现如下
将此更改为Hashtable,
public Hashtable VotesPosition = new Hashtable();
,功能可以如下。
public void Vote() {
String filename = null, line = null;
sc = new Scanner(System.in);
System.out.println("Enter file name > ");
filename = sc.next();
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
} catch (FileNotFoundException ex) {
//Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
if (br != null) {
do {
voter++;
try {
line = br.readLine();
if (line != null && line.contains(",")) {
String votesPerPosition[] = line.split(",");
VotesPosition.put(voter, votesPerPosition.length);
} else if (line != null) {
VotesPosition.put(voter, 1);
}
} catch (IOException ex) {
line = null;
// Logger.getLogger(Vote.class.getName()).log(Level.SEVERE, null, ex);
}
} while (line != null);
}
}