?
Solved

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

Posted on 2004-04-18
4
Medium Priority
?
4,381 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
venkateshwarr earned 2000 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 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