6 Replies - 751 Views - Last Post: 24 July 2010 - 01:16 PM Rate Topic: -----

#1 Neelkamal0666  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 23-July 10

char input

Posted 23 July 2010 - 06:57 PM

How to accept char value from user.can I do it as
import java.io.*;
.....
....
char ch[]=new char[20];
DataInputStream ir=new DataInputStream(System.in);
..........
Ch=ir.readLine();
.....
.....
Is This A Good Question/Topic? 0
  • +

Replies To: char input

#2 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: char input

Posted 23 July 2010 - 07:33 PM

Are you familiar with the Scanner class at all? One way I could think of accepting a char value would be to allow the user to enter a 1 character String and then use the charAt() method to extract it. This really probably isn't the best way to do it, but right away without much thought, this is what came to mind. I'm sure someone will be around to provide more helpful feedback.

This post has been edited by Codebug: 23 July 2010 - 07:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: char input

Posted 23 July 2010 - 08:13 PM

by the way DataInputStream.readLine() is deprecated

Anyway it returns a String not an array of char[]

As Codebug suggested the best way is to use a Scanner

Scanner scan = new Scanner(System.in);
String line = scan.nextLine();
char[] digit = line.toCharArray();



digit[0] will hold the first character on the line

P.S.
if you expect to read only one character after the user hit it (without a <CR>) you just can't in Java as you can in C/C++.
Java is platform independant and the way TTY is handled in different OD is different
So the common way of handling user input is supported, the user should be able to enter characters and edit the line using <DEL>. A <CR> is used to confirm the input.

For game programming you can use a Keyboard listener but this requires a GUI.

Happy coding
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: char input

Posted 23 July 2010 - 09:35 PM

To read a single character, you can also do this:
char c = (char) System.in.read();


Was This Post Helpful? 0
  • +
  • -

#5 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 971
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: char input

Posted 23 July 2010 - 10:17 PM

View PostDogstopper, on 23 July 2010 - 08:35 PM, said:

To read a single character, you can also do this:
char c = (char) System.in.read();


Not without a line terminator. This code in NetBeans 6.9:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package myhash;

/**
 * @(#)MyHash.java
 *
 *
 * @author
 * @version 1.00 2010/5/1
 */
import java.io.*;
import java.util.*;

public class MyHash {

    public static void main(String args[]) {
        try {
            char c = (char) System.in.read();

            System.out.println(c);
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }
    }
}



produces:

run:
1234567890 // carriage return entered after the zero
1          // only then does the '1' appear as output
BUILD SUCCESSFUL (total time: 6 seconds)


Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2857
  • View blog
  • Posts: 10,961
  • Joined: 15-July 08

Re: char input

Posted 24 July 2010 - 05:21 AM

Oh...the question was without a line terminator? My bad. I was giving this as an easy way to read one character (with terminator)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: char input

Posted 24 July 2010 - 01:16 PM

View PostDogstopper, on 23 July 2010 - 10:35 PM, said:

To read a single character, you can also do this:
char c = (char) System.in.read();


You will neeed to enclose this statement in a try/catch block thow
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1