2010-11-09 11 views
0

bien fondamentalement j'ai une ligne qui a un finial dedans de 40, si un ajouter 2 mots qui sont 10 lettres chaque long qui me laisse avec 20 espaces thouse 20 espaces devraient être rempli de points ...... par exemple bonjour ................................ vousAjouter des points dans un system.out.println

ou

pile ........................... débordement

maintenant j'ai le code fonctionne bien, je n'ai pas idée sur la façon de s'il vous plaît cela dans un system.out.println afin que le résultat est imprimé t o la console.

Je pensais à utiliser un whileloop pour imprimer les points un à la fois, après avoir testé combien de nombre de points il devrait y avoir une fois que les mots sont importés.

Au moment où j'ai ce qui ne fonctionne pas clairement

{ 
     System.out.println (word1 + ".." + word2); 

     while (wordlegnth1 + wordlegnth2 < LINELENGTH) 

     { 
      System.out.println (word1 + "." + word2); 

      wordlegnth1++; 

     } 

Répondre

3
final int LINE_LENGTH = 40; 

String word1 = ...; 
String word2 = ...; 

StringBuilder sb = new StringBuilder(LINE_LENGTH); 
sb.append(word1); 
for (int i = 0; i + word1.length() + word2.length() < LINE_LENGTH; i++) { 
    sb.append("."); 
} 
sb.append(word2); 

System.out.println(sb.toString()); 

Remarque: l'utilisation de StringBuilder est d'éviter la peine de performance due à cordes immuabilité

1
/** 
* Formats a given string to 40 characters by prefixing it with word1 and 
* suffixing it with word2 and using dots in the middle to make length of final 
* string = 40. 
* If string will not fit in 40 characters, -1 is returned, otherwise 0 is 
* returned. 
* 
* @param word1 the first word 
* @param word2 the second word 
* @return 0 if string will fit in 40 characters, -1 otherwise 
*/ 
public static int formatString(String word1, String word2) { 
    final int LINELENGTH = 40; 

    // determine how many dots we need 
    int diff = LINELENGTH - (word1.length() + word2.length()); 

    if (diff < 0) { 
     // it's too big 
     System.out.println("string is too big to fit"); 
     return -1; 
    } 
    // add the dots 
    StringBuilder dots = new StringBuilder(); 
    for (int i = 0; i < diff; ++i) { 
     dots.append("."); 
    } 

    // build the final result 
    String result = word1 + dots.toString() + word2; 

    System.out.println(result); 

    return 0; 

} 

public static void main(String[] args) throws Throwable { 
    formatString("stack", "overflow"); 
    String str = ""; 
    for (int i = 0; i < 21; ++i) { 
     str += "a"; 
    } 
    formatString(str, str); 
} 

Sortie:

stack...........................overflow 
string is too big to fit 
0

Pourquoi ne pas commencer avec quelque chose comme:

int lengthA = word1.length(); 
int lengthB = word2.length(); 
int required = LINE_LENGHT - (lengthA + lengthB); 

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

Il peut être amélioré (essayez les cordes qui sont très grandes par exemple, ou utilisez un StringBuilder plutôt que System.out.print).

0

ajouter commons-lang apache à votre classpath et utiliser StringUtils.leftPad()

System.out.println(str1 + StringUtils.leftPad(str2, 40 - str1.length(), '.')); 

pourquoi écrire une méthode pour faire quelqu'un quelque chose d'autre a déjà écrit + testé?

+0

Je suppose que c'est des devoirs ... donc plutôt que de se baser sur une bibliothèque étudiant doit comprendre la logique nécessaire pour l'implémenter. – TofuBeer

+0

qui ne faisait pas partie de la question. – pstanton

Questions connexes