简体   繁体   中英

characters in for loop printing out too far away from values

I am very new to java and for loops. I would like my code to print out this

Andy      **
Kristy    **********
Amy       ***** `

But my I am getting this instead

Andy          **
Kristy          **********
Amy          *****

How do I change my for loop to stop the spacing from the names being this way? Thank you.

public static void drawChart(String name, int age) {
    System.out.print(name);   
    for(int j = 1; j <= 10; j++) {
        System.out.print(" ");
    }

    for (int i=0; i<age; i++) {
        System.out.print("*");    
    }

    System.out.println(" "); 
}

You can use String.format() utility to write the name in a certain width. If the name is less than that width, it'll fill the rest with spaces. Something like that.

public static void drawChart(String name, int age){
        String nameWithPadding = String.format("%-20s", name);
        System.out.print(nameWithPadding);
        for (int i = 0; i < age; i++)
            System.out.print('*');
        System.out.println();
}

Here, this part %-20s is writing the name in 20 chars. First it writes the name, followed by spaces until it reaches 20 chars.

Instead of printing 10 spaces, you can print a number of spaces with is a function of the name length:

System.out.print(name);   

for(int j = 1; j <= 20 - name.length(); j++) { // the value 20 should be 
                                               // higher than the longest name
    System.out.print(" ");
}

for (int i=0; i<age; i++) {
    System.out.print("*");    
}

System.out.println(" ");

If you are using Java 8 you can use String::join like this :

public static void drawChart(String name, int age) {
    String spaces = String.join("", Collections.nCopies(20 - name.length(), " "));
    String stars = String.join("", Collections.nCopies(age, "*"));
    System.out.println(name + spaces + stars);
}

The above answers are good, but they do not consider the situation when you call the function with the longest name and then you call the function again with the next longest name. In that case the spacing would be different. Here is the solution with due respect to that issue:

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import java.util.Map;
import java.util.Set;

public class Search {

    public static void drawChart(Multimap<String, Integer> persons){
        //we use Multimap because we may have more than one person with the same name
        Set<String> names = persons.keySet();
        int maxLength = 0;
        for (String name : names) {
            if (name.length() > maxLength) {
                maxLength = name.length(); //define the longest name
            }
        }

        for (Map.Entry<String, Integer> entry : persons.entries()) {
            String name = entry.getKey();
            System.out.print(name);
            if (name.length() < maxLength) {
                //adjusting the number of spaces required to be on the same level
                //with the longest name
                int diff = maxLength - name.length();
                for (int i = 0; i < diff; i++) {
                    System.out.print(" ");
                }
            }
            Integer age = entry.getValue();
            for (int i = 0; i < age; i++) {
                System.out.print("*");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        Multimap<String, Integer> map = ArrayListMultimap.create();
        map.put("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" +
            "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 2);
        map.put("ffffffffffffffffffffffffffffffffffffffffffffffffff" +
            "fffffffffffffffffffffffff", 3);
        map.put("zz", 3);
        map.put("zz", 6);
        map.put("ffff", 9);
        drawChart(map);
    }
}

The output:

zz                                                                                    ***
zz                                                                                    ******
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff           ***
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa**
ffff                                                                                  *********

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