Results 1 to 2 of 2
  1. #1
    leeavital is offline Member
    Join Date
    Dec 2009
    Posts
    2
    Rep Power
    0

    Default I can only print string literals

    I'm writing a class to make a random password, but for some reason I can only print string literals in the main method. Can anyone tell me why?
    Java Code:
    public class RandomPassword
    {
    	/* main method, only used for testing methods */
    	public static void main(String[] args)
    	{
    		System.out.print(RandomPassword.fixLast("this_"));
    	}
    
    	/* returns random char alpha (caps or no caps) or underscore '_'
    	 *this will temporrily return char 'a', waiting for ***'s method */
    	public static char randomChar()
    	{
    		return 'a';
    	}
    	
    	/*fixLast, this will test to make sure that the last character of password is not '_'
    	 *if it is it will replace the last character with a random char (by calling randomChar) */
    	public static String fixLast(String pass)
    	{
    		//isolates the last char of the password
    		char lastChar = (pass.charAt(pass.length()-1));		
    		
    		//test if lastChar meets the required range.
    		while(('a' <= lastChar && 'z' >= lastChar) || ('A' <=lastChar && 90 >= 'Z'))
    		{
    			pass = pass.substring(0, pass.length()-1) + RandomPassword.randomChar();
    			lastChar = (pass.charAt(pass.length()-1));
    		}
    		return pass;
    	}
    		
    
    
    }

  2. #2
    CodesAway's Avatar
    CodesAway is offline Senior Member
    Join Date
    Sep 2009
    Location
    Texas
    Posts
    238
    Rep Power
    5

    Default

    Quote Originally Posted by leeavital View Post
    I'm writing a class to make a random password, but for some reason I can only print string literals in the main method. Can anyone tell me why?
    Your code returns a String, so it outputs a String, I'm confused to your question. What are you trying to output, but are unable to? - your code doesn't reflect this.

    Edit:
    After running your code and entering an infinite loop, I checked your code. What is it that you are trying to do with your loop in the fixLast method? Currently, it loops while the character is in the range (and it doesn't properly check the range).
    Last edited by CodesAway; 12-11-2009 at 01:29 AM.
    CodesAway - codesaway.info
    writing tools that make writing code a little easier

Similar Threads

  1. Replies: 7
    Last Post: 11-03-2009, 02:18 AM
  2. Replies: 5
    Last Post: 01-11-2009, 10:12 AM
  3. How to print chars with symbols from a string
    By blacksky in forum New To Java
    Replies: 23
    Last Post: 01-06-2009, 01:14 PM
  4. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM
  5. Replies: 0
    Last Post: 11-20-2007, 04:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •