ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Yes, it looked fine to me too, but it doesn't work ;-)
Ok, I'll explain why I need an empty char.
I have a message (String) that has to be shown to a user. This message has to be formatted according to the channel the user is on.
The formatting of the channel is always different (screen height and screen widht for example). One of the parameters for the formatting of the test is a prefix character. This character can be a space or an empty char.
That's what I need the empty char for.
I know I could do it with a String, but that would mean I have to change quite a bit, so I'm really looking for an empty char.
maybe that will let you initialize an empty string through it's constructor or maybe the default constructer initializes the variable to an empty string or null. if it's null, i guess you could catch a null exception and do the necessary coding in that block. not very elegant, but i'm not a java guru either.
I already looked at the Character class, but found nothing that could help me. In fact, you always have to pass a char value to the constructor of a Character, so that won't work.
Don't know if this it what you're looking for as I only had a quick look at the Character class but
char ch=Character.UNASSIGNED;
seems to place a unicode value of zero into ch which translates to a null according to unicode table
Code:
public class test
{
public static void main(String args[])
{
final char ch=Character.UNASSIGNED;
String testString=new String(ch +"Hello");
//it's a null at the start
if(testString.charAt(0)==Character.UNASSIGNED)
System.out.println("Null char at string(0)" + " char value " + (int)testString.charAt(0));
else
System.out.println("Char at string(0) is " + testString.charAt(0));
//it's an x at the start
testString=new String("x" + "Hello");
if(testString.charAt(0)==Character.UNASSIGNED)
System.out.println("Null char at string(0)");
else
System.out.println("Char at string(0) is " + testString.charAt(0) + " char value " + (int)testString.charAt(0));
//it's a space at the start
testString=new String(" " + "Hello");
if(testString.charAt(0)==Character.UNASSIGNED)
System.out.println("Null char at string(0)");
else
System.out.println("Char at string(0) is " + testString.charAt(0) + " char value " + (int)testString.charAt(0));
}
}
No, that's not what I want, because even if the char is unassigned, it still prints something (in most cases a black rectangle). What I was looking for is a char that doesn't print anything.
You can't initialize an empty char. Cause how would it be a char in the first place if there is nothing in it? Either you store a character or you don't have a char.
The s.charAt(0) for an empty string won't work either since there is nothing at 0.
So you can either keep using chars and draw/display/print the prefix only when you have to (and not when you don't, instead of trying to use an empty char) or you could do the same thing with empty strings.
Oh, you're sounding like a C++ programmer. Java has primitives, and object references (not pointers.) A char is a primitive not a reference. Really it is simply a 16-bit unsigned integer. Those bits must be set to some value... the char can never "point" to something (or nothing ). A char instance variable that hasn't been explicitly initialized to some value is implicitly initialized to '\u0000'. A char automatic (method local) variable cannot be examined until it is explicitly initialized, otherwise the compiler will complain.
Originally posted by nephilim I have a message (String) that has to be shown to a user. This message has to be formatted according to the channel the user is on.
The formatting of the channel is always different (screen height and screen widht for example).
I don't know the details of what you're trying to do, but perhaps you should create a Message Interface, then create a separate class that implements Message for each different type of message formatting that you need. Each class that implements Message would know how to format itself. Then your channel could just aggregate Messages and not have to worry about how to format those Messages.
In other words, put the responsibility of message formatting on Message objects rather than having Channels doing the formatting.
This makes it really easy to add new types of formatting: simply create new classes that implement Message and properly format themselves. Just an idea.
Last edited by eric.r.turner; 09-24-2003 at 01:59 AM.
Well, the channel calling the application can't handle java objects, it expects a raw string. So if I create message objects for every format I will have to cast that to a string anyway. And since memory usage is an issue as well, I don't want to do that.
I worked around the problem by using a String as prefix instead of a char.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.