Java:使用文件读取器在while循环中添加字符串数组
我一直在做一个小程序,需要阅读一个高尔夫球场的列表,这些球场可能会改变,需要随时打电话。代码如下:Java:使用文件读取器在while循环中添加字符串数组,java,arrays,loops,arraylist,while-loop,Java,Arrays,Loops,Arraylist,While Loop,我一直在做一个小程序,需要阅读一个高尔夫球场的列表,这些球场可能会改变,需要随时打电话。代码如下: public class Courses { public String[] courselist; public void loadCourses() throws IOException{ int count = 0; int counter = 0; File f = new File("src//courses//courses.txt"); Buf
public class Courses {
public String[] courselist;
public void loadCourses() throws IOException{
int count = 0;
int counter = 0;
File f = new File("src//courses//courses.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
while(count<1){
String s = reader.readLine();
if(s.equalsIgnoreCase("*stop*")){
reader.close();
count = 5;
}else{
courselist[counter] = s;
counter++;
}
s = "";
}
}
现在,当我开始运行程序时,因为我立即调用了该方法,它会给我一个throw exeption,这是因为数组。我需要保留和数组,因为我在JComboBox中使用它。如果您可以帮助或解决问题。很可能我只是做错了,我是个笨蛋。只要帮忙。提前谢谢。
我知道所有的文件读取器和东西都能正常工作,因为它正确地打印到系统中,我只需要帮助将其重复写入数组。在使用数组之前,您应该初始化数组
public static final MAX_SIZE = 100;
public String[] courselist = new String[MAX_SIZE];
您应该在使用数组之前初始化它
public static final MAX_SIZE = 100;
public String[] courselist = new String[MAX_SIZE];
您最好创建一个易于操作的列表,并在过程结束时将其转换为数组:
List<String> list = new ArrayList<String>();
String [] array = list.toArray(new String [] {});
List List=new ArrayList();
字符串[]数组=list.toArray(新字符串[]{});
以下是使用列表加载的可能实现:
public static String [] loadCourses() throws IOException {
File f = new File("src//courses.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> courses = new ArrayList<String>();
while (true){
String s = reader.readLine();
if (s == null || s.trim().equalsIgnoreCase("*stop*")){
break;
} else{
courses.add(s);
}
}
return courses.toArray(new String [] {});
}
公共静态字符串[]loadCourses()引发IOException{
文件f=新文件(“src//courses.txt”);
BufferedReader reader=新的BufferedReader(新文件读取器(f));
列出课程=新建ArrayList();
while(true){
字符串s=reader.readLine();
如果(s==null | | s.trim().equalsIgnoreCase(“*stop*”){
打破
}否则{
课程。添加(s);
}
}
return courses.toArray(新字符串[]{});
}
还有为什么要使用stop关键字?您只需在到达文件末尾时停止进程(s为空)。您最好创建一个易于操作的列表,并在进程末尾将其转换为数组:
List<String> list = new ArrayList<String>();
String [] array = list.toArray(new String [] {});
List List=new ArrayList();
字符串[]数组=list.toArray(新字符串[]{});
以下是使用列表加载的可能实现:
public static String [] loadCourses() throws IOException {
File f = new File("src//courses.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> courses = new ArrayList<String>();
while (true){
String s = reader.readLine();
if (s == null || s.trim().equalsIgnoreCase("*stop*")){
break;
} else{
courses.add(s);
}
}
return courses.toArray(new String [] {});
}
公共静态字符串[]loadCourses()引发IOException{
文件f=新文件(“src//courses.txt”);
BufferedReader reader=新的BufferedReader(新文件读取器(f));
列出课程=新建ArrayList();
while(true){
字符串s=reader.readLine();
如果(s==null | | s.trim().equalsIgnoreCase(“*stop*”){
打破
}否则{
课程。添加(s);
}
}
return courses.toArray(新字符串[]{});
}
还有为什么要使用stop关键字?您只需在到达文件结尾时(当s为null时)停止进程。在
loadCourses()
期间更改代码以分配新数组,并向构造函数添加对loadCourses()
的调用:
public class Courses {
public String[] courselist;
public Courses() throws IOException { // <-- Added constructor
loadCourses(); // <-- Added call to loadCourses
}
public void loadCourses() throws IOException {
int count = 0;
int counter = 0;
File f = new File("src//courses//courses.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> courses = new ArrayList<String>(); // <-- A List can grow
while(true){
String s = reader.readLine();
if (s.equalsIgnoreCase("*stop*")){
break;
}
courses.add(s);
}
courseList = courses.toArray(new String[0]); // <-- assign it here
}
}
公共课程{
公共字符串[]课程列表;
public Courses()抛出IOException{//在loadCourses()
期间更改代码以分配新数组,并向构造函数添加对loadCourses()
的调用:
public class Courses {
public String[] courselist;
public Courses() throws IOException { // <-- Added constructor
loadCourses(); // <-- Added call to loadCourses
}
public void loadCourses() throws IOException {
int count = 0;
int counter = 0;
File f = new File("src//courses//courses.txt");
BufferedReader reader = new BufferedReader(new FileReader(f));
List<String> courses = new ArrayList<String>(); // <-- A List can grow
while(true){
String s = reader.readLine();
if (s.equalsIgnoreCase("*stop*")){
break;
}
courses.add(s);
}
courseList = courses.toArray(new String[0]); // <-- assign it here
}
}
公共课程{
公共字符串[]课程列表;
public Courses()抛出IOException{/这里是一些示例,没有使用*stop*
:
public class ReadFile {
public static void main(String[] args) {
BufferedReader reader = null;
List<String> coursesList = new ArrayList<>();
String[] courses;
try {
File file = new File("courses.txt");
reader = new BufferedReader(new FileReader(file));
String readLine;
do {
readLine = reader.readLine();
if(readLine == null)
break;
coursesList.add(readLine);
} while (true);
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
courses = coursesList.toArray(new String[coursesList.size()]);
for(int i = 0; i < courses.length; i++) {
System.out.println(courses[i]);
}
}
}
公共类读取文件{
公共静态void main(字符串[]args){
BufferedReader reader=null;
List coursesList=新建ArrayList();
串[]个课程;
试一试{
File File=新文件(“courses.txt”);
reader=newbufferedreader(newfilereader(file));
字符串读取线;
做{
readLine=reader.readLine();
if(readLine==null)
打破
coursesList.add(readLine);
}虽然(正确);
}捕获(IOEX异常){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}最后{
试一试{
reader.close();
}捕获(IOEX异常){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}
}
courses=coursesList.toArray(新字符串[coursesList.size()]);
for(int i=0;i
这里是一些示例,不使用*停止*
:
public class ReadFile {
public static void main(String[] args) {
BufferedReader reader = null;
List<String> coursesList = new ArrayList<>();
String[] courses;
try {
File file = new File("courses.txt");
reader = new BufferedReader(new FileReader(file));
String readLine;
do {
readLine = reader.readLine();
if(readLine == null)
break;
coursesList.add(readLine);
} while (true);
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
reader.close();
} catch (IOException ex) {
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex);
}
}
courses = coursesList.toArray(new String[coursesList.size()]);
for(int i = 0; i < courses.length; i++) {
System.out.println(courses[i]);
}
}
}
公共类读取文件{
公共静态void main(字符串[]args){
BufferedReader reader=null;
List coursesList=新建ArrayList();
串[]个课程;
试一试{
File File=新文件(“courses.txt”);
reader=newbufferedreader(newfilereader(file));
字符串读取线;
做{
readLine=reader.readLine();
if(readLine==null)
打破
coursesList.add(readLine);
}虽然(正确);
}捕获(IOEX异常){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}最后{
试一试{
reader.close();
}捕获(IOEX异常){
Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE,null,ex);
}
}
courses=coursesList.toArray(新字符串[coursesList.size()]);
for(int i=0;i
您的基本问题是Java中的数组不是动态大小的。在下面的问题中有很多关于这方面的好信息:您必须检查readLine()的结果如果为null,则是EOF:关闭文件。基本问题是Java中的数组不是动态大小的。下面的问题中有很多关于这方面的好信息:您必须检查readLine()的结果如果为空,则为EOF:关闭该文件。谢谢您的帮助,但现在我想不出如何将其应用于JComboBox。请在创建组合框的位置重新调用此代码。示例:String[]values=loadCourses();J