For Programmers: Free Programming Magazines  


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.
Andrew Thompson

2004-03-28, 10:36 pm

On 28 Mar 2004 13:40:21 -0800, John. C wrote:

> I need help. I need your help to figure out what I am doing wrong
> about this (enclosed) juvenile code below:


Please do not cross-post,
<http://www.physci.org/codes/javafaq.jsp#xpost>

this is the best group at the moment..
<http://www.physci.org/codes/javafaq.jsp#cljh>

While we are at it, please do _not_ post
to groups not listed along with the last
link. Notably, invalid groups..

comp.lang.java.developer,comp.lang.java.api

f'Ups set to cljh.

> This code refuses to compile.


<http://www.physci.org/codes/javafaq.jsp#exact>

Having said that, you might look at
the constructor Character(char value),
which accepts a _char_, not a String.

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology
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
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com