Solved

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

Posted on 2004-04-18
4
4,328 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:mayankeagle
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
bigHeights  challenge 13 55
firstChar challenge 13 84
starter POM and spring-boot-starter,  spring-boot-web 2 14
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 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

19 Experts available now in Live!

Get 1:1 Help Now