简体   繁体   中英

Printing result into two columns error for java

Its just as the title stated I want to print something into two columns but I seem to be running to an error.

int noOfHorseInList = 7;
        String horseName[] = {"Blitz", "Sparky", "Sandy", "Rolo", "Beano", 

"Flash", "", "", ""};
        int horseAge[] = {2, 4, 1, 4, 4, 2, 0, 0, 0};
        int timeTakenInSeconds[] = {600, -1, 500, 430, 412, -1, 0, 0, 0};

    int option = 0;

    System.out.println("Please choose from the following selection");
    System.out.println("1. Show horses that finished\n2. Show horse details\n3. Adding a Late Entrant");
    option = kb.nextInt();
    kb.hasNextLine();

    switch(option)
    {
    case 1:
        //Finishers
        String showHorseThatFinished = "";
        showHorseThatFinished = finishers(noOfHorseInList, horseName, timeTakenInSeconds);

        String timeOfHorse = "";

        for(int i = 0; i < noOfHorseInList; i++)
        {
            if(timeTakenInSeconds[i] > -1)
            {
                timeOfHorse = timeOfHorse + "\n" + timeTakenInSeconds[i];
            }
        }


        System.out.println("Horses Name\tFinishing Time");
        System.out.println(showHorseThatFinished + "\t" + timeOfHorse);



        break;

    case 2:
        //Horse details





        break;

    case 3:
        //Adding Late Entrant



        break;

    }

}

public static String finishers(int noOfHorseInList,String horseName[],int timeTakenInSeconds[])
{
    String showHorseThatFinished = "";

    for(int i = 0; i < noOfHorseInList; i++)
    {
        if(timeTakenInSeconds[i] > -1)
        {
            showHorseThatFinished = showHorseThatFinished + "\n" + horseName[i];
        }
    }


    return showHorseThatFinished;
}

This is my output, I want the values into two columns next to each other

在此处输入图片说明

Append horse names and times in the same function.

Use StringBuffer for appending. It's more efficient.

public static String finishers(int noOfHorseInList,String   horseName[],int timeTakenInSeconds[]) {
StringBuffer showHorseThatFinished = new StringBuffer();

for(int i = 0; i < noOfHorseInList; i++) {
        // You need all horses to be written. Mark somehow that their time is erroneous
        String time = "ERROR";    

        if (timeTakenInSeconds[i] > -1) {
              time = "" + timeTakenInSeconds[i];
        }

        showHorseThatFinished.append(horseName[i]);
        showHorseThatFinished.append("\t");
        showHorseThatFinished.append(time);
        showHorseThatFinished.append("\n");
    }
}

    return showHorseThatFinished.toString();
}

There is no need for this piece of code anymore

    String timeOfHorse = "";

    for(int i = 0; i < noOfHorseInList; i++)
    {
        if(timeTakenInSeconds[i] > -1)
        {
            timeOfHorse = timeOfHorse + "\n" + timeTakenInSeconds[i];
        }
    }

Just

  System.out.println(showHorseThatFinished);

You are actually calculating too much...

take a look at this method:
finishers(int noOfHorseInList, String horseName[], int timeTakenInSeconds[])

modify it like this:

public static String finishers(int noOfHorseInList, String horseName[], int timeTakenInSeconds[]) {
        String showHorseThatFinished = "";

        for (int i = 0; i < noOfHorseInList; i++) {
            if (timeTakenInSeconds[i] > -1) {
                showHorseThatFinished = showHorseThatFinished + "\n" + horseName[i] + "\t\t\t" + timeTakenInSeconds[i];
            }
        }
        return showHorseThatFinished;

    }

and your table will be done with no need of the

for(int i = 0; i < noOfHorseInList; i++)
        {

in the case 1:

如何将 for 循环中的以下行更改为 -

timeOfHorse = timeOfHorse + "\t" + timeTakenInSeconds[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM