将线性链表打印到表中

时间:2012-11-04 16:53:13

标签: java printing linked-list

我正在尝试将存储在LLL中的一些值打印到可读表中。 我存储的数据如下:

DEBBIE     STARR           F 3 W 1000.00
JOAN       JACOBUS         F 9 W  925.00
DAVID      RENN            M 3 H    4.75
ALBERT     CAHANA          M 3 H   18.75
DOUGLAS    SHEER           M 5 W  250.00
SHARI      BUCHMAN         F 9 W  325.00
SARA       JONES           F 1 H    7.50
RICKY      MOFSEN          M 6 H   12.50
JEAN       BRENNAN         F 6 H    5.40
JAMIE      MICHAELS        F 8 W  150.00

我已将每个名字,姓氏,性别,任期,薪水和工资存储到自己的列表中。并希望能够以与我在其中读取的文本文件中查看的格式相同的格式打印出来。我已经搞砸了一些方法,允许我遍历和打印列表,但我最终得到了丑陋的输出。 。 。

这是我的代码,用于存储文本文件和我想要打印的格式:

public class Payroll
{
  private LineWriter lw;
  private ObjectList output;
  ListNode input;
  private ObjectList firstname, lastname, gender, tenure, rate, salary;

  public Payroll(LineWriter lw)
  {
      this.lw = lw;
      this.firstname = new ObjectList();
      this.lastname = new ObjectList();
      this.gender = new ObjectList();
      this.tenure = new ObjectList();
      this.rate = new ObjectList();
      this.salary = new ObjectList();
      this.output = new ObjectList();
      this.input = new ListNode();
  } 
   public void readfile()
   {
       File file = new File("payfile.txt");
       try{
           Scanner scanner = new Scanner(file);
           while(scanner.hasNextLine())
           {
               String line = scanner.nextLine();
               Scanner lineScanner = new Scanner(line);
               lineScanner.useDelimiter("\\s+");
               while(lineScanner.hasNext())
               {
                   firstname.insert1(lineScanner.next());
                   lastname.insert1(lineScanner.next());
                   gender.insert1(lineScanner.next());
                   tenure.insert1(lineScanner.next());
                   rate.insert1(lineScanner.next());
                   salary.insert1(lineScanner.next());
                }
            }
        }catch(FileNotFoundException e)
        {e.printStackTrace();}
    }


   public void printer(LineWriter lw)
   {
       String msg = " FirstName " + " LastName " + " Gender " + " Tenure " +
                    " Pay Rate " + " Salary "; 
             output.insert1(msg);
             System.out.println(output.getFirst());
             System.out.println(" " + firstname.getFirst() + "      " + lastname.getFirst() + "\t" + gender.getFirst() +
            "\t" + tenure.getFirst() + "\t" + rate.getFirst() + "\t" + salary.getFirst());


     }

}

2 个答案:

答案 0 :(得分:1)

您可以使用System.out.printf()方法使用C-style format specifiers将字符串和整数打印到固定宽度的位置(使用对齐方式)。

答案 1 :(得分:0)

你试过这个

吗?
 String msg = " FirstName " + " LastName \t" + " Gender \t" + " Tenure \t" +
                " Pay Rate \t" + " Salary \t";