Home > other >  Limiting # of characters per line
Limiting # of characters per line

Time:05-04

I'm a beginner and I'm having trouble trying to display the output so that if its too big it will move it to the next line.

This is what I have so far:

import java.util.Scanner;

public class numberBracket {

    public static void main(String[] args) {
        
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("howMany: ");
        int howMany = scanner.nextInt();
    
        
        System.out.println("lineLength: ");
        int lineLength = scanner.nextInt();
        scanner.nextLine();
        
        printNumbers(howMany, lineLength);
         
    }
        
    public static void printNumbers(int howMany, int lineLength) {
        Integer charLength =  Integer.valueOf(lineLength); 
        
        for(int i = 1; i <= howMany; i   ) {
            
            String t = "["   i   "]";
            
            
            while(t.length() > charLength ) {
                int index = t.lastIndexOf(' ', charLength);
                System.out.print(t.substring(0, index));
                t = t.substring(index   1);
            }
            System.out.print(t);
        }
}
}

So if they enter 10 for the lineLength it would be

[1][2][3]

[4]

and if they entered 12 it would be

[1][2][3][4]

CodePudding user response:

You can use this snippet:

    public static void printNumbers(int howMany, int lineLength) {
        StringBuilder sb = new StringBuilder();
        int length = 0;

        for (int i = 1; i <= howMany; i  ) {
            String t = "["   i   "]";

            if (length   t.length() > lineLength) {
                sb.append("\n");
                length = 0;
            }
            
            length  = t.length();

            sb.append(t);
        }
        
        System.out.println(sb.toString());
    }

CodePudding user response:

public static void printNumbers(int howMany, int lineLength) {
    String printed = "";

    for (int i = 1; i <= howMany; i  ) {
        String t = "["   i   "]";
        if ((printed.length()   t.length()) > lineLength) {
            printed = "";
            System.out.println();
        }
        printed  = t;
        System.out.print(t);
    }
}

CodePudding user response:

I believe you have to check if it has room in the current line before printing.

If it does have room print it, if it doesn't, print in a new line.

public static void printNumbers(int howMany, int lineLength) {
    int alreadyPrintedLength = 0;
    for(int i = 1; i <= howMany; i   ) {
      String t = "["   i   "]";
      int actualCharLength = t.length();
      boolean hasRoomInCurrentLine = (alreadyPrintedLength   actualCharLength) <= lineLength;
      if (hasRoomInCurrentLine) {
        System.out.print(t);
        alreadyPrintedLength  = actualCharLength;
      } else {
        System.out.print(System.lineSeparator()   t);
        alreadyPrintedLength = actualCharLength;
      }
    }
  }
  •  Tags:  
  • java
  • Related