Solved

Can't convert java.lang.String to char.

Posted on 2004-04-18
4
4,334 Views
Last Modified: 2008-02-26
Hi
I wonder if any body could help me.
Please have a look on the following code:-

import java.rmi.*;
import java.io.*;


public class RoutingClient
{
    public static void main(String[] args)
    {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        try
        {
            Routing obj = (Routing) Naming.lookup("//localhost/Router");

          if ((args.length < 2)|| (args.length >2))
      {              {

System.out.println("usage: java httpc servername port page.html");
System.out.println("example: java httpc www.yahoo.com 80 index.html");
                         return;
                  }
                     //int lineno = args[0];
                     int lineno = Integer.parseInt(args[0]);
                     char code = args[1];
obj.addRoute(lineno,code);

}
}

there is an error on line <char code =args[1]

Error given by compiler is:-

 Incompatible type for declaration. Can't convert java.lang.String to char. char code = args[1];

How to convert String in to char
Thank YOU
0
Comment
Question by:Rafi-muqaddar
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 500 total points
ID: 10854346

String s = args[1];
char[] chars = s.toCharArray();
char code= chars[0];
0
 
LVL 20

Expert Comment

by:Venabili
ID: 10854768
char code = args[1].charAt(0);
0
 
LVL 20

Expert Comment

by:Venabili
ID: 10854774
Oops - did not see venkateshwarr's comment
So... you already have two valid answers.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10856837
>> char code = args[1];

What if args[1] has more than one character? Do you want to omit the other characters? If so, then the above code is fine, otherwise, you can try:

if ( args[1].length > 1 )
  System.out.println ( "Invalid argument. " ) ;
else
  code = args[1].charAt ( 0 ) ;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
for i loop in grovy 1 44
@SBGen Method 3 35
maven example error 3 52
how do i compare an object based on two fields 6 48
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now