读取文件以将其数据存储在数组中时出错
Error Reading Files to Store their Data in an Array
我正在写的程序在Java。我试图让我的程序读取文件 "name.txt" 并将文本文件的值存储在数组中。
到目前为止,我正在使用一个将在我的主程序中读取的文本文件,一个名为 People.java 的服务 class,它将用作我的程序的模板,以及我的主程序称为 Names.java,它将读取文本文件并将其值存储到数组中。
name.txt:
John!Doe
Jane!Doe
Mike!Smith
John!Smith
George!Smith
People.java:
public class People
{
String firstname = " ";
String lastname = " ";
public People()
{
firstname = "First Name";
lastname = "Last Name";
}
public People(String firnam, String lasnam)
{
firstname = firnam;
lastname = lasnam;
}
public String toString()
{
String str = firstname+" "+lastname;
return str;
}
}
Names.java:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Names
{
public static void main(String[]args)
{
String a = " ";
String b = "empty";
String c = "empty";
int counter = 0;
People[]peoplearray=new People[5];
try
{
File names = new File("name.txt");
Scanner read = new Scanner(names);
while(read.hasNext())
{
a = read.next();
StringTokenizer token = new StringTokenizer("!", a);
while(token.hasMoreTokens())
{
b = token.nextToken();
c = token.nextToken();
People p = new People(b,c);
peoplearray[counter]=p;
++counter;
}
}
}
catch(IOException ioe1)
{
System.out.println("There was a problem reading the file.");
}
System.out.println(peoplearray[0]);
}
}
如我在我的程序中所示,我尝试打印 peoplearray[0] 的值,但是当我这样做时,我的输出显示为:"null."
如果程序运行正常,peoplearray[0] 的值应该是 "John Doe" 因为这些是 "names.txt"
中的适当值
peoplearray[0] 的值应该为空吗?
如果没有,我该怎么做才能解决这个问题?
谢谢!
你的参数顺序错误:
StringTokenizer token = new StringTokenizer("!", a);
public StringTokenizer(String str, String delim)
使用
StringTokenizer token = new StringTokenizer(a,"!");
我正在写的程序在Java。我试图让我的程序读取文件 "name.txt" 并将文本文件的值存储在数组中。
到目前为止,我正在使用一个将在我的主程序中读取的文本文件,一个名为 People.java 的服务 class,它将用作我的程序的模板,以及我的主程序称为 Names.java,它将读取文本文件并将其值存储到数组中。
name.txt:
John!Doe
Jane!Doe
Mike!Smith
John!Smith
George!Smith
People.java:
public class People
{
String firstname = " ";
String lastname = " ";
public People()
{
firstname = "First Name";
lastname = "Last Name";
}
public People(String firnam, String lasnam)
{
firstname = firnam;
lastname = lasnam;
}
public String toString()
{
String str = firstname+" "+lastname;
return str;
}
}
Names.java:
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Names
{
public static void main(String[]args)
{
String a = " ";
String b = "empty";
String c = "empty";
int counter = 0;
People[]peoplearray=new People[5];
try
{
File names = new File("name.txt");
Scanner read = new Scanner(names);
while(read.hasNext())
{
a = read.next();
StringTokenizer token = new StringTokenizer("!", a);
while(token.hasMoreTokens())
{
b = token.nextToken();
c = token.nextToken();
People p = new People(b,c);
peoplearray[counter]=p;
++counter;
}
}
}
catch(IOException ioe1)
{
System.out.println("There was a problem reading the file.");
}
System.out.println(peoplearray[0]);
}
}
如我在我的程序中所示,我尝试打印 peoplearray[0] 的值,但是当我这样做时,我的输出显示为:"null."
如果程序运行正常,peoplearray[0] 的值应该是 "John Doe" 因为这些是 "names.txt"
中的适当值peoplearray[0] 的值应该为空吗? 如果没有,我该怎么做才能解决这个问题? 谢谢!
你的参数顺序错误:
StringTokenizer token = new StringTokenizer("!", a);
public StringTokenizer(String str, String delim)
使用
StringTokenizer token = new StringTokenizer(a,"!");