Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java异常打印两次_Java_Exception_Console_Printing - Fatal编程技术网

Java异常打印两次

Java异常打印两次,java,exception,console,printing,Java,Exception,Console,Printing,我知道异常是没有意义的,但是我试图学习如何使用/创建异常,所以我使用了这个。唯一的问题是由于某种原因,异常生成的错误消息打印到控制台两次 导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.PrintStream; 导入java.util.Scanner public class Project3 { public static void main(String[] args) { try {

我知道异常是没有意义的,但是我试图学习如何使用/创建异常,所以我使用了这个。唯一的问题是由于某种原因,异常生成的错误消息打印到控制台两次

导入java.io.File; 导入java.io.FileNotFoundException; 导入java.io.PrintStream; 导入java.util.Scanner

public class Project3
{

  public static void main(String[] args)
  {
    try
    {
      String inputFileName = null;
      if (args.length > 0)
        inputFileName = args[0];
      File inputFile = FileGetter.getFile(
          "Please enter the full path of the input file: ", inputFileName);

      String outputFileName = null;
      if (args.length > 1)
        outputFileName = args[1];
      File outputFile = FileGetter.getFile(
          "Please enter the full path of the output file: ", outputFileName);

      Scanner in = new Scanner(inputFile);
      PrintStream out = new PrintStream(outputFile);
      Person person = null;

      // Read records from input file, get an object from the factory,
      // output the class to the output file.
      while(in.hasNext())
      {
        String personRecord = in.nextLine();

        person = PersonFactory.getPerson(personRecord);

        person.display(); 

        person.output(out);
      }
    } catch (Exception e)
    {
      System.err.println(e.getMessage());
    }
  }

}





import java.util.Scanner;

class Student extends Person
{
  private double gpa;

  public Student()
  {
    super();
    gpa = 0.0;
  }

  public Student(String firstName, String lastName, double gpa)
  {
    super(firstName, lastName);
    this.gpa = gpa;
  }

  public String toString(){
   try{
        if (gpa >= 0.0 && gpa <= 4.0){
            return super.toString() + "\n\tGPA: " + gpa;
        }
        else {
            throw new InvalidGpaException();
        }
    }
   catch (InvalidGpaException e){
       System.out.println(e);
       return super.toString() + "\n\tGPA: " + gpa;
   }
  }

  public void display()
  {
    System.out.println("<<Student>>" + this);
  }

  @Override
  public void input(Scanner in)
  {
    super.input(in);

    if (in.hasNextDouble())
    {
      this.gpa = in.nextDouble();
    }
  }

  class InvalidGpaException extends Exception {
    public InvalidGpaException() {
        super("Invalid GPA: " + gpa);
      }
  }
}
公共类项目3
{
公共静态void main(字符串[]args)
{
尝试
{
字符串inputFileName=null;
如果(args.length>0)
inputFileName=args[0];
File inputFile=FileGetter.getFile(
请输入输入文件的完整路径:,inputFileName);
字符串outputFileName=null;
如果(参数长度>1)
outputFileName=args[1];
File outputFile=FileGetter.getFile(
请输入输出文件的完整路径:,outputFileName);
扫描仪输入=新扫描仪(输入文件);
PrintStream out=新的打印流(outputFile);
Person=null;
//从输入文件读取记录,从工厂获取对象,
//将类输出到输出文件。
while(在.hasNext()中)
{
字符串personRecord=in.nextLine();
person=PersonFactory.getPerson(personRecord);
person.display();
人员输出(输出);
}
}捕获(例外e)
{
System.err.println(e.getMessage());
}
}
}
导入java.util.Scanner;
班级学生延伸人
{
私人双gpa;
公立学生()
{
超级();
gpa=0.0;
}
公立学生(字符串名、字符串名、双gpa)
{
超级(名字,姓氏);
this.gpa=gpa;
}
公共字符串toString(){
试一试{

如果(gpa>=0.0&&gpa你的主要目标是什么?你的输入是什么。。 将您的异常更改为其他异常

你在哪里打印这些数据

<< Student>>
        Id: 2        Doe, Junior
        GPA: -4.0
>
身份证号码:2 Doe,初级
平均成绩:-4.0

您确定没有两次调用person.toString()吗?

您的主要任务是什么?您的输入是什么。。 将您的异常更改为其他异常

你在哪里打印这些数据

<< Student>>
        Id: 2        Doe, Junior
        GPA: -4.0
>
身份证号码:2 Doe,初级
平均成绩:-4.0

您确定没有两次调用person.toString()吗?

我猜您正在调用
toString
代码中您没有向我们显示的某个地方


toString
实现中放入
Thread.dumpStack();
应该会告诉您从哪里开始。

我猜您正在调用
toString
代码中未显示给我们的某个地方

toString
实现中放入
Thread.dumpStack();
会告诉您从何处开始。

尝试更改以下内容:

System.out.println(e);
   return super.toString() + "\n\tGPA: " + gpa;

(或类似的内容)

尝试更改以下内容:

System.out.println(e);
   return super.toString() + "\n\tGPA: " + gpa;


(或类似的东西)

我猜您的
Person.output()
方法在其中调用了
toString()
,它将在返回正确的字符串之前打印异常,而该字符串不会显示,因为您正在将其输出到
out


E:如果你想得到我的推论,这里是:第一条错误消息和正常消息在调用
display()
时被打印出来,这是应该的。紧接着就是
output()
调用,我猜它的名字就是用来做
display()的
会,除了文件。但是,您忘记了异常会直接打印到
System.out
,因此它会出现在控制台中,而
toString()
实际返回的字符串会写入文件。

我猜您的
Person.output()
方法调用了
toString()
中,它将在返回正确的字符串之前打印异常,该字符串不会显示,因为您正在将其输出到
out


E:如果你想得到我的推论,这里是:第一条错误消息和正常消息在调用
display()
时被打印出来,这是应该的。紧接着就是
output()
调用,我猜它的名字就是用来做
display()的
会,除了文件。但是,您忘记了异常会直接打印到
System.out
,因此它会出现在控制台中,而
toString()
实际返回的字符串会写入文件。

看到运行此操作的代码(可能是您的
main
方法)非常有帮助。主代码现在已启动。查看正在运行此操作的代码(可能是您的
main
方法)这将非常有帮助。主代码现在已经启动。数据同时打印到控制台和输出文件,控制台单独接收错误数据。数据同时打印到控制台和输出文件,控制台单独接收错误数据。如果我这样做,我会得到一个错误,那里必须有返回。如果我这样做,我会得到一个错误,那里必须有b我们回到那里。