Hi
I been trying to figure out this problem for last few days but cannot find the answer.
I am trying to use nested for loops to try and create a 6 character string using letters/numbers from a array. I need it so that it starts off with one character and then adds one more etc. Then need it to stop when it finds the word that has been inputted.

Example is
a
b
..z
then
aa
ab
ac
...az

or even

a
z
aa
ba
za etc

thanks

Recommended Answers

All 6 Replies

You need to look at the ASCII chart, and figure out what characters your program will look for.(i.e. 65-90 correspond to A-Z, 97-122 correspond to a-z) There is a distinction between upper case and lower case letters.
I'll write in pseudo-code to get you started.

//import the Scanner class to read in user input(import java.util.Scanner;)
//make a Scanner object(Scanner scan = new Scanner(System.in);)
//make a String to store the user input and read in user input
//make a character array to store each character that needs to be added

//for loop
for(int i = 0; i < userInput.length(); i++)//will move through the String on letter at a time
{
  for(int j = 32; j < 127; j++)//will move through the ASCII chart(32-127 is most of the  common character, punctuation, upper and lower case letters)
  {
     if(userInput.charAt(i) == (char)j)//you can cast an integer into a character
     {
       character[i] = (char)j;//
     }
  }
}
//print out the character array

The charAt() method can be used on Strings, this method returns a character.
When casting an integer into a character:

int k = 65;
char c = (char)k;
System.out.println(c); //c will be an upper case A when it is printed
//Where as
int j = 97;
char m = (char)j;
System.out.println(m);//m will be a lower case a when it is printed

I'm using pseudo-code to help you get started, because I don't want to just give you the answer. Cause you'll never get better if others give you the answer.
Concepts to understand:
-converting primitive types(i.e. char to int, int to char) ASCII chart helps
-String methods, .charAt(), .length()
-Arrays
Note: When you want the length of an array you do Not place parenthesis after length. Correct way for Arrays: c.length
Incorrect for Arrays: c.length()
For getting the length of a String you Do place parenthesis after length,
userInput.lenght()

If you have problems getting this coded, just post the code you've got.
I hope this gets you started.

thanks
but I currently have only lowercase letters and the numbers 0 to 9 in one Char array.
Im then using that 36 character array to produce the words.
I just need to figure out how to get them to start at one character and then add one more character after it has gone through the array once.

this is my messy code at the moment:

char[] letters = { 
          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
         's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0','1','2','3','4','5','6','7','8','9',
      };
      boolean found = false;
      char[] guess = new char[7];
      int pos = 0, i = 0;
      String word;
      
  // while (found == false) {
        loop:
         
            for (int a = 0; a<36; a++){


            guess[5] = letters[i];

      


                
             for (int b = 0; b<36; b++){
          
            
              guess[4] = letters[i];

    

                 for (int c = 0; c<36; c++){
    
        
                        guess[3] = letters[i];

                     

                 for (int d = 0; d<36; d++){

                         guess[2] = letters[i];
      

                        for (int e = 0; e<36; e++){
              
                            guess[1] = letters[e];
                             

                            for (int f = 0; f<36; f++){



   guess[0] = letters[f];


                          
     word = String.valueOf(guess);

Are you reading in the String word and trying to make a character array that displays the same thing as the String?

yes it has to guess the word by going through a array.
The actual comparing to the string is working fine.
Just the loop I need help on as I cant get it to start with one char and then add 2nd and so on.

This is modified from the code that I put up earlier, it use the letters array you defined.

for(int i = 0; i < userInput.length(); i++)//this loop starts at the 1st char and moves to the last char
{
  for(int j = 0; j < letters.length; j++)//this loop guesses characters
  {
     if(userInput.charAt(i) == letters[j])//checks to see if the guess is right
     {
       character[i] = letters[j];//If the guess is right then the letter gets added to the character array
     }
  }
}

The outer most for loop controls which character you are trying to guess, it starts with the first char and moves one by one till it gets to the last char.
The inner loop guesses characters and when it finds a match it adds it the character array.

thanks for that but i dont want the loop to use the length of the input string.
It needs to create a word between 1 and 6 characters. My code works but its just the making it start at one char from a till z and then move onto next char so aa..az then ba...bz is my issue.
I can get it to work doing that but it always had 6 characters all the time so it can never match a word less then 6. I am trying to make it so that it starts with one char and checks and then adds another char and checks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.