MY question is,
like we do for integer and float,
Integer.parseInt(args[0])
Float.parseFloat(args[1])
How do we take character input from command line ?
MY question is,
like we do for integer and float,
Integer.parseInt(args[0])
Float.parseFloat(args[1])How do we take character input from command line ?
Those are methods to convert stuffs since command args are passed as Strings (i.e String[]).
So to get a string it is as easy as
String argX = args[i];//to get the i-1th argument
I want to pass a character not a string
The only thing passed to the program from the command line is the array of String.
main(String[] args)
You can use one of the String class methods to get a char out of a String.
that is what i am asking ??
how to parse the string into character like we do for integer and float???
You can use one of the String class methods to get a char out of a String.
Read the API doc for the String class. It has methods for accessing/getting the characters that make up the String.
i've read but didn't understand :(
Please copy and paste here the methods that you are not sure which one to use.
We'll help you know what they do and how to use them.
Look at all those methods which return a char.
It is very important that you know how to read and understand the API doc for java classes.
i think it is-- Character.parseChar()
@Megha SR
Where did you find the parseChar() method? I don't see it in the java API doc.
If your are going to give advice, at least try it in a small program and test it before posting it and wasting everyone's time.
I want to pass a character not a string
Isn't Character a string with length 1?
No. 'c' is a char and "c" is a String of length one.
A char is a primitive. A String is an object.
To expand on what norm said:
A char is a numeric primitive - its a 16 bit number that's used to hold a Unicode value representing an individual character in some chosen characterset. Primitives like char do not have methods.
A String is an Object that holds some text as a (private) array of chars and has many methods for working with them.
"Parse a String into a char" just doesn't make sense. A String of length n contains n chars.
If you want to get a char from some text input stream or String array you have to ensure that the String is of length 1, then get that one char from the String using its charAt method.
no a character is not a string. the reaseon is that a character is enclosed within single quotes whereas a string is enclosed within double quotes.
if you wanna get a character out from a string:
String s1 = "abc";
char ch = s1.charAt(0); // To get the first character from the String
if you wanna read a character from console :
char ch;
System.out.print("Enter a character : ");
ch = (char)System.in.read();
System.out.println("You have entered " + ch);
I hope this is what you are looking for =)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.