gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:17:38 25 4
gpt4 key购买 nike

程序问题是我需要下面的代码来打印并在左信息列和右页码列之间创建填充虚线。它基本上可以工作,但右列没有正确排列。我将包含我的所有代码,以便更容易地看到我哪里出错了。非常感谢任何帮助!

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

最佳答案

与章节和章节一样,您也需要有 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

关于java - 尝试在Java中的字符串之间创建填充虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31619796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com