LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-22-2003, 03:13 AM   #1
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Rep: Reputation: 30
empty char in java


Hi everyone,

I want to initialize an empty char in java, but I just found out that isn't as simple as I thought.

I tried

Code:
char ch = '';
But that doesn't work.

I also tried this:

Code:
String s = "";
char ch = s.charAt(0);
But that doesn't work either.

Does anyone know the solution to this one?

tnx
 
Old 09-22-2003, 03:34 AM   #2
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
what's the context in which you want to initialize ch?

Code:
char ch = '';
looks perfectly fine to me.

are you going to test it?
Code:
if ('' == ch)
{
	System.out.println ("you do-do!  enter a character!");
}
else
{
	//do stuff
}
i guess i'm asking you why doesn't it work for what you're wanting to do?

edit: nevermind. i see what you're talking about.

Last edited by megaspaz; 09-22-2003 at 03:42 AM.
 
Old 09-22-2003, 03:46 AM   #3
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
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.

Any suggestions?
 
Old 09-22-2003, 04:04 AM   #4
megaspaz
Senior Member
 
Registered: Nov 2002
Location: Silly Con Valley
Distribution: Red Hat 7.3, Red Hat 9.0
Posts: 2,054

Rep: Reputation: 46
yeah, my guess is that you'd still have to change your code a bit. how about looking into the Character class?

http://java.sun.com/docs/books/tutor...a/strings.html

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.
 
Old 09-22-2003, 04:23 AM   #5
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
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.
 
Old 09-22-2003, 07:59 AM   #6
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
Another thing I tried:

Code:
char ch = '\0';
but this still prints a weird-looking character to the screen. I'm starting to think that it just isn't possible to create an empty character in java.

I'm going to use a String to solve my problem. If anyone knows how to create an empty char, he or she is still very welcome to let me know.

tnx for the effort guys!
 
Old 09-22-2003, 09:35 AM   #7
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
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));




  }
}
 
Old 09-22-2003, 12:45 PM   #8
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
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.
 
Old 09-22-2003, 02:19 PM   #9
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
I guess I'm way off then, I was testing it on a console with a black background so didn't see anything.


To remove/hide/replace the square I guess you could add an extra line

testString=testString.replace((char)Character.UNASSIGNED, ' ');

Only a suggestion, as you're probably happy with what you got now.
 
Old 09-22-2003, 02:22 PM   #10
coolman0stress
Member
 
Registered: Jun 2003
Location: Toronto, Ontario, Canada
Posts: 288

Rep: Reputation: 30
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.
 
Old 09-22-2003, 02:27 PM   #11
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
Yup,

a truly empty char would be pointer to nothing;

char ch;
 
Old 09-24-2003, 01:47 AM   #12
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
Originally posted by Looking_Lost
Yup,

a truly empty char would be pointer to nothing;

char ch;
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.
 
Old 09-24-2003, 01:56 AM   #13
eric.r.turner
Member
 
Registered: Aug 2003
Location: Planet Earth
Distribution: Linux Mint
Posts: 216

Rep: Reputation: 31
Quote:
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.
 
Old 09-24-2003, 02:29 AM   #14
nephilim
Member
 
Registered: Aug 2003
Location: Belgium
Distribution: Debian (server), Kubuntu (desktop)
Posts: 248

Original Poster
Rep: Reputation: 30
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.

Thanks to all for your ideas and suggestions!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
java sybase char problem eantoranz Programming 5 03-07-2005 12:14 PM
Java - Reading /dev/mouse - appears to be empty chakkerz Linux - Hardware 1 08-19-2004 07:48 PM
invalid conversion from `char' to `const char* bru Programming 6 05-09-2004 03:07 PM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM
using java to find the location of the "\" char in a string caged Programming 8 02-03-2004 10:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration