Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide Letters in Java

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Can anyone explain how to hide letters in java ?
For example, if I want to make the word "herbert" appear as _ _ _ _ _ _ _ , how do I do this ?

Thanks for any help. ;)
 
I want to take a word from a dictionary, then instead of displaying the actual word, I want to display it in the form _ _ _ on a GUI. So instead of a GUI with "The word is : herbert", my GUI should display "Thw word is : _ _ _ _ _ _ _".

I know theres a way to draw the lines in on the GUI, but I'm sure theres a simpler way.

Help!
 
In reality, you can just do this:

String test = "herbert";
int length = test.length();
char [] echochars = new char[length];
static final char ECHO_CHAR = '_';

for (int i = 0; i < length; i++)
echochars = ECHO_CHAR;

String test2 = new String(echochars);
System.out.println(test2);

There is also the notion of an 'echo character' that is used in some gui components, e.g. password fields that does just what you are saying.
 
Thanks for your help, but can you please tell me where the
echochars = ECHO_CHAR comes from and how it works exactly ?

Thank you for your help
 
Aww crud. That's a problem in this forum, it should have said:

echochars = ECHO_CHARS;

The tgml processor recognizes the index as a tag. I forgot to turn it off.
 
I'm confused. If I get your problem correctly you can write something to count the number of letter in your string e.g herbert is 7 letters. You hold 7 in a variable of type int lets say int numberOfLetters. Then you use a for loop from 0 to numberOfLetters (in this case 7). In this for loop you would be printing the character '_' to whereever you want it to go.

This is similar to meadandale's post. I've not given you a program more just the logic behind what you need to write.

Cheers

Chris
 
Thanks for your help, I just can't get this to work.

I get an error : Illegal start of expression
for the line: static final char ECHO_CHAR = '_';

I've tried it my own way, and using the code in the post above, and it doesn't work. I think its this one line thats stopping it working. Apart from that, the program seems to be ok...I think...



 
Example code:
Code:
import java.util.Arrays;

public class Toolkit {

    static final char MASK = '_';

    public static void main( String[] args ) {
        try {
            String in = args[0];
            int length = in.length();
            char[] mask = new char[ length ];
            Arrays.fill( mask, MASK );
            String out = new String( mask );
            System.out.println( in + &quot; becomes &quot; + out );
        } catch( Exception e ) {
            e.printStackTrace();
        }
    }
}
Cheers, Neil
 
Thanks Neil. I'm determined to get my echo char thing running just now though. It works, to a certain extent. For example, I'm outputting a word to DOS at the moment and want it to appear as - - - -. Basically, I want a - in place of a letter. However, say I wanted to output &quot;herbert&quot; in DOS, now what I get is :

-
--
---
----
-----
------
-------

It's like it does what I want, except on different lines.
Since h is the first letter, it goes on its own line with a &quot;-&quot;. e is the 2nd, so it gets &quot;- -&quot; on a separate line, etc.
I'm gonna put my fist through the screen in a minute....
 
Ok, this is crazy. This is the simplest program:

import java.io.*;

public class HerbertTest
{
static final char ECHO_CHAR = '-';

public static void main(String args[]) throws Exception
{
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));

String line = null;
printPrompt();
while ((line = in.readLine()) != null)
{
// Trim whitespace
line = line.trim();

// Get hidden representation
String hidden = getHidden(line);

// Print it out
System.out.println(&quot;Your hidden word is: &quot; + hidden);
printPrompt();
}
}

public static String getHidden(String str)
{
if (str == null || str.length() == 0)
return &quot;&quot;;

int len = str.length();
StringBuffer buf = new StringBuffer();
for (int i = 0; i < len; i++)
buf.append(ECHO_CHAR);

return buf.toString();
}

public static void printPrompt()
{
System.out.print(&quot;Enter a word to echo and press <Enter>:&quot;);
System.out.flush();
}

}

I've compiled this and run it and it does exactly what you want to the console.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top