java - 在 Java 中运行程序时输出未完全显示

标签 java output bluej

我正在使用BlueJ,供引用。

程序编译并运行良好。

问题是输出没有输出全部内容。

这是当前的输出:

H Sanders,HarlandDavid   277651 8.72 false
S Baron,James 368535 310236.0
S Moran,Blake 123456 260000.0
H Bob,Billy 654321 15.0 false
H Smith,Will 345612 10.5 true 30
6) Remove a worker who is NOT in the list
The Employee is not Found

7) Remove a worker who is the first in the list 
H MacDonald,Ronald 386218 7.8 true 40
H Walton,Samuel 268517 8.21 false
H Thomas,David 131313 9.45 true 38
H Sanders,HarlandDavid   277651 8.72 false
S Baron,James 368535 310236.0
S Moran,Blake 123456 260000.0
H Bob,Billy 654321 15.0 false
H Smith,Will 345612 10.5 true 30

8) Find a worker who is the middle of the list
Found at 4

9) Find a worker who is NOT in the list
Found at -1

10) Find the weekly salary of a worker who is salaried
5000.0

11) Find the weekly salary of an hourly worker who has no overtime allowed [50 hours]
750.0

12) Find the weekly salary of an hourly worker who has overtime allowed [50 hours]
630.0

13) Find the weekly salary of an hourly worker who has overtime allowed [20 hours]
210.0

14) Print the sorted list
H MacDonald,Ronald 386218 7.8 true 40
H Walton,Samuel 268517 8.21 false
H Thomas,David 131313 9.45 true 38
H Sanders,HarlandDavid   277651 8.72 false
S Baron,James 368535 310236.0
S Moran,Blake 123456 260000.0
H Bob,Billy 654321 15.0 false
H Smith,Will 345612 10.5 true 30

15) End the process

正如我的主类 WorkerApp 所示,输出大约还有三分之一。相反,输出从 5) 的中间开始并显示其余部分,而不输出 5) 之前的内容。

import java.io.*;
import java.util.*;
public class WorkerApp{
/**
* Reads the infile, runs tests, and prints the output.
*/
public static void  main (String args[]){
    Company company = new Company();
    try{
        Scanner reader = new Scanner (new File("EmployeeData.txt"));
        while(reader.hasNext()){
            String line = reader.nextLine();
            String Employee[] = line.split("\\s+");
            String sorh = Employee[0];
            String name = Employee[1];
            String id = Employee[2];
            double salary = Double.parseDouble(Employee[3]);
            Employee e;
            if (Employee[0].equals("S")){
                e = new SalariedWorker(sorh, name, id, salary);}
            else {
                boolean overtime = Boolean.parseBoolean(Employee[4]);
                if(overtime){
                    int maxHours = Integer.parseInt(Employee[5]);
                     e = new HourlyWorker(sorh, name, id, salary, maxHours);
                }
                else{
                    e = new HourlyWorker(sorh, name, id, salary);
                }
            }
            company.add(e);
        }
    }catch (Exception err){
        //System.out.println(err);
        err.printStackTrace();
    }

    //Test Number 1
    System.out.println("1) Add a salaried worker");
    SalariedWorker SWorker1 = new SalariedWorker("S", "Moran,Blake", "123456", 260000);
    company.add(SWorker1);
    company.print();

    //Test Number 2
    System.out.println("2) Add an hourly worker who has no overtime allowed");
    HourlyWorker HWorker1 = new HourlyWorker("H", "Bob,Billy", "654321", 15);
    company.add(HWorker1);
    company.print();

    //Test Number 3
    System.out.println("3) Add an hourly worker who has overtime allowed");
    HourlyWorker HWorker2 = new HourlyWorker("H", "Smith,Will", "345612", 10.5, 30);
    company.add(HWorker2);
    company.print();

    //Test Number 4
    System.out.println("4) Add a worker that is already in the database");
    try{
        company.add(SWorker1);
    }catch(Exception err){
        System.out.println(err);
        System.out.println();
    }   

    //Test Number 5
    System.out.println("5) Print the sorted list");
    company.print();

    //Test Number 6
    System.out.println("6) Remove a worker who is NOT in the list");
    company.remove("Brooks,Phil");
    System.out.println();

    //Test Number 7
    System.out.println("7) Remove a worker who is the first in the list ");
    company.remove("Washington,George");
    company.print();
    System.out.println();

    //Test Number 8
    System.out.println("8) Find a worker who is the middle of the list");
    int index = company.find("Baron,James");
    System.out.println("Found at "+ index);
    System.out.println();

    //Test Number 9
    System.out.println("9) Find a worker who is NOT in the list");
    index = company.find("Harrison,Ford");
    System.out.println("Found at "+ index);
    System.out.println();

    //Test Number 10
    System.out.println("10) Find the weekly salary of a worker who is salaried");
    System.out.println(SWorker1.FindSalary());
    System.out.println();

    //Test Number 11
    System.out.println("11) Find the weekly salary of an hourly worker who has no overtime allowed [50 hours]");
    System.out.println(HWorker1.FindSalary(50));
    System.out.println();

    //Test Number 12
    System.out.println("12) Find the weekly salary of an hourly worker who has overtime allowed [50 hours]");
    System.out.println(HWorker2.FindSalary(50));
    System.out.println();

    //Test Number 13
    System.out.println("13) Find the weekly salary of an hourly worker who has overtime allowed [20 hours]");
    System.out.println(HWorker2.FindSalary(20));
    System.out.println();

    //Test Number 14
    System.out.println("14) Print the sorted list");
    company.print();

    //Test Number 15
    System.out.println("\n15) End the process");
} 
}

如果有帮助,这是它读取的文本文件:

S       Washington,George       000001      125000
H   MacDonald,Ronald        386218     7.80 true  40
H       Walton,Samuel           268517  8.21    false
H   Thomas,David            131313  9.45    true    38
H   Sanders,HarlandDavid    277651  8.72    false
S   Baron,James         368535  310236

这是主要类之一,Company:

import java.io.*;
import java.util.*;
public class Company{
private Employee[] employeeArray;
private final int InitialCapacity = 7;
private int employCount;

/**
* Creates the employee array and sets employCount to 0.
*/
public Company(){
    employeeArray = new Employee[InitialCapacity];
    employCount = 0;
}

/**
* Finds an employee in the list.
*/
public int find(String name){
    for (int i = 0; i < employCount; i++){
        if (employeeArray[i].getName().equals(name)){
            return i;
        }
    }

    return -1;
}

/**
* Adds an employee to the list.
*/
public int add(Employee employ){
    int index;
    for (index = 0; index < employCount; index++){
        int result = employeeArray[index].getName().compareTo(employ.getName());
        if(result == 0){
            throw new RuntimeException ("The Employee Is Not New");
        }
    }

    if (employeeArray.length == employCount){
        expand();
    }

    employeeArray[index] = employ;
    employCount++;
    return index;
}

/**
* Removes an employee to the list.
*/
public void remove(String name){
    int index = find(name);
    if (index == -1){
        System.out.println("The Employee is not Found");
        return;
    }

    for (int i = index; i < employCount - 1; i++){
        employeeArray[i] = employeeArray[i + 1];
    }

    employCount--;
}

/**
* Prints the list.
*/
public void print(){
    if(employCount == 0){
        System.out.println("The List is Empty");
        return;
    }

    for(int i = 0; i < employCount; i++){
        System.out.println(employeeArray[i]);
    }
}

/**
* Expands the list.
*/
private void expand(){
    Employee[] newArray = new Employee[employeeArray.length + InitialCapacity];
    for (int i = 0; i < employeeArray.length; i++){
        newArray[i] = employeeArray[i];
    }

    employeeArray = newArray;
}
}  

提前谢谢您!

最佳答案

似乎您必须扩展 BlueJ as shown here. 中输出的行号

“在 BlueJ 终端窗口中,转到 Options 并打开 Unlimited Buffering。这将解决您的问题。您还可以在方法调用时打开 Clear Screen 以进行以下操作:每个程序都在清晰的屏幕上运行。” - 极限编码员

关于java - 在 Java 中运行程序时输出未完全显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26819972/

相关文章:

按升序组合两个整数文件,比较它们,然后将它们排序到第三个文件中,而不使用c中的数组

c++ - 15和015有什么区别?

ios - Xcode iOS 测试套件 NSLog 输出丢失

java - 我应该如何克隆嵌套 ArrayList?

java - 尝试在方法中访问三角形数组时出现空指针异常

java - BlueJ 导入自定义类库

java - 崩溃时如何处理调用?

java - 改造 + rxJava : how to implement iterable N requests?

java - 无法为 Android 运行 Espresso 测试

Java - 如何验证当鼠标悬停在按钮上时是否显示替代文本?