3

I am trying to write a method that can take a starting lowercase letter and an ending lowercase letter and generate random letters in that range. Next, the random letters are assigned to the elements of an array of a size determined by the user. So far, my code seems to work as long as the starting character is 'a'. I can't figure out how to make it work when the starting character is not 'a'. I wrote this complete program to try to troubleshoot my method:

import java.util.Random;
import java.util.Scanner;
public class Add
{
    public static void main( String[] args )
    {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter array size: ");
        int arraySize = scan.nextInt();
        char charArray[] = new char[arraySize];

        int fromChar;
        int toChar;
        do
        {
        System.out.print("Enter first character: ");
        String line = scan.next();
        fromChar = line.charAt(0);

        System.out.print("Enter second character: ");
        line = scan.next();
        toChar = line.charAt(0);

        Random generator = new Random();
        for(int i = 0; i < arraySize; i++)
        {
            int randomInt = generator.nextInt(toChar - fromChar + 1) + 97;
            //System.out.print(randomInt + " | "); //used to print unicode values
            charArray[i] = (char)(randomInt);
        }
        System.out.println(charArray);
    }
        while(true);

    } // end of main
}// end of class

Any help would be greatly appreciated!

Johnny
  • 675
  • 3
  • 15
  • 25
  • 2
    Hi John, welcome to SO. +1 for the SSCCE :) – Ben Nov 12 '12 at 05:11
  • SSCCE? Sorry, I'm new to lingo. :) – Johnny Nov 12 '12 at 05:17
  • 2
    "Short, Self Contained, Correct (Compilable), Example" (http://sscce.org/). I thought I knew the answer as soon as I read the code, but I was able to test in a couple of minutes because I did not need to write my own test wrapper. – Patricia Shanahan Nov 12 '12 at 05:20

3 Answers3

5
int randomInt = generator.nextInt(toChar - fromChar + 1) + fromChar;
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
  • @Patricia Shanahan Ok, now I am getting a NullPointerException when calling the method with my object. Is there something wrong with my constructor? public CharCollection(int arraySize)//constructor { char charArray[] = new char[arraySize]; numOfElements = arraySize; } – Johnny Nov 12 '12 at 06:06
  • Look at the line "char charArray[] = new char[arraySize];". It declares a local variable in the constructor and initializes it. If it was supposed to initialize a field in your object get rid of the initial "char" changing it from a declaration-with-initializer to an assignment. – Patricia Shanahan Nov 12 '12 at 08:21
  • Hmmm.. I am still a little confused. I understand that after declaring the array (done up above as an private instance variable), I need only to assign a value. eg. private int x; x = 1; I don't know how to translate this for an array though. – Johnny Nov 13 '12 at 17:56
  • private int[] myArray = new int[10]; or private int[] myArray = {2, 3, 100}; – Patricia Shanahan Nov 13 '12 at 21:20
3
int randomInt = generator.nextInt(toChar - fromChar + 1) + 97;

shouldn't this be: -

int randomInt = generator.nextInt(toChar - fromChar + 1) + fromChar;
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
javaPhobic
  • 476
  • 4
  • 12
0

*its will help you? if you will put where the 'a' for example A it will rand a UPPER CASE, and you put there number it will rand for you some char, i put there b, to start from b. *

public static void main(String[] args) {

    Random r = new Random();


    System.out.print((char) ((r.nextInt(26) + 'b')) + " ");

    System.out.println();


}
Ofir Attia
  • 1,237
  • 4
  • 21
  • 39