如何在java中获得正确的空间填充

如何在java中获得正确的空间填充,java,string,formatting,Java,String,Formatting,我试图将所有的名字和数字整齐地对齐,间距为10个字符,但由于某种原因,格式混乱 我使用.trim()来去除空白,并尝试使用它作为分隔符,但格式仍然混乱 import java.util.Scanner; import java.io.File; public class StraightAs { public static void main(String[] args) throws Exception { if (args.length == 0) {

我试图将所有的名字和数字整齐地对齐,间距为10个字符,但由于某种原因,格式混乱

我使用.trim()来去除空白,并尝试使用它作为分隔符,但格式仍然混乱

import java.util.Scanner;
import java.io.File;

public class StraightAs {

    public static void main(String[] args) throws Exception {
        if (args.length == 0) {
            // TODO: the line of code below is a checkstyle violation.
            // Figure out what the violation is and what you can do to fix it.
            // You can find instructions on how to run checkstyle in the
            // homework instructions
            System.out.println("Welcome to Java. Are you ready to take on the challenge?\nMake sure you read and fully understand the provided instructions and the comments in this file to ensure you get full credit for your work.");



            showUsage("");
        } else if (args.length == 3) {
            // The code you write in processGrades will be called when your
            // program is run with two additional command line arguments, for
            // example: java StraightAs myfile.csv myseparator
            processGradesFromFile(args[0], args[1], args[2]);
        } else {
            showUsage(args[0]);
        }
    }


    /**
     * This method will create a grade histogram from a given csv file.
     * In your implementation, you need to read in the data, parse it as a CSV
     * formatted file with the provided data separator string, and output the
     * processed data to your terminal window. See the @param tags below for
     * more details on each parameter
     *
     * @param filename      the filename of the CSV file to read and process
     *                      data from
     * @param separator     the CSV file's given data separator. This is the
     *                      string constant that your program should use to
     *                      split each line in the CSV file into data fields
     *                      for parsing
     * @param displayMode   the type of output your program should show. We
     *                      define three modes: TABLE, HIST, and BOTH. See the
     *                      homework instructions for information on each of
     *                      these modes
     * @throws Exception    Don't worry about what this means yet. You'll learn
     *                      about exceptions later in the course =)
     */
    public static void processGradesFromFile(
            String filename, String separator, String displayMode)
        throws Exception {
        // TODO implement me!
        Scanner scan = new Scanner(new File(filename));



        while(scan.hasNextLine()){

            System.out.printf("%10s\n", scan.nextLine().trim());

        }

        scan.close();

        if (displayMode == "TABLE"){

        } else if (displayMode == "HIST") {





        } else if (displayMode == "BOTH"){

        }


    }

已编辑

你不允许用标签吗?有“\t”吗?当然,你会更喜欢它,而不是在它们之间添加10个空格。添加10个空格将丢失基于名称长度的格式

这将在您的工作时间内完成

    while(scan.hasNextLine())
    {
        String[] splitted = scan.split(",");

        for(int i = 0; i<splitted.length; i++)
        {
            System.out.printf("%s\t\n", splitted[i].trim());
        }
        System.out.println(""); //for new line, or else print("\n");
    }

不要使用
==
比较字符串=。请改用
equals(…)
equalsIgnoreCase(…)
方法。了解
==
检查两个对象引用是否相同,这不是您感兴趣的。另一方面,这些方法检查两个字符串是否具有相同顺序的相同字符,这就是这里的重点。另外,请阅读有关文件和分隔符的csv性质的注释。你需要对此作出解释。10不是字段之间的间距-它是为每个打印值留出的最小空间量。您正在传递包含要打印的整行的1个字符串,未分离。因此,displayMode.equalsIgnoreCase(“表格”)将是执行该部分的正确方法?我们被告知不要为分隔符部分硬编码任何内容,因此我不确定该怎么做。我想知道使用
%-10s\n
可以得到什么。打印字符串,用空格填充其余10个字符,然后添加硬编码换行符。。。那么,添加这些空白有什么意义呢?您的意思可能是
%10s\n
?此代码在输出的最左边添加了空格,如下所示。我想做的是让它看起来有点像这样。我在想也许我应该用一个分隔符来试着删除逗号?我会继续努力的。非常感谢你的帮助!但是您实际上不能使用该格式,因为您已经在下一行之前\n考虑过我将进行格式设置,但是如果您已经这样做了,请在此评论中通知我它们是每行2个字符串的常数吗?允许它进行一些暴力编码吗?
import java.util.Scanner;
import java.io.File;

public class StraightAs 
{
    public static void main(String[] args) throws Exception 
    {
        if (args.length == 0) 
        {
            // TODO: the line of code below is a checkstyle violation.
            // Figure out what the violation is and what you can do to fix it.
            // You can find instructions on how to run checkstyle in the
            // homework instructions

            System.out.println("Welcome to Java. Are you ready to take on the challenge?\nMake sure you read and fully understand the provided instructions and the comments in this file to ensure you get full credit for your work.");

            showUsage("");
        }

        else if (args.length == 3) 
        {
            // The code you write in processGrades will be called when your
            // program is run with two additional command line arguments, for
            // example: java StraightAs myfile.csv myseparator
            processGradesFromFile(args[0], args[1], args[2]);
        }

        else 
        {
            showUsage(args[0]);
        }
    }


    /**
     * This method will create a grade histogram from a given csv file.
     * In your implementation, you need to read in the data, parse it as a CSV
     * formatted file with the provided data separator string, and output the
     * processed data to your terminal window. See the @param tags below for
     * more details on each parameter
     *
     * @param filename      the filename of the CSV file to read and process
     *                      data from
     * @param separator     the CSV file's given data separator. This is the
     *                      string constant that your program should use to
     *                      split each line in the CSV file into data fields
     *                      for parsing
     * @param displayMode   the type of output your program should show. We
     *                      define three modes: TABLE, HIST, and BOTH. See the
     *                      homework instructions for information on each of
     *                      these modes
     * @throws Exception    Don't worry about what this means yet. You'll learn
     *                      about exceptions later in the course =)
     */

    public static void processGradesFromFile(
            String filename, String separator, String displayMode)
        throws Exception 
    {

        // TODO implement me!

        Scanner scan = new Scanner(new File(filename));

        while(scan.hasNextLine())
        {
            String[] splitted = scan.split(",");

            for(int i = 0; i<splitted.length; i++)
            {
                System.out.printf("%s\t\n", splitted[i].trim());
            }
            System.out.println(""); //for new line, or else print("\n");
        }

        scan.close();

        if (displayMode.equalsIgnoreCase("TABLE"))
        {
        } 

        else if (displayMode.equalsIgnoreCase("HIST")) 
        {
        }

        else if (displayMode.equalsIgnoreCase("BOTH"))
        {
        }
    }
}
    while(scan.hasNextLine())
    {
        System.out.printf("%10s%s\n", "", scan.nextLine().trim());
    }

    //%10s to add 10 space
    //%s for your scanned nextLine()
    //.trim() to trim your input