• Post Reply Bookmark Topic Watch Topic
  • New Topic

Homework Help  RSS feed

 
Davey Lopez
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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);
}

}
}
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • 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.
     
    Henry Wong
    author
    Sheriff
    Posts: 23295
    125
    C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
    • Mark post as helpful
    • send pies
    • 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
    • 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
     
    Andrea Binello
    Ranch Hand
    Posts: 62
    5
    Eclipse IDE Java Spring
    • Mark post as helpful
    • send pies
    • 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: 56541
    172
    • Mark post as helpful
    • send pies
    • 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?
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • 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.
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • 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: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • 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
    Master Rancher
    Posts: 2044
    75
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Beg pardon!

    When I typed my little respons, all I remembered from your post was the string.split...
     
    Consider Paul's rocket mass heater.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!