3 Replies - 14939 Views - Last Post: 16 May 2010 - 10:17 AM Rate Topic: -----

#1 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Character input using Scanner

Posted 16 May 2010 - 03:43 AM

Well I have been having trouble with the Scanner class because there is no method to input characters without using a String like the BufferedReader.read(); method. EXAMPLE:
import java.util.Scanner;
class CharScan {
public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Are you above 18? (y/n) ");
    do {
        String str=sc.next();//Inputs String
        char ch=str.charAt(0);
    }while(ch!='y' || ch!='Y' || ch!='n' || ch!='N');
    boolean legal=(ch=='y' || ch=='Y')?true:false;
    //More and more coding...
    }//ends Main method
}//ends class


Is there some way to get character input without using String or char[]?Any help will be helpful!

Is This A Good Question/Topic? 0
  • +

Replies To: Character input using Scanner

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Character input using Scanner

Posted 16 May 2010 - 04:16 AM

I'm afraid this is all the Scanner class has to offer in getting char input.
But it is with reason:
Using a Scanner to read many chars one at a time will be very inefficient, though probably inconsequential for the kind of programs you're writing now. Scanner will have to read chars from the underlying stream, buffer them, parse them, possibly convert them, then create a String for them.

To read chars directly, use a Reader. to parse streams of chars into tokens
and types, use a Scanner.
Was This Post Helpful? 1
  • +
  • -

#3 sh1n3  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 164
  • Joined: 22-April 10

Re: Character input using Scanner

Posted 16 May 2010 - 04:23 AM

View Postjapanir, on 16 May 2010 - 03:46 PM, said:

I'm afraid this is all the Scanner class has to offer in getting char input.
But it is with reason:
Using a Scanner to read many chars one at a time will be very inefficient, though probably inconsequential for the kind of programs you're writing now. Scanner will have to read chars from the underlying stream, buffer them, parse them, possibly convert them, then create a String for them.

To read chars directly, use a Reader. to parse streams of chars into tokens
and types, use a Scanner.

Guess I'll use BufferedReader again! Thank you for the help.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2554
  • View blog
  • Posts: 10,654
  • Joined: 20-September 08

Re: Character input using Scanner

Posted 16 May 2010 - 10:17 AM

You could do

        do {
            String str = sc.next(); //Inputs String
	    ...
        } while ("yYnN".indexOf(str) > -1);


This post has been edited by g00se: 16 May 2010 - 10:18 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1