import java.util.Scanner; //Import necessary classes
import java.text.DecimalFormat;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileWriter;
import java.io.FileNotFoundException;
public class Lab11
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
DecimalFormat fmt = new DecimalFormat("0.00");
final int LIMIT = 1000, MAX = 10, MIN = -10; //number of ints to be read into file
String fileName; //holds name of file
System.out.print("Enter the name of the file to hold the integers: ");
fileName = scan.next();
PrintWriter outFile = null; //creates PrintWriter to write to specified file
try
{
outFile = new PrintWriter(new File(fileName)); //TryCatch statement to catch any errors when file opens
}
catch (FileNotFoundException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}
for (int i = 0; i <= LIMIT; i++) { //for loop to write to file using random num between -10 and 10
outFile.print((int)(Math.random()*(MAX+1-MIN)) + MIN + " "); //makes sure numbers in file are separated by spaces
outFile.close(); //closes file for output
}
Scanner inFile = null; //creates scanner to read in file
try //TryCatch statement to catch errors
{
inFile = new Scanner(new File(fileName));
}
catch (FileNotFoundException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}
int num, neg = 0, pos = 0, zero = 0; //variables to hold current number, positives, negatives and average
double avg = 0;
while (inFile.hasNext()) // Use a while loop to read in numbers from the file until the end
{
inFile.nextLine();
fileName = inFile.nextLine();
avg = 0;
num = 0;
while(inFile.hasNextInt())
{
num = inFile.nextInt();
avg += num; //adds current num to avg
if (num > 0)
pos++; //checks if positive and adds one to total pos
if (num < 0)
neg++; //checks is negative and adds one to total neg
else
zero++; //checks for zeros and adds one to total
}
avg = (avg/LIMIT); //calculates actual average
inFile.close(); //closes file for input
}
outFile = null; //Opens file for output again
try
{
outFile = new PrintWriter(new FileWriter(fileName, true)); //TryCatch statement to catch any errors when file opens
} //Allows file to be appended to instead of truncated
catch (IOException e)
{
System.out.println("File "+fileName+" doesn't exist. Exiting program");
System.exit(-1);
}
System.out.println("\n\nNumber of negative numbers in the file: " + neg);
System.out.println("\nNumber of positive numbers in the file: " + pos);
System.out.println("\nNumber of zeroes in the file: " + zero);
System.out.println("\nAverage of the numbers in the file: " + fmt.format(avg));
outFile.print("Number of negative numbers in the file: " + neg);
outFile.print("Number of positive numbers in the file: " + pos);
outFile.print("Number of zeroes in the file: " + zero);
outFile.print("Average of the numbers in the file: " + fmt.format(avg));
outFile.close();
} // End of main
}
私はJavaの入門を取っていますが、私はそれに残忍です。何時間練習したり勉強したりしても、なかなか上達しません。私はこれだけやり遂げましたが、今はこれを手に入れています。
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1585)
at Lab11.main(Lab11.java:89)
どこで間違ったのかわかりません。どんなガイダンスも素晴らしいでしょう。ありがとう。