Program:
- User enters a file name containing numbers (1 per line)
- File is read and data is stored in StringBuffer
- StringBuffer is converted to String type
- String is split() and stored in a String[]
- Double[] is created and converts/stores elements of the String[]
用户输入包含数字的文件名(每行1个)
读取文件并将数据存储在StringBuffer中
StringBuffer转换为String类型
字符串是split()并存储在String []中
创建Double []并转换/存储String []的元素
The above is what I am trying to achieve; however, the program is not working. *Look below for an example...
以上就是我想要实现的目标;但是,该程序无法正常工作。 *请看下面的例子......
class tester
{
public static int x = 0;
public static double[] dataArray = new double[x];//array storing elements
public static void main(String[] args) throws IOException
{
//userInput .txt
Scanner sc = new Scanner(System.in);
System.out.println("Enter filename:\n");
String name = sc.nextLine();
File file = new File(name);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String newString = stringBuffer.toString();
String [] stringArray = newString.split("\n");
double [] dataArray = new double[stringArray.length];
for(int i=0 ; i < stringArray.length ; i++)
{
dataArray[i] = Double.parseDouble(stringArray[i]);
}
I have a file named: Ben.txt that looks like this:
我有一个名为:Ben.txt的文件,如下所示:
1
2
3
4
5
6
7
8
9
10
When I run the program...
当我运行程序时......
Enter filename:
c:/Ben.txt
Exception in thread "main" java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)
at sun.misc.FloatingDecimal.parseDouble(Unknown Source)
at java.lang.Double.parseDouble(Unknown Source)
at tester.main(tester.java:27)
2 个解决方案
#1
The problem is you are not actually reading anything from the file. You do all the setup but you aren't reading.
问题是你实际上没有从文件中读取任何内容。你做了所有的设置,但你没有阅读。
So when you have:
所以当你有:
File file = new File(name);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String newString = stringBuffer.toString();
Here I don't think you actually need a StringBuffer, all you have to add/replace is this
在这里我不认为你真的需要一个StringBuffer,所有你必须添加/替换就是这个
String str;
str = bufferedReader.readLine();
If you want to read the whole file all you have to do is iterate over the entire file using readLine().
如果要读取整个文件,您只需使用readLine()遍历整个文件即可。
#2
As GiantTree said, you are not actually reading in the file at all
正如GiantTree所说,你实际上并没有阅读文件
Changing the definition of newString to the following will give you some progress.
将newString的定义更改为以下内容将为您提供一些进展。
String newString = bufferedReader.readLine();
Other things you should consider:
您应该考虑的其他事项:
- closing the resources you've opened
- exception handling
- following standard class naming conventions
- no need to initialise dataArray twice
关闭你打开的资源
遵循标准类命名约定
无需初始化dataArray两次
#1
The problem is you are not actually reading anything from the file. You do all the setup but you aren't reading.
问题是你实际上没有从文件中读取任何内容。你做了所有的设置,但你没有阅读。
So when you have:
所以当你有:
File file = new File(name);
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuffer stringBuffer = new StringBuffer();
String newString = stringBuffer.toString();
Here I don't think you actually need a StringBuffer, all you have to add/replace is this
在这里我不认为你真的需要一个StringBuffer,所有你必须添加/替换就是这个
String str;
str = bufferedReader.readLine();
If you want to read the whole file all you have to do is iterate over the entire file using readLine().
如果要读取整个文件,您只需使用readLine()遍历整个文件即可。
#2
As GiantTree said, you are not actually reading in the file at all
正如GiantTree所说,你实际上并没有阅读文件
Changing the definition of newString to the following will give you some progress.
将newString的定义更改为以下内容将为您提供一些进展。
String newString = bufferedReader.readLine();
Other things you should consider:
您应该考虑的其他事项:
- closing the resources you've opened
- exception handling
- following standard class naming conventions
- no need to initialise dataArray twice
关闭你打开的资源
遵循标准类命名约定
无需初始化dataArray两次