简体   繁体   English

使用PrintWriter时出错

[英]Error when using PrintWriter

I am trying to write a program which prompts the user to enter a velocity and time. 我正在尝试编写一个程序,提示用户输入速度和时间。 After this I need to calculate distance = velocity * time . 之后,我需要计算distance = velocity * time If the user enters a time less than zero and bigger than time, then I need to re-prompt the user. 如果用户输入的时间小于零且大于时间,那么我需要重新提示用户。

Hour        Distance Travelled
===========================
1           40
2           80
3           120

For example if the user enters time as 5, the table should resemble the following: 例如,如果用户将时间输入为5,则该表应类似于以下内容:

Hour        Distance Travelled
===========================
1           40
2           80
3           120
4           160
5           200

I need a table like above, but I need to output the table to a text file. 我需要像上面的表格,但是我需要将表格输出到文本文件。

Here is my code: 这是我的代码:

import java.io.*;
import java.util.Scanner;

public class Lab4 {
    public static void main(String[] args) throws IOException {
        double distance;
        double velocity;
        double time;

        System.out.println("enter the velocity and time");
        Scanner sn = new Scanner(System.in);
        velocity = sn.nextDouble();
        time = sn.nextDouble();

        do {
            System.out.print(" Time cannot be smaller than zero and larger than ten");
            System.out.print("Please enter again");
            time = sn.nextDouble();
        } while(time < 0 && time > 10);

        System.out.print("please enter the file name");
        String filename = sn.nextLine();

        PrintWriter outputFile = new PrintWriter(filename);
        distance = velocity*time;
        outputFile.println(distance);
    }
}

Question 1 why I am getting this error: 问题1为什么我收到此错误:

PrintWriter outputFile = new PrintWriter(filename);
^
bad source file: .\PrintWriter.java
file does not contain class PrintWriter
Please remove or make sure it appears in the correct subdirectory of the sourcepath.

Question 2: How can I draw that file ? 问题2:如何绘制该文件?

There are a number of issues with your code and aleb2000 has already mentioned a big one (take aleb2000 's advice) but we'll only touch on the one your question is ultimately about and that is the error you are receiving. 您的代码有很多问题,而aleb2000已经提到了一个大问题(请接受aleb2000的建议),但是我们只涉及您的问题最终涉及的问题,那就是您收到的错误。

You're getting this error because your supplied output file name is actually a Scanner newline character and PrintWriter doesn't know what to do with that. 之所以出现此错误,是因为提供的输出文件名实际上是Scanner 换行符,PrintWriter不知道该怎么做。 It's not something it recognizes as a valid path and file name. 它不能识别为有效的路径和文件名。

Why is your supplied file name incorrect? 为什么提供的文件名不正确? Well, it's actually quite simple, there is a little quirk with the Scanner Class when utilizing the nextInt(), nextDouble() methods, or any Scanner method for that matter that expects a numerical value to be supplied, and that is when you hit the enter button on your keyboard you are also supplying a newline character which remains stored within the Scanner buffer and is only released when the Scanner.newLine() method is used as you do when you ask for a file name to be supplied. 好吧,这实际上很简单,在使用nextInt(),nextDouble()方法或任何希望提供数值的事件的Scanner方法时,Scanner类会有一些古怪之处,即当您点击在键盘上的Enter按钮上,您还提供了换行符,该换行符仍存储在Scanner缓冲区中,并且仅在使用Scanner.newLine()方法时(如您要求提供文件名时一样)才会释放。 The newline character is not issued with the numerical values you supply however, when you supply the file name it is pulled forward from the buffer and takes the place of what was actually typed for the file name. 换行符不会随您提供的数值一起发出,但是,当您提供文件名时,它将从缓冲区中拉出,并取代了实际为文件名键入的内容。 Does this make sense to you? 你能理解这个吗?

Fortunately there is an easy solution to this problem and that is to simply apply the Scanner.newLine() method to nothing (no variable) directly after your last numerical input request, for example: 幸运的是,有一个解决此问题的简便方法,那就是在您上次输入数值后直接将Scanner.newLine()方法应用于任何内容(无变量),例如:

time = sn.nextDouble(); sn.nextLine();

You will obviously want to do this within your do/while loop as well but in my opinion you should do away with the do/while and just use a while loop like this (can you see why?): 您显然显然也希望在do / while循环中执行此操作,但我认为您应该取消do / while并仅使用while循环这样(您知道为什么吗?):

while(time < 0.0 || time > 10.0) {
    System.out.println("Time cannot be smaller than zero and larger than ten!");
    System.out.print("Please enter again: --> ");
    time = sn.nextDouble(); sn.nextLine();
} 

Oh...and don't for get to close both your PrintWriter Object and your Scanner Object once the task is complete. 哦...而且不要在任务完成后同时关闭 PrintWriter对象和Scanner对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM