Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

How do I get this to output 12 strings per line

Thread view: 
Louie LaRue - 30 Oct 2006 14:49 GMT
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[i]);

   }
 }

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

   int size = input.length;
   int [] indices = new int[size];
   for (int i=0; i<size; i++)
     indices[i] = 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[i] = randomNo;
   }

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

   return result;
   }
 
}
beyond - 30 Oct 2006 15:13 GMT
sorry!  l can not do this!but l also want to kown the way>>
RedGrittyBrick - 30 Oct 2006 20:39 GMT
> 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!
Oliver Wong - 30 Oct 2006 16:50 GMT
> 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
RedGrittyBrick - 30 Oct 2006 20:41 GMT
> 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[i]);

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

>     }
>   }
Robert Mark Bram - 30 Oct 2006 22:46 GMT
Hi,

Something like this will work:

    for (int i=0; i<result.length; i++){
        System.out.print (result[i]);
        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 - 04 Nov 2006 09:49 GMT
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

> Hi,
>
[quoted text clipped - 21 lines]
> Rob
> :)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.