尝试在Java中的字符串之间创建填充虚线

时间:2015-07-24 21:06:59

标签: java

程序问题,我需要以下代码,在信息的左栏和页码的右栏之间打印并创建填充的虚线。它主要起作用,但右栏没有正确排列。我将包含我的所有代码,以便更容易看到我出错的地方。 非常感谢任何帮助!

public class Anderson13 {

    /**
    * @param args
    */
    public static void main(String[] args) {
        TableOfContents toc = new TableOfContents();
        toc.addChapter("Introduction", 17);
        toc.addChapter("Variables", 11);
        toc.addChapter("If Statements", 1);
        toc.addSection("If", 7);
        toc.addSection("Else", 6);
        toc.addChapter("Loops", 12);
        toc.setLineLength(68);
        toc.printToConsole();
        toc.setLineLength(72);
        toc.printToFile("JavaBook.txt");
    }
}

public class TableOfContents {
    private int chapcount = 0;
    private int seccount = 0;
    private int lenght;

    ArrayList<Integer> listOfInts = new ArrayList<Integer>();
    ArrayList<String> chapter = new ArrayList<String>();

    public void addChapter(String title, int page) {
        chapcount++;
        chapter.add("Chapter " + chapcount + ":" + " " + title + " ");
        listOfInts.add(page);   
    }

    public void addSection(String title, int page) {
        seccount++;
        chapter.add("  Section " + seccount + ":" + " " + title + " ");
        listOfInts.add(page);
    }

    // takes arraylists and changes toString
    String str = listOfInts.toString().replaceAll("[\\[\\]]", ".");
    String nextString = listOfInts.toString().replaceAll("[\\[\\]]", ".");
    PaddedLine line = new PaddedLine(str, nextString);
    public void setLineLength(int lenght) {
        this.lenght = lenght;
    }

    public void printToConsole() {
        for (int i = 0; i < listOfInts.size(); i++) {
            System.out.println(chapter.get(i) + line.getString(lenght)
            + +listOfInts.get(i));
        }
    }

    public void printToFile(String filename) {
        try {
            FileWriter myFile = new FileWriter(filename);
            PrintWriter out = new PrintWriter(myFile);
            for (int i = 0; i < listOfInts.size(); i++) {
                out.println(chapter.get(i) + line.getString(lenght)
                + +listOfInts.get(i));
            }
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

public class PaddedLine {
    private String beforeDots; // e.g. "Chapter 2: Variables "
    private String afterDots; // e.g. " 18"

    // Helper method to create a string of the right number of dots
    private String getDots(int numDots) {
        String dots = "";
        for (int i = 0; i < numDots; i++)
        dots += ".";
        return dots;
    }

    // Constructor only needs strings for either side of the padding
    public PaddedLine(String beforeDots, String afterDots) {
        this.beforeDots = beforeDots;
        this.afterDots = afterDots;
    }

    // Select the line length when getting the string by using the helper
    public String getString(int lineLength) {
        int lengthUsed = beforeDots.length() + afterDots.length();
        return beforeDots + getDots(lineLength - lengthUsed) + afterDots;
    }
}

产生输出

Chapter 1: Introduction ....................................................................17
Chapter 2: Variables ....................................................................11
Chapter 3: If Statements ....................................................................1
  Section 1: If ....................................................................7
  Section 2: Else ....................................................................6
Chapter 4: Loops ....................................................................12

1 个答案:

答案 0 :(得分:1)

与章节相似,您还需要有paddedLine列表。以下是更新后的代码。

package edu.uga.cs1302.mp3files;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

public class Anderson13 {

    public static void main(String[] args) {
        TableOfContents toc = new TableOfContents();
        toc.addChapter("Introduction", 17);
        toc.addChapter("Variables", 11);
        toc.addChapter("If Statements", 1);
        toc.addSection("If", 7);
        toc.addSection("Else", 6);
        toc.addChapter("Loops", 12);
        toc.setLineLength(68);
        toc.printToConsole();
        toc.setLineLength(72);
        toc.printToFile("JavaBook.txt");
    }

}

class TableOfContents {
    private int chapcount = 0;
    private int seccount = 0;
    private int lenght;

    ArrayList<Integer> listOfInts = new ArrayList<Integer>();
    ArrayList<String> chapter = new ArrayList<String>();
    ArrayList<PaddedLine> padedLines=new ArrayList<>();

    public void addChapter(String title, int page) {
        chapcount++;
        chapter.add("Chapter " + chapcount + ":" + " " + title + " ");
        listOfInts.add(page);

        PaddedLine line = new PaddedLine("Chapter " + chapcount + ":" + " " + title + " ",String.valueOf(page));
        padedLines.add(line);

    }

    public void addSection(String title, int page) {
        seccount++;
        chapter.add("  Section " + seccount + ":" + " " + title + " ");
        listOfInts.add(page);

        PaddedLine line = new PaddedLine("  Section " + seccount + ":" + " " + title + " ",String.valueOf(page));
        padedLines.add(line);
    }


    public void setLineLength(int lenght) {
        this.lenght = lenght;

    }



    public void printToConsole() {
        for (int i = 0; i < listOfInts.size(); i++) {
            System.out.println( chapter.get(i)+padedLines.get(i).getString(lenght)
                    + listOfInts.get(i));
        }
    }

    public void printToFile(String filename) {
        try {
            FileWriter myFile = new FileWriter(filename);
            PrintWriter out = new PrintWriter(myFile);
            for (int i = 0; i < listOfInts.size(); i++) {
                out.println(chapter.get(i) + padedLines.get(i).getString(lenght)
                        + +listOfInts.get(i));
            }
            out.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}

class PaddedLine {
    private String beforeDots; // e.g. "Chapter 2: Variables "
    private String afterDots; // e.g. " 18"

    // Helper method to create a string of the right number of dots
    private String getDots(int numDots) {
        String dots = "";
        for (int i = 0; i < numDots; i++)
            dots += ".";
        return dots;
    }

    // Constructor only needs strings for either side of the padding
    public PaddedLine(String beforeDots, String afterDots) {
        this.beforeDots = beforeDots;
        this.afterDots = afterDots;
    }

    // Select the line length when getting the string by using the helper
    public String getString(int lineLength) {
        int lengthUsed = beforeDots.length() + afterDots.length();
        return getDots(lineLength - lengthUsed) ;
    }

} 

<强>输出

Chapter 1: Introduction ..........................................17
Chapter 2: Variables .............................................11
Chapter 3: If Statements ..........................................1
  Section 1: If ...................................................7
  Section 2: Else .................................................6
Chapter 4: Loops .................................................12