954,545 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

console.nextChar(); ???

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

georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Scanner has no definition of method getChar.

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 
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

georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

You can use scanner.next() it returns string, but you can convert it to char.

Antenka
Posting Whiz
362 posts since Nov 2008
Reputation Points: 293
Solved Threads: 82
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

actually i found the answer

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

thanks guys ;)

georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

mmm.... just in case you need this again, a better way to do it is:

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

I hope it helps...:)

ingrid2
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

hi buddyyyy.. i m ur new friend n recently join to this site...


u have seen ur problemm.... as u r still unable to get single character bu using scanner class....


you shud write:

char ch;
scanner scan = new scanner(System.in);
ch = scan.next(). charAt(0);


when u write the above statement u will able to read a character in java....

hifazat shah
Newbie Poster
1 post since Feb 2011
Reputation Points: 10
Solved Threads: 0
 

@hifazat
hiiii 'buddyyyy ...
if a thread is solved, it means he did succees in getting the caracter.
don't revive threads that haven't been active for over two years, don't revive threads that are marked 'solved' and try to use some proper language.
English is chosen here, since most developers are familiar with it, but then try to keep it with correct grammar and vocabulary, since we don't all have english as our first language.

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 
English is chosen here, since most developers are familiar with it,

The company I work for has 6k developers in America/Europe and 12k developers in India. I would agree that most developers certainly are familiar with English, but from my experience it is broken, heavily-accented English...haha

ztini
Posting Whiz in Training
299 posts since Jan 2011
Reputation Points: 54
Solved Threads: 52
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You