Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: reading a char with SCANNER

  1. #1
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default reading a char with SCANNER

    my teacher only teach to read char with system.in.read(),is there any way to read a char with scanner?? and is a String of "a" same as a char of 'a' .


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: reading a char with SCANNER

    Hello lotus,

    You can read a char with the Scanner class like this:

    import java.util.Scanner;
     
    public class lotus {
     
        /**
         * JavaProgrammingForums.com
         */
        public static void main(String[] args) {
     
            Scanner sc = new Scanner(System.in);
            String input = sc.next();
     
            char[] myChar;
            myChar = input.toCharArray();
     
            System.out.println(myChar);
        }
     
    }

    The Scanner class doesn't have .nextChar(); unlike .nextDouble(); .nextInt(); etc.

    Basically you use the Scanner class to read in the user input as String and then you need to convert the String to char.
    A char is a single character, a String is ment to be a sequence of characters. String is an full-blown object, a char primitive data type is just two bytes in unicode.

    I never use a char variable nowdays. Always just stick it in a String.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: reading a char with SCANNER

    That's a problem, though if you want to read in the next char after... It's only going to return the first char of each word

    To read in a single character, you can use the CharArrayReader class or the BufferedReader class. Actually, I think most of Reader's sub classes should be able to read in a single character.
    Last edited by helloworld922; July 28th, 2009 at 10:55 AM.

  4. #4
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: reading a char with SCANNER

    why do you use a char[]??? and not char myChar;

  5. #5
    Junior Member
    Join Date
    Jun 2009
    Posts
    24
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: reading a char with SCANNER

    because im at the final stage of my project, i have users input choice of 1 2 or 3 , but if a user inpuuts a non int number like a it gives a error, so if i want to solve these type of problems i have a String choice and then convert it to whatever i want it to be?

  6. #6
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: reading a char with SCANNER

    You'd want to make sure you use char[] for your passwords though

    // Json

  7. #7
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: reading a char with SCANNER

    Quote Originally Posted by lotus View Post
    why do you use a char[]??? and not char myChar;
    Take a read of this:

    String (Java Platform SE 6)
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. reading a sentence
    By lotus in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: July 20th, 2009, 08:29 AM
  2. Problem on reading file in Java program
    By nathanernest in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: April 13th, 2009, 08:14 AM
  3. Program to convert Hexadecimal to its Character equivalent
    By nathanernest in forum Java Theory & Questions
    Replies: 2
    Last Post: April 8th, 2009, 03:12 AM
  4. [SOLVED] Program to read from created file
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 7th, 2009, 03:51 AM
  5. [SOLVED] Problem in reading a file from java class
    By aznprdgy in forum File I/O & Other I/O Streams
    Replies: 11
    Last Post: March 23rd, 2009, 09:31 AM