How do I get this to output 12 strings per line

Discussion in 'Java' started by Louie LaRue, Oct 30, 2006.

  1. Louie LaRue

    Louie LaRue Guest

    This is a program that randomizies the strings in the array 20 times
    and outputs them in a single line. How do I make it output 12 strings
    per line?

    thanks, Lane




    /*
    * Main.java
    *
    * Created on October 29, 2006, 12:27 AM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package randomizing;

    /**
    *
    * @author mltodd
    */


    import java.util.Random;

    public class Main {

    public static void main(String [] args) {for (int lap=1; lap <=20;
    lap++) {

    String [] input = {"ais ", "b ", "bes ", "bis ", "c' ", "ces' ",
    "cis' ",
    "d' ", "des' ", "dis' ", "e' ", "ees' ", "eis'
    ", "f' ", "fes' ",
    "fis' ", "g' ", "ges' ", "gis' ", "a' ", "aes'
    ", "ais' ", "b' ",
    "bes' ", "bis' ", "c'' ", "ces'' ", "cis''",
    "d'' ", "des'' ",
    "dis'' ", "e'' ", "ees'' ", "eis'' ", "f'' ",
    "fes'' ", "fis'' ",
    "g'' ", "ges'' ", "gis'' ", "a'' ", "aes'' ",
    "ais'' ", "b'' ",
    "bes'' ", "bis'' ", "c''' ", "ces''' ", "cis'''
    ", "d''' ",
    "des''' ", "dis''' ", "ees''' "} ;

    String [] result = randomSortArray(input);

    for (int i=0; i<result.length; i++)


    System.out.print (result);


    }
    }

    public static String [] randomSortArray(String [] input) {

    int size = input.length;
    int [] indices = new int[size];
    for (int i=0; i<size; i++)
    indices = i;

    Random random = new Random();
    for (int i=0; i<size; i++) {

    boolean unique = false;
    int randomNo = 0;
    while (!unique) {
    unique = true;
    randomNo = random.nextInt(size);
    for (int j=0; j<i; j++) {
    if (indices[j] == randomNo) {
    unique = false;
    break;
    }
    }
    }

    indices = randomNo;
    }

    String [] result = new String [size];
    for (int i=0; i<size; i++)
    result[indices] = input;

    return result;
    }

    }
     
    Louie LaRue, Oct 30, 2006
    #1
    1. Advertising

  2. Louie LaRue

    beyond Guest

    sorry! l can not do this!but l also want to kown the way>>
     
    beyond, Oct 30, 2006
    #2
    1. Advertising

  3. Louie LaRue

    Oliver Wong Guest

    "Louie LaRue" <> wrote in message
    news:...
    > This is a program that randomizies the strings in the array 20 times
    > and outputs them in a single line. How do I make it output 12 strings
    > per line?


    The fact that the array of strings is randomized is irrelevant for your
    question. If someone gave you a list of strings and told you to write them
    down (e.g. on a piece of paper) such that there were 12 strings per line,
    how would you do it as a human?

    - Oliver
     
    Oliver Wong, Oct 30, 2006
    #3
  4. beyond wrote:
    > sorry! l can not do this!but l also want to kown the way
    >


    No need to say so, just be patient and watch carefully!
     
    RedGrittyBrick, Oct 30, 2006
    #4
  5. Louie LaRue wrote:
    > This is a program that randomizies the strings in the array 20 times
    > and outputs them in a single line. How do I make it output 12 strings
    > per line?
    > for (int i=0; i<result.length; i++)
    > System.out.print (result);


    At this point I'd insert a statement involving i, 12, the modulo
    function and the println() method.

    >
    > }
    > }
     
    RedGrittyBrick, Oct 30, 2006
    #5
  6. Hi,

    Something like this will work:

    for (int i=0; i<result.length; i++){
    System.out.print (result);
    if (i%12 == 11){
    System.out.println("");
    }
    }

    Note a few important things about this code:

    1) You need curly braces around the code inside your FOR loop because
    you are doing more than one thing i.e. have more than one statement
    ended with a ";". I always use curly braces on my FORs, IFs etc even if
    I am only doing one thing. That way I can always put in extra lines
    like output lines without having to worry about the braces.

    2) "i%12" means get the remainder from dividing i by 12. If i=0, i%12
    will be 0. If i=12, i%12 will be 0... but if i=11, i%12 will be 11.
    This means execute what is inside the IF every 12th time around..

    Rob
    :)
     
    Robert Mark Bram, Oct 30, 2006
    #6
  7. Louie LaRue

    Louie LaRue Guest

    Thank you very much Robert. This works but every fourth line it prints
    17 strings per line. It works good enough for my purposes though. I
    appreciate it very much.

    sincerely, Lane


    Robert Mark Bram wrote:
    > Hi,
    >
    > Something like this will work:
    >
    > for (int i=0; i<result.length; i++){
    > System.out.print (result);
    > if (i%12 == 11){
    > System.out.println("");
    > }
    > }
    >
    > Note a few important things about this code:
    >
    > 1) You need curly braces around the code inside your FOR loop because
    > you are doing more than one thing i.e. have more than one statement
    > ended with a ";". I always use curly braces on my FORs, IFs etc even if
    > I am only doing one thing. That way I can always put in extra lines
    > like output lines without having to worry about the braces.
    >
    > 2) "i%12" means get the remainder from dividing i by 12. If i=0, i%12
    > will be 0. If i=12, i%12 will be 0... but if i=11, i%12 will be 11.
    > This means execute what is inside the IF every 12th time around..
    >
    > Rob
    > :)
     
    Louie LaRue, Nov 4, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S01aX3N0YXRl?=

    Quick one - Is SESSION per browser instance or per IP Address?

    =?Utf-8?B?S01aX3N0YXRl?=, Apr 4, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    5,944
    gerry
    Apr 10, 2006
  2. Razvan
    Replies:
    1
    Views:
    437
    tony vee
    Sep 10, 2004
  3. Hugo
    Replies:
    10
    Views:
    1,393
    Matt Humphrey
    Oct 18, 2004
  4. Replies:
    5
    Views:
    2,632
  5. Replies:
    0
    Views:
    370
Loading...

Share This Page