DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Java (http://www.daniweb.com/forums/forum9.html)
-   -   console.nextChar(); ??? (http://www.daniweb.com/forums/thread158866.html)

georgewb Nov 21st, 2008 8:31 pm
console.nextChar(); HELP PLEASE???
 
Hi everyone,
I need your help please

What do i need to make my program read
the next character

using console.nextChar();

I've added the following:

import java.util.*;
static Scanner console = new Scanner(System.in);

and then in the main:

char ch;

ch = console.nextChar();

and when i compile i get this error::


cannot find symbol
symbol : method nextChar()
location: class java.util.Scanner
ch = console.nextChar();

can any 1 help me please??

thanks

Antenka Nov 21st, 2008 8:44 pm
Re: console.nextChar(); HELP PLEASE???
 
Scanner has no definition of method getChar.

georgewb Nov 21st, 2008 8:50 pm
Re: console.nextChar(); HELP PLEASE???
 
Quote:

Originally Posted by Antenka (Post 741773)
Scanner has no definition of method getChar.

so how can i make a program that reads the 1st character that the user enters:

for example:

if user enters :: A 1 2

ch= console.nextChar();
g = console.nextInt();
h= console.nextInt();

what can i use instead of nextChar()??
Thanks in advance

Antenka Nov 21st, 2008 9:07 pm
Re: console.nextChar(); ???
 
You can use scanner.next() it returns string, but you can convert it to char.

stultuske Nov 22nd, 2008 3:10 pm
Re: console.nextChar(); ???
 
just read it as String, convert it to an array of chars, or make a new Scanner class in which you define a nextChar() method :)

georgewb Nov 23rd, 2008 1:59 am
Re: console.nextChar(); ???
 
actually i found the answer

String a= console.next();
char b = (char)a.charAt(0);

thanks guys ;)

ingrid2 Feb 22nd, 2009 5:02 pm
Re: console.nextChar(); ???
 
mmm.... just in case you need this again, a better way to do it is:

variable = console.next().charAt(0);

I hope it helps...:)


All times are GMT -4. The time now is 11:27 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC