...

View Full Version : problem with char data type



donski
01-30-2008, 08:54 AM
hi!ive been practicing basic java programming.i encountered an error in using char data type in my program.i thought that input.readLine() can be used to accept an input of type char but there's an error. Is there any conversion that must be used to accept char as input just like when using int or float or double? thanks in advance!

nikos101
01-30-2008, 03:59 PM
I think you shouldn't be using that on chars, its for reading streams of strings I think.

Aradon
01-30-2008, 04:54 PM
What are you using for input? Are you using a scanner? Or are you actually using a BufferedReader?

What type of stream are you using?

Can we see the code that you're working on? There are ways to get just a character with certain input streams, but without knowing what you're doing we can't give you a complete answer.

nikos101
01-30-2008, 06:08 PM
What are you using for input? Are you using a scanner? Or are you actually using a BufferedReader?

What type of stream are you using?

Can we see the code that you're working on? There are ways to get just a character with certain input streams, but without knowing what you're doing we can't give you a complete answer.

I digg it :)

donski
02-01-2008, 03:20 AM
hi! tnx for the reply.im just using bufferedreader.its a very simple program,here it is:

import java.io.*;

public class test2
{
public static InputStreamReader reader = new InputStreamReader(System.in);
public static BufferedReader input = new BufferedReader(reader);

public static void main(String [] args) throws Exception
{ char choice;

System.out.print("Please enter your choice : ");
choice=input.readLine();
if(choice=='x')
System.out.println("HI!");
System.out.println("thank you!");

}
}

the error in this program is "incompatible types". i already tried to use string in this case and use .equals and it worked. i just want to use a char instead of string. tnx again in advance

Aradon
02-01-2008, 06:39 AM
hi! tnx for the reply.im just using bufferedreader.its a very simple program,here it is:

import java.io.*;

public class test2
{
public static InputStreamReader reader = new InputStreamReader(System.in);
public static BufferedReader input = new BufferedReader(reader);

public static void main(String [] args) throws Exception
{ char choice;

System.out.print("Please enter your choice : ");
choice=input.readLine();
if(choice=='x')
System.out.println("HI!");
System.out.println("thank you!");

}
}

the error in this program is "incompatible types". i already tried to use string in this case and use .equals and it worked. i just want to use a char instead of string. tnx again in advance

You could do various things. If you don't care what the users enters just as long as the first character that they enter is x, you could use charAt to grab the first character in the string choice

So something like




BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String choice = stdin.readline();
if(choice.charAt(0) == 'x' || choice.charAt(0) == 'X')
System.out.println("You hit the X key!");


Now doing this means that if the user types x, x, xtreme, xtastic, xsomething, etc. it will all work since we are just checking the first character.

If you wanted them to just type in X, then you should probably use the .equals method with String rather then a character check. That way you could know if they entered something else




BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
String choice = stdin.readline();
if(choice.equalsIgnoreCase("x"))
System.out.println("You just hit the X key!");
else
System.out.println("Stop giving me your jibber jabber");



In any case, both examples give you a good idea of some options that you have.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum