This site requires JavaScript, please enable it in your browser!
Greenfoot back
jpcp8000
jpcp8000 wrote ...

2016/5/24

Help with parallel strings!

jpcp8000 jpcp8000

2016/5/24

#
I'm trying to write code that will let the user set their password length, and will output a random letter combination, which is the length the user specified.... Do i need to use a paralell array and greenfoot.getRandomNumber()? The code is listed below.. Thanks!
import greenfoot.*;
import java.awt.Color;
/**
 * Write a description of class Generator here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Generator extends World
{
    int i = 1;
    String[] Letters =
            {"1","2","3","4","5","6","7","8","9","0","A","a","B","b","C","c","D","d","E","e","F",
                "f","G","g","H","h","I","i","J","j","K","k","L","l","M","m","N","n","O","o","P",
                "p","Q","q","R","r","S","s","T","t","U","u","V","v","W","w","X","x","Y","y","Z","z",};
    /**
     * Constructor for objects of class Generator.
     * 
     */
    public Generator()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
    }
    
    public void act()
    {
      int J = Integer.parseInt(Greenfoot.ask("How many letters would you like in your new password?")); 
      while (i < J)
      {
        showText(Letters[i], 300, 200);
        i = i + 1;
        showText("Click the 'Reset' button to reset Password Generator V 1.0", 300, 50);
        Greenfoot.stop();
        get
      }
    }
}
danpost danpost

2016/5/24

#
If you want a random password, then you should not be using 'i' in line 34. A random number with the chance of the number of letters in your array would be more appropriate.
jpcp8000 jpcp8000

2016/5/25

#
Letters[Greenfoot.getRandomNumber(J)]
I changed up the Letters so its Letters but am still having trouble getting it to spit out "J" number of letters...
danpost danpost

2016/5/25

#
jpcp8000 wrote...
I changed up the Letters so its Letters but am still having trouble getting it to spit out "J" number of letters...
Well, you do have another problem here. Your 'while' loop places each randomly chosen letter at the same location, thereby replacing previously chosen letters. Change line 34 to this:
showText(Letters[Greenfoot.getRandomNumber(Letters.length)], 300+i*20, 200);
You can adjust the '20' to suit. Oh, and change line 11 to this:
int i = 0;
Counting in java starts at zero and ends one less than the limit.
jpcp8000 jpcp8000

2016/5/25

#
I cant see what line 34 should be. can you repost it?
danpost danpost

2016/5/25

#
jpcp8000 wrote...
I cant see what line 34 should be. can you repost it?
It was this:
showText(Letters[Greenfoot.getRandomNumber(Letters.length)], 300+i*20, 200);
That probably shows the same way however. It can be re-written like this:
String letter = Letters[Greenfoot.getRandomNumber(Letters.length)];
showText(letter, 300+i*20, 200);
You need to login to post a reply.