Home > Archive > Java Help > March 2004 > Code I need help to figure out
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Code I need help to figure out
|
|
| John. C 2004-03-28, 10:36 pm |
| Dear Group Members,
I need help. I need your help to figure out what I am doing wrong
about this (enclosed) juvenile code below:
public class ConvertCharacter
{
public static void main(String [ ] args)
{
InputStreamReader k = new InputStreamReader( System.in) ;
BufferedReader input = new BufferedReader (k);
// tells user to enter characters from keyboard
System.out.println("Please enter a character");
String text = input.readLine(); // reads character input.
// char c1, c2;
char c1 = new Character(text).charValue(); // converts string input
to char type
char c2 = new Character(text).charValue();
System.out.print(c2); // prints characters in reversing other
System.out.print(c1);
System.out.println();
}
}
Yes go ahead and laugh at me. This code is very basic and I do not
know how to figure it out. It's okey, I just want to learn more of
this stuff. Go ahead, I can take it.
The problem is how to convert the input string from the keyboard to a
char type.
This code refuses to compile. It is straight to the point, I think. I
am trying to read characters from the keyboard. I just do not know
what I am doing wrong in this code. I had written and compiled other
codes using this method.
Help me Java community. I come to you with all humility. Send me an
email at ciscoguru1@msn.com or just post your suggestions. Thanks in
advance for your help.
Sincerely,
John C.
| |
|
|
| Fahd Shariff 2004-03-29, 3:34 am |
| Alternatively, for this particular problem you could use String
methods to access the characters in the input String...
String text = input.readLine() ;
char c1 = text.charAt(0) ;
char c2 = text.charAt(1) ;
System.out.print(c2) ;
System.out.print(c1) ;
System.out.println() ;
OR:
You could use a loop to iterate over the characters in the input
string in reverse order:
char c ;
for(int i = text.length()-1 ; i >= 0 ; i--)
{
c = text.charAt(i) ;
System.out.print(c) ;
}
OR:
To read a single character from the keyboard you could try:
char c = (char)input.read() ;
Hope this helps,
Fahd
http://www.fahdshariff.cjb.net
|
|
|
|
|