1,105,594 Community Members

console.nextChar(); ???

Member Avatar
georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Antenka
Posting Whiz
368 posts since Nov 2008
Reputation Points: 274 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 9 [?]
 
0
 

Scanner has no definition of method getChar.

Member Avatar
georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Antenka
Posting Whiz
368 posts since Nov 2008
Reputation Points: 274 [?]
Q&As Helped to Solve: 86 [?]
Skill Endorsements: 9 [?]
 
0
 

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

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

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

Question Answered as of 5 Years Ago by Antenka and stultuske
Member Avatar
georgewb
Newbie Poster
3 posts since Nov 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

actually i found the answer

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

thanks guys ;)

Member Avatar
ingrid2
Newbie Poster
1 post since Feb 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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...:)

Member Avatar
hifazat shah
Newbie Poster
1 post since Feb 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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....

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
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.

Member Avatar
ztini
Posting Whiz in Training
291 posts since Jan 2011
Reputation Points: 8 [?]
Q&As Helped to Solve: 52 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article