Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile problem with char data type

    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!

  • #2
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts
    I think you shouldn't be using that on chars, its for reading streams of strings I think.


  • #3
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    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.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #4
    Senior Coder nikos101's Avatar
    Join Date
    Dec 2006
    Location
    London
    Posts
    1,007
    Thanks
    59
    Thanked 10 Times in 10 Posts

    Thumbs up

    Quote Originally Posted by Aradon View Post
    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


  • #5
    New to the CF scene
    Join Date
    Jan 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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

  • #6
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Quote Originally Posted by donski View Post
    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

    Code:
    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

    Code:
    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.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •