How do I get this to output 12 strings per line

L

Louie LaRue

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;
}

}
 
O

Oliver Wong

Louie LaRue said:
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
 
R

RedGrittyBrick

Louie said:
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.
 
R

Robert Mark Bram

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
:)
 
L

Louie LaRue

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top