[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2004-04-18
4
Medium Priority
?
4,408 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

656 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