为什么我的代码总是出现 "java.io.FileNotFoundException" 错误?
Why do I keep getting a "java.io.FileNotFoundException" error on my code?
我花了几个小时试图修复此代码,并在该站点上访问了无数问题。
对于代码转储,我深表歉意,但我不确定代码出了什么问题。
错误是基于文件的读取,但为了安全起见,我包含了所有代码。
package ch7;
import java.util.Scanner;
import java.io.*;
public class DistributionChart
{
public static void main (String[] args) throws IOException
{
int size = 10;
int[] ranges = new int[size]; // each entry represents a range of values
getData(ranges); //pass the entire array into the method
displayChart(ranges);
getDataFromFile(ranges);
displayChartToFile(ranges);
System.out.println("\nSee you later!!");
} //end of main
public static void getData(int[] someArray)
{
Scanner scan = new Scanner (System.in);
System.out.println ("Enter a series of numbers between 1 and 100. Separate each number with a space.");
System.out.println ("Signal the end by entering a number outside " +
"of that range and then press enter.");
System.out.print("Go: ");
//reads an arbitrary number of integers that are in the range 1 to 100 inclusive
//for each integer read in, determine which range it is in and increment the corresponding element in the array
//your code goes here
int values = 0;
values = scan.nextInt();
while(values > 0 && values < 101)
{
if(values >= 1 && values <= 10)
{
someArray[0]++;
}
else if (values >= 11 && values <= 20)
{
someArray[1] ++;
}
else if (values >= 21 && values <= 30)
{
someArray[2]++;
}
else if(values >= 31 && values <= 40)
{
someArray[3]++;
}
else if(values >= 41 && values <= 50)
{
someArray[4]++;
}
else if(values >= 51 && values <= 60)
{
someArray[5]++;
}
else if(values >= 61 && values <= 70)
{
someArray[6]++;
}
else if(values >= 71 && values <= 80)
{
someArray[7]++;
}
else if(values >= 81 && values <= 90)
{
someArray[8]++;
}
else if(values >= 91 && values <= 100)
{
someArray[9]++;
}
values = scan.nextInt();
}
scan.close();
}//end of getData
public static void displayChart(int[] someArray)
{
System.out.println("Distribution chart by Royce T.");
System.out.println("================================");
//Print histogram.
for(int i = 0; i < someArray.length; i++)
{
System.out.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");
for(int y = 0; y < someArray[i]; y++)
{
System.out.print("*");
}
System.out.println();
}
} //end of displayChart
public static void getDataFromFile(int[] someArray) throws IOException
{
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
int values = 0;
while(inFile.hasNext())
{
values = inFile.nextInt();
while(values > 0 && values < 101)
{
if(values >= 1 && values <= 10)
{
someArray[0]++;
}
else if (values >= 11 && values <= 20)
{
someArray[1] ++;
}
else if (values >= 21 && values <= 30)
{
someArray[2]++;
}
else if(values >= 31 && values <= 40)
{
someArray[3]++;
}
else if(values >= 41 && values <= 50)
{
someArray[4]++;
}
else if(values >= 51 && values <= 60)
{
someArray[5]++;
}
else if(values >= 61 && values <= 70)
{
someArray[6]++;
}
else if(values >= 71 && values <= 80)
{
someArray[7]++;
}
else if(values >= 81 && values <= 90)
{
someArray[8]++;
}
else if(values >= 91 && values <= 100)
{
someArray[9]++;
}
values = inFile.nextInt();
}
inFile.close();
}
}
public static void displayChartToFile(int[] someArray) throws IOException
{
getDataFromFile(someArray);
PrintWriter outFile = new PrintWriter( "C:/Users/RTmag/workspaceRATI//CSC110/outputFile.txt");
//Print chart title with your name
outFile.println("Distribution chart by Royce T.");
outFile.println("================================");
//Print histogram.
for(int i = 0; i < someArray.length; i++)
{
outFile.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");
for(int x = 0; x <someArray[i]; x++)
{
outFile.print("*");
}
outFile.println();
}
outFile.close();
}
}
// end of DistributionChart tester class
我正在尝试让 displayChartToFile 方法写入位于 CSC110 文件夹中名为 outputFile.txt 的文件。此文件夹还包含我的 bin 和 src 文件夹。
每当我 运行 我的代码时,前两种方法在我输入后正确显示。
错误输出示例:
Enter a series of numbers between 1 and 100. Separate each number with a space.
Signal the end by entering a number outside of that range and then press enter.
Go: 55 66 555
Distribution chart by Royce T.
================================
1-10 |
11-20 |
21-30 |
31-40 |
41-50 |
51-60 |*
61-70 |*
71-80 |
81-90 |
91-100 |
Exception in thread "main" java.io.FileNotFoundException: C:\Users\RTmag\workspaceRATI\CSC110\inputFile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at ch7.DistributionChart.getDataFromFile(DistributionChart.java:143)
at ch7.DistributionChart.main(DistributionChart.java:28)
第 28 行是:
getDataFromFile(ranges);
第 143 行是:
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
inputFile.txt里面的文字是
75 49 62 35 18 97 62 54 83 61 44 29 98 75 14
outputFile.txt 为空。
非常感谢任何帮助。谢谢!
如果我没有您可能需要帮助我的所有信息,请告诉我!
编辑:
我修复了文件位置,但现在它返回了一组新的错误。
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ch7.DistributionChart.getDataFromFile(DistributionChart.java:207)
at ch7.DistributionChart.main(DistributionChart.java:30)
更改此行
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
要么
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI/CSC110/inputFile.txt"));
或
Scanner inFile = new Scanner(new FileReader("C:\Users\RTmag\workspaceRATI\CSC110\inputFile.txt"));
对我来说,这看起来不对...
int values = 0;
while (inFile.hasNext()) {
values = inFile.nextInt();
while (values > 0 && values < 101) {
//...
values = inFile.nextInt();
}
}
- 你检查一下文件是否有更多的行,好的
- 你得到下一个
int
值,很好
- 当值在
1..100
之间时循环,好的,但是如果到达输入的末尾会发生什么?
- 你看下一个
int
……啊,不太清楚。
根据您输入的 75 49 62 35 18 97 62 54 83 61 44 29 98 75 14
,您可以简单地执行类似...
int values = 0;
while (inFile.hasNext()) {
values = inFile.nextInt();
//...
}
但是如果输入文件可能有多行,你可以做...
int values = 0;
while (inFile.hasNext()) {
String nextLine = inFile.nextLine();
Scanner line = new Scanner(nextLine);
while (line.hasNextInt()) {
values = line.nextInt();
//...
}
}
(即使只有一行也可以做到)
基本问题是,您正在读到文件末尾,Scanner
运行 没有数据要读取
我花了几个小时试图修复此代码,并在该站点上访问了无数问题。
对于代码转储,我深表歉意,但我不确定代码出了什么问题。
错误是基于文件的读取,但为了安全起见,我包含了所有代码。
package ch7;
import java.util.Scanner;
import java.io.*;
public class DistributionChart
{
public static void main (String[] args) throws IOException
{
int size = 10;
int[] ranges = new int[size]; // each entry represents a range of values
getData(ranges); //pass the entire array into the method
displayChart(ranges);
getDataFromFile(ranges);
displayChartToFile(ranges);
System.out.println("\nSee you later!!");
} //end of main
public static void getData(int[] someArray)
{
Scanner scan = new Scanner (System.in);
System.out.println ("Enter a series of numbers between 1 and 100. Separate each number with a space.");
System.out.println ("Signal the end by entering a number outside " +
"of that range and then press enter.");
System.out.print("Go: ");
//reads an arbitrary number of integers that are in the range 1 to 100 inclusive
//for each integer read in, determine which range it is in and increment the corresponding element in the array
//your code goes here
int values = 0;
values = scan.nextInt();
while(values > 0 && values < 101)
{
if(values >= 1 && values <= 10)
{
someArray[0]++;
}
else if (values >= 11 && values <= 20)
{
someArray[1] ++;
}
else if (values >= 21 && values <= 30)
{
someArray[2]++;
}
else if(values >= 31 && values <= 40)
{
someArray[3]++;
}
else if(values >= 41 && values <= 50)
{
someArray[4]++;
}
else if(values >= 51 && values <= 60)
{
someArray[5]++;
}
else if(values >= 61 && values <= 70)
{
someArray[6]++;
}
else if(values >= 71 && values <= 80)
{
someArray[7]++;
}
else if(values >= 81 && values <= 90)
{
someArray[8]++;
}
else if(values >= 91 && values <= 100)
{
someArray[9]++;
}
values = scan.nextInt();
}
scan.close();
}//end of getData
public static void displayChart(int[] someArray)
{
System.out.println("Distribution chart by Royce T.");
System.out.println("================================");
//Print histogram.
for(int i = 0; i < someArray.length; i++)
{
System.out.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");
for(int y = 0; y < someArray[i]; y++)
{
System.out.print("*");
}
System.out.println();
}
} //end of displayChart
public static void getDataFromFile(int[] someArray) throws IOException
{
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
int values = 0;
while(inFile.hasNext())
{
values = inFile.nextInt();
while(values > 0 && values < 101)
{
if(values >= 1 && values <= 10)
{
someArray[0]++;
}
else if (values >= 11 && values <= 20)
{
someArray[1] ++;
}
else if (values >= 21 && values <= 30)
{
someArray[2]++;
}
else if(values >= 31 && values <= 40)
{
someArray[3]++;
}
else if(values >= 41 && values <= 50)
{
someArray[4]++;
}
else if(values >= 51 && values <= 60)
{
someArray[5]++;
}
else if(values >= 61 && values <= 70)
{
someArray[6]++;
}
else if(values >= 71 && values <= 80)
{
someArray[7]++;
}
else if(values >= 81 && values <= 90)
{
someArray[8]++;
}
else if(values >= 91 && values <= 100)
{
someArray[9]++;
}
values = inFile.nextInt();
}
inFile.close();
}
}
public static void displayChartToFile(int[] someArray) throws IOException
{
getDataFromFile(someArray);
PrintWriter outFile = new PrintWriter( "C:/Users/RTmag/workspaceRATI//CSC110/outputFile.txt");
//Print chart title with your name
outFile.println("Distribution chart by Royce T.");
outFile.println("================================");
//Print histogram.
for(int i = 0; i < someArray.length; i++)
{
outFile.print( ((i * 10 + 1)) + "-" + (((i *10 +1)) + 9) + " \t" + "|");
for(int x = 0; x <someArray[i]; x++)
{
outFile.print("*");
}
outFile.println();
}
outFile.close();
}
}
// end of DistributionChart tester class
我正在尝试让 displayChartToFile 方法写入位于 CSC110 文件夹中名为 outputFile.txt 的文件。此文件夹还包含我的 bin 和 src 文件夹。
每当我 运行 我的代码时,前两种方法在我输入后正确显示。
错误输出示例:
Enter a series of numbers between 1 and 100. Separate each number with a space.
Signal the end by entering a number outside of that range and then press enter.
Go: 55 66 555
Distribution chart by Royce T.
================================
1-10 |
11-20 |
21-30 |
31-40 |
41-50 |
51-60 |*
61-70 |*
71-80 |
81-90 |
91-100 |
Exception in thread "main" java.io.FileNotFoundException: C:\Users\RTmag\workspaceRATI\CSC110\inputFile.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at ch7.DistributionChart.getDataFromFile(DistributionChart.java:143)
at ch7.DistributionChart.main(DistributionChart.java:28)
第 28 行是:
getDataFromFile(ranges);
第 143 行是:
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
inputFile.txt里面的文字是
75 49 62 35 18 97 62 54 83 61 44 29 98 75 14
outputFile.txt 为空。
非常感谢任何帮助。谢谢!
如果我没有您可能需要帮助我的所有信息,请告诉我!
编辑:
我修复了文件位置,但现在它返回了一组新的错误。
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at ch7.DistributionChart.getDataFromFile(DistributionChart.java:207)
at ch7.DistributionChart.main(DistributionChart.java:30)
更改此行
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI//CSC110/inputFile.txt"));
要么
Scanner inFile = new Scanner(new FileReader("C:/Users/RTmag/workspaceRATI/CSC110/inputFile.txt"));
或
Scanner inFile = new Scanner(new FileReader("C:\Users\RTmag\workspaceRATI\CSC110\inputFile.txt"));
对我来说,这看起来不对...
int values = 0;
while (inFile.hasNext()) {
values = inFile.nextInt();
while (values > 0 && values < 101) {
//...
values = inFile.nextInt();
}
}
- 你检查一下文件是否有更多的行,好的
- 你得到下一个
int
值,很好 - 当值在
1..100
之间时循环,好的,但是如果到达输入的末尾会发生什么? - 你看下一个
int
……啊,不太清楚。
根据您输入的 75 49 62 35 18 97 62 54 83 61 44 29 98 75 14
,您可以简单地执行类似...
int values = 0;
while (inFile.hasNext()) {
values = inFile.nextInt();
//...
}
但是如果输入文件可能有多行,你可以做...
int values = 0;
while (inFile.hasNext()) {
String nextLine = inFile.nextLine();
Scanner line = new Scanner(nextLine);
while (line.hasNextInt()) {
values = line.nextInt();
//...
}
}
(即使只有一行也可以做到)
基本问题是,您正在读到文件末尾,Scanner
运行 没有数据要读取