• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Homework Help

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey guys new here just wanted to get some help with my homework

Write A program that prints 25 random strings of length 4 such that each string is composed of uppercase alphabetical characters.


Is There an easier way to do this rather than linking strings together like in the code below

import java.util.*;

public class RandomString
{
public static void main(String args[])
{
String array[] = new String[26];

array[0] = "A";
array[1] = "B";
array[2] = "C";
array[3] = "D";
array[4] = "E";
array[5] = "F";
array[6] = "G";
array[7] = "H";
array[8] = "I";
array[9] = "J";
array[10] = "K";
array[11] = "L";
array[12] = "M";
array[13] = "N";
array[14] = "O";
array[15] = "P";
array[16] = "Q";
array[17] = "R";
array[18] = "S";
array[19] = "T";
array[20] = "U";
array[21] = "V";
array[22] = "W";
array[23] = "X";
array[24] = "Y";
array[25] = "Z";

Random generator = new Random();

for(int i = 0; i < 25; i++)
{
String line1 = array[generator.nextInt(25)];
String line2 = array[generator.nextInt(25)];
String line3 = array[generator.nextInt(25)];
String line4 = array[generator.nextInt(25)];

System.out.println(line1 + line2 + line3 + line4);
}

}
}
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

there is a limit to how much help one can give anybody with homework, but I can give hints.
  • You may have been told a char is a letter, but that ain't true. It is a number. Look here. If it is a number you can do arithmetic with it.
  • If you want random numbers, try the Random class. I prefer that to Math#random() because it has methods providing more‑or‑less what you want.
  • Move as much code out of the main method as you can.
  • String is an immutable class, not designed for changes. It has a counterpart class, designed for changes: This.

  • That code looks as if you had been guessing. Write down what you want on paper before you write any code at all. And divide and rule. Don't try 25 × 4 letters. Start with 1 × 1 letters and enlarge the program when you have got that working.
     
    author
    Posts: 23951
    142
    jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Davey Lopez wrote:
    Is There an easier way to do this rather than linking strings together like in the code below



    Yes, you can take advantage of the fact that the letters "A" to "Z" are linear in representation. So, there is no need for the lookup table. You can do something like this...

    String line1 = generator.nextInt(25) + 'A';


    Henry
     
    Davey Lopez
    Ranch Hand
    Posts: 33
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well I spoke to my professor He didn't like How I just stored the Uppercase letters in an array he wants me to cast them to char and use the ascii key code
     
    Ranch Hand
    Posts: 62
    5
    Eclipse IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Davey Lopez wrote:Well I spoke to my professor He didn't like How I just stored the Uppercase letters in an array he wants me to cast them to char and use the ascii key code


    If you want an array of chars you can do any of these:
    They are absolutely equivalent.
     
    Campbell Ritchie
    Marshal
    Posts: 79177
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Henry Wong wrote:. . . You can do something like this...

    String line1 = generator.nextInt(25) + 'A';


    Henry

    Has OP noticed the out‑by‑one error yet?
     
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Davey Lopez wrote:Well I spoke to my professor He didn't like How I just stored the Uppercase letters in an array he wants me to cast them to char and use the ascii key code


    Well I'd say that's rather subjective of him; although I suspect it's prompted by how much code it takes you to do something that is, in programming terms, really very simple.

    You can tell him from me that I, for one, like YOUR implementation better - and I've been programming for 35 years.

    Why? Because it doesn't rely on the fact that ASCII letters are contiguous, so it would work just as well with another coding system like EBCDIC.

    Assuming you take the other advice here on board and use chars instead of Strings for your array, you could reduce your initialization code enormously by using String.toCharArray(), viz:
    char[] array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();

    Try your idea out with that and see what he says.

    Winston

    PS: If you really must have a String array, you can also use:
    String[] array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("(?<=.)");
    but the explanation for how it works is quite advanced.
    If you're interested, have a look at the Java regular expressions page.
     
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hmm... OP asked for an easier way, not for a more clever or elegant way.
    And I do not find the alternatives easier than what the OP presented.

    Greetz,
    Piet
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Piet Souris wrote:Hmm... OP asked for an easier way, not for a more clever or elegant way.
    And I do not find the alternatives easier than what the OP presented.


    Erm ... didn't I just say that? All I suggested was a way to initialize variables more concisely.

    However - glad to know that I'm not alone.

    Winston
     
    Piet Souris
    Bartender
    Posts: 5465
    212
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Beg pardon!

    When I typed my little respons, all I remembered from your post was the string.split...
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic