Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2004

Tip: Looking for answers? Try searching our database.

Can't define a "char"

Thread view: 
JS - 08 Dec 2004 18:29 GMT
I am trying to to call a method in my main class. The method that I am
calling needs two arguments: a char (p) and a String (q).

But I get the following error when I try to define the char:

Error: incompatible types
found   : java.lang.String
required: char

Here is the code:

class TestSearch{
 public static void main(String[] args)
 {

   c = new Character((char)Integer
   char p = args[0];
   String q = args[1];
   Search z = new Search();
   System.out.println(z.findChar(p,q));

 }
}

class Search{
 public int findChar(char c, String s)
{ boolean found = false;
 int index = 0;
 while ( !found  &&  index < s.length() )
       { if ( s.charAt(index) == c )
              { found = true; }
         else { index = index + 1; }
       }
 if ( !found )
    { index = -1; }
 return index;
}
}

JS
Lothar Kimmeringer - 08 Dec 2004 18:32 GMT
> Error: incompatible types
> found   : java.lang.String
> required: char
>
> Here is the code:

[...]
>     char p = args[0];

String is not char. So you have to get the first char of the
String with charAt(0).

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

JS - 08 Dec 2004 18:41 GMT
> > Error: incompatible types
> > found   : java.lang.String
[quoted text clipped - 7 lines]
> String is not char. So you have to get the first char of the
> String with charAt(0).

But when I run the main class I need to type the char I want to search the
string for. Something like this in the console:

java TestSearch k punk

where "k" is the char I am seaching the string "punk" for.

JS
JS - 08 Dec 2004 18:49 GMT
> > > Error: incompatible types
> > > found   : java.lang.String
[quoted text clipped - 7 lines]
> > String is not char. So you have to get the first char of the
> > String with charAt(0).

Now I get it:

String p = args[0];
char r = p.charAt(0);

and now I can use r as my first argument!

Sorry for not thinking before asking

JS
Nicholas Parnell - 08 Dec 2004 21:13 GMT
you would assign  argument to a string first and then cast it to a char.
You might want to do a test case first to make sure the length of the string
is 1.

> > > Error: incompatible types
> > > found   : java.lang.String
[quoted text clipped - 16 lines]
>
> JS
Chris Smith - 09 Dec 2004 19:26 GMT
> you would assign  argument to a string first and then cast it to a char.
> You might want to do a test case first to make sure the length of the string
> is 1.

Well, you don't need a variable for something so simple.  Works just
fine to say:

char c = args[0].charAt(0);

(Please don't top-post.)

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

JS - 08 Dec 2004 18:33 GMT
> class TestSearch{
>   public static void main(String[] args)
>   {
>
>     c = new Character((char)Integer

this line was not meant to be there

JS


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.